|
| 1 | +import { AuthToken, AuthTokenDatabase } from '../database/AuthTokenDatabase.js' |
| 2 | +import jwt from 'jsonwebtoken' |
| 3 | +import { checkNonce, NonceResponse } from '../core/utils/nonceHandler.js' |
| 4 | +import { OceanNode } from '../../OceanNode.js' |
| 5 | +import { getConfiguration } from '../../utils/index.js' |
| 6 | + |
| 7 | +export interface CommonValidation { |
| 8 | + valid: boolean |
| 9 | + error: string |
| 10 | +} |
| 11 | + |
| 12 | +export class Auth { |
| 13 | + private authTokenDatabase: AuthTokenDatabase |
| 14 | + |
| 15 | + public constructor(authTokenDatabase: AuthTokenDatabase) { |
| 16 | + this.authTokenDatabase = authTokenDatabase |
| 17 | + } |
| 18 | + |
| 19 | + public async getJwtSecret(): Promise<string> { |
| 20 | + const config = await getConfiguration() |
| 21 | + return config.jwtSecret |
| 22 | + } |
| 23 | + |
| 24 | + public getMessage(address: string, nonce: string): string { |
| 25 | + return address + nonce |
| 26 | + } |
| 27 | + |
| 28 | + async getJWTToken(address: string, nonce: string, createdAt: number): Promise<string> { |
| 29 | + const jwtToken = jwt.sign( |
| 30 | + { |
| 31 | + address, |
| 32 | + nonce, |
| 33 | + createdAt |
| 34 | + }, |
| 35 | + await this.getJwtSecret() |
| 36 | + ) |
| 37 | + |
| 38 | + return jwtToken |
| 39 | + } |
| 40 | + |
| 41 | + async insertToken( |
| 42 | + address: string, |
| 43 | + jwtToken: string, |
| 44 | + validUntil: number, |
| 45 | + createdAt: number |
| 46 | + ): Promise<void> { |
| 47 | + await this.authTokenDatabase.createToken(jwtToken, address, validUntil, createdAt) |
| 48 | + } |
| 49 | + |
| 50 | + async invalidateToken(jwtToken: string): Promise<void> { |
| 51 | + await this.authTokenDatabase.invalidateToken(jwtToken) |
| 52 | + } |
| 53 | + |
| 54 | + async validateToken(token: string): Promise<AuthToken | null> { |
| 55 | + const tokenEntry = await this.authTokenDatabase.validateToken(token) |
| 56 | + if (!tokenEntry) { |
| 57 | + return null |
| 58 | + } |
| 59 | + return tokenEntry |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Validates the authentication or token |
| 64 | + * You need to provider either a token or an address, signature and message |
| 65 | + * @param {string} token - The token to validate |
| 66 | + * @param {string} address - The address to validate |
| 67 | + * @param {string} signature - The signature to validate |
| 68 | + * @param {string} message - The message to validate |
| 69 | + * @returns The validation result |
| 70 | + */ |
| 71 | + async validateAuthenticationOrToken({ |
| 72 | + token, |
| 73 | + address, |
| 74 | + nonce, |
| 75 | + signature |
| 76 | + }: { |
| 77 | + token?: string |
| 78 | + address?: string |
| 79 | + nonce?: string |
| 80 | + signature?: string |
| 81 | + }): Promise<CommonValidation> { |
| 82 | + try { |
| 83 | + if (signature && address && nonce) { |
| 84 | + const oceanNode = OceanNode.getInstance() |
| 85 | + const nonceCheckResult: NonceResponse = await checkNonce( |
| 86 | + oceanNode.getDatabase().nonce, |
| 87 | + address, |
| 88 | + parseInt(nonce), |
| 89 | + signature, |
| 90 | + this.getMessage(address, nonce) |
| 91 | + ) |
| 92 | + |
| 93 | + if (!nonceCheckResult.valid) { |
| 94 | + return { valid: false, error: nonceCheckResult.error } |
| 95 | + } |
| 96 | + |
| 97 | + if (nonceCheckResult.valid) { |
| 98 | + return { valid: true, error: '' } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (token) { |
| 103 | + const authToken = await this.validateToken(token) |
| 104 | + if (authToken) { |
| 105 | + return { valid: true, error: '' } |
| 106 | + } |
| 107 | + |
| 108 | + return { valid: false, error: 'Invalid token' } |
| 109 | + } |
| 110 | + |
| 111 | + return { |
| 112 | + valid: false, |
| 113 | + error: |
| 114 | + 'Invalid authentication, you need to provide either a token or an address, signature, message and nonce' |
| 115 | + } |
| 116 | + } catch (e) { |
| 117 | + return { valid: false, error: `Error during authentication validation: ${e}` } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments