-
Notifications
You must be signed in to change notification settings - Fork 19
Auth token routes #952
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Auth token routes #952
Changes from all commits
Commits
Show all changes
36 commits
Select commit
Hold shift + click to select a range
9425566
routes and unit tests
giurgiur99 fec1996
unit and integration tests
giurgiur99 9664a3b
add jwt
giurgiur99 a41cc02
add jwt types
giurgiur99 85d1a5f
fix unit tests
giurgiur99 7ac6c68
refactor to handler
giurgiur99 99d99b8
remove unused typesense schema
giurgiur99 c3451a8
remove console logs
giurgiur99 02611ca
revert ddo file
giurgiur99 3fce736
refactor
giurgiur99 b131405
use port 8000 tests
giurgiur99 d21dfdf
test with handler
giurgiur99 36d2cd1
revert aquarius
giurgiur99 a562545
decorator used for valdiation
giurgiur99 b9d0ffa
fix validation tests
giurgiur99 e4d18eb
support for new node instance
giurgiur99 5c0c0e4
force refresh node
giurgiur99 2e891da
new instance test
giurgiur99 8a4a997
fix bad enum
giurgiur99 0fe8395
force refresh handler
giurgiur99 c262457
cli tests paid compute
giurgiur99 eb1483c
revert main cli
giurgiur99 1c00310
specify message in handler
giurgiur99 54926c2
remove duplicate routes
giurgiur99 f0d2d66
implement skipValidation params
giurgiur99 7120eb1
override env tests
giurgiur99 51f9896
Merge branch 'main' into feature/auth-token
giurgiur99 38e0101
add header in handler
giurgiur99 987d194
add docker comput envs in ci
giurgiur99 843b029
Merge branch 'feature/auth-token' of https://github.com/oceanprotocol…
giurgiur99 6bfb248
reorder priority
giurgiur99 92e003a
add nonce
giurgiur99 42bc3f3
Merge branch 'main' into feature/auth-token
giurgiur99 d9c44c1
check nonce
giurgiur99 797a066
name routes
giurgiur99 3476f90
env example and use config
giurgiur99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| import { AuthToken, AuthTokenDatabase } from '../database/AuthTokenDatabase.js' | ||
| import jwt from 'jsonwebtoken' | ||
| import { checkNonce, NonceResponse } from '../core/utils/nonceHandler.js' | ||
| import { OceanNode } from '../../OceanNode.js' | ||
| import { getConfiguration } from '../../utils/index.js' | ||
|
|
||
| export interface CommonValidation { | ||
| valid: boolean | ||
| error: string | ||
| } | ||
|
|
||
| export class Auth { | ||
| private authTokenDatabase: AuthTokenDatabase | ||
|
|
||
| public constructor(authTokenDatabase: AuthTokenDatabase) { | ||
| this.authTokenDatabase = authTokenDatabase | ||
| } | ||
|
|
||
| public async getJwtSecret(): Promise<string> { | ||
| const config = await getConfiguration() | ||
| return config.jwtSecret | ||
| } | ||
|
|
||
| public getMessage(address: string, nonce: string): string { | ||
| return address + nonce | ||
| } | ||
|
|
||
| async getJWTToken(address: string, nonce: string, createdAt: number): Promise<string> { | ||
| const jwtToken = jwt.sign( | ||
| { | ||
| address, | ||
| nonce, | ||
| createdAt | ||
| }, | ||
| await this.getJwtSecret() | ||
| ) | ||
|
|
||
| return jwtToken | ||
| } | ||
|
|
||
| async insertToken( | ||
| address: string, | ||
| jwtToken: string, | ||
| validUntil: number, | ||
| createdAt: number | ||
| ): Promise<void> { | ||
| await this.authTokenDatabase.createToken(jwtToken, address, validUntil, createdAt) | ||
| } | ||
|
|
||
| async invalidateToken(jwtToken: string): Promise<void> { | ||
| await this.authTokenDatabase.invalidateToken(jwtToken) | ||
| } | ||
|
|
||
| async validateToken(token: string): Promise<AuthToken | null> { | ||
| const tokenEntry = await this.authTokenDatabase.validateToken(token) | ||
| if (!tokenEntry) { | ||
| return null | ||
| } | ||
| return tokenEntry | ||
| } | ||
|
|
||
| /** | ||
| * Validates the authentication or token | ||
| * You need to provider either a token or an address, signature and message | ||
| * @param {string} token - The token to validate | ||
| * @param {string} address - The address to validate | ||
| * @param {string} signature - The signature to validate | ||
| * @param {string} message - The message to validate | ||
| * @returns The validation result | ||
| */ | ||
| async validateAuthenticationOrToken({ | ||
| token, | ||
| address, | ||
| nonce, | ||
| signature | ||
| }: { | ||
| token?: string | ||
| address?: string | ||
| nonce?: string | ||
| signature?: string | ||
| }): Promise<CommonValidation> { | ||
| try { | ||
| if (signature && address && nonce) { | ||
| const oceanNode = OceanNode.getInstance() | ||
| const nonceCheckResult: NonceResponse = await checkNonce( | ||
| oceanNode.getDatabase().nonce, | ||
| address, | ||
| parseInt(nonce), | ||
| signature, | ||
| this.getMessage(address, nonce) | ||
| ) | ||
|
|
||
| if (!nonceCheckResult.valid) { | ||
| return { valid: false, error: nonceCheckResult.error } | ||
| } | ||
|
|
||
| if (nonceCheckResult.valid) { | ||
| return { valid: true, error: '' } | ||
| } | ||
| } | ||
|
|
||
| if (token) { | ||
| const authToken = await this.validateToken(token) | ||
| if (authToken) { | ||
| return { valid: true, error: '' } | ||
| } | ||
|
|
||
| return { valid: false, error: 'Invalid token' } | ||
| } | ||
|
|
||
| return { | ||
| valid: false, | ||
| error: | ||
| 'Invalid authentication, you need to provide either a token or an address, signature, message and nonce' | ||
| } | ||
| } catch (e) { | ||
| return { valid: false, error: `Error during authentication validation: ${e}` } | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.