-
Notifications
You must be signed in to change notification settings - Fork 44
[Human App] Implement JWT authentication guard and strategy to validate the token #3394
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
88bea4e
feat: Implement JWT authentication guard and strategy to validate the…
flopez7 d359653
fix: Restore host check logic in ForbidUnauthorizedHostMiddleware
flopez7 7c8edfe
Add Public decorator to bypass JWT auth for specific endpoints and en…
flopez7 a1db3a3
Merge branch 'develop' into feat/human-app/validate-jwt
flopez7 5536a87
add JWT user email and site key for hcaptcha endpoints
flopez7 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
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
39 changes: 0 additions & 39 deletions
39
packages/apps/human-app/server/src/common/config/params-decorators.ts
This file was deleted.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const JWT_KVSTORE_KEY = 'jwt_public_key'; |
11 changes: 11 additions & 0 deletions
11
packages/apps/human-app/server/src/common/decorators/index.ts
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 |
|---|---|---|
| @@ -1 +1,12 @@ | ||
| import { Reflector } from '@nestjs/core'; | ||
|
|
||
| export * from './enums'; | ||
|
|
||
| /** | ||
| * Decorator for HTTP endpoints to bypass JWT auth guard | ||
| * where JWT auth not needed | ||
| */ | ||
| export const Public = Reflector.createDecorator<boolean>({ | ||
| key: 'isPublic', | ||
| transform: () => true, | ||
| }); |
40 changes: 40 additions & 0 deletions
40
packages/apps/human-app/server/src/common/guards/jwt.auth.ts
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,40 @@ | ||
| import { | ||
| CanActivate, | ||
| ExecutionContext, | ||
| Injectable, | ||
| UnauthorizedException, | ||
| } from '@nestjs/common'; | ||
| import { Reflector } from '@nestjs/core'; | ||
| import { AuthGuard } from '@nestjs/passport'; | ||
| import { JwtUserData } from '../utils/jwt-token.model'; | ||
|
|
||
| @Injectable() | ||
| export class JwtAuthGuard extends AuthGuard('jwt-http') implements CanActivate { | ||
| constructor(private readonly reflector: Reflector) { | ||
| super(); | ||
| } | ||
|
|
||
| public async canActivate(context: ExecutionContext): Promise<boolean> { | ||
| // Check for public routes first | ||
| const isPublic = this.reflector.getAllAndOverride<boolean>('isPublic', [ | ||
| context.getHandler(), | ||
| context.getClass(), | ||
| ]); | ||
|
|
||
| if (isPublic) { | ||
| return true; | ||
| } | ||
|
|
||
| // Try to authenticate with JWT | ||
| await super.canActivate(context); | ||
|
|
||
| const request = context.switchToHttp().getRequest(); | ||
| const user = request.user as JwtUserData; | ||
| if (!user) { | ||
| throw new UnauthorizedException('User not found in request'); | ||
| } | ||
| request.token = request.headers['authorization']; | ||
|
|
||
| return true; | ||
| } | ||
| } | ||
1 change: 1 addition & 0 deletions
1
packages/apps/human-app/server/src/common/guards/strategy/index.ts
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 @@ | ||
| export * from './jwt.http'; |
67 changes: 67 additions & 0 deletions
67
packages/apps/human-app/server/src/common/guards/strategy/jwt.http.ts
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,67 @@ | ||
| import { Injectable, Req, UnauthorizedException } from '@nestjs/common'; | ||
| import { PassportStrategy } from '@nestjs/passport'; | ||
| import * as jwt from 'jsonwebtoken'; | ||
| import { ExtractJwt, Strategy } from 'passport-jwt'; | ||
| import { EnvironmentConfigService } from '../../../common/config/environment-config.service'; | ||
| import { JwtUserData } from '../../../common/utils/jwt-token.model'; | ||
| import { KvStoreGateway } from '../../../integrations/kv-store/kv-store.gateway'; | ||
|
|
||
| @Injectable() | ||
| export class JwtHttpStrategy extends PassportStrategy(Strategy, 'jwt-http') { | ||
| constructor( | ||
| private readonly configService: EnvironmentConfigService, | ||
| private readonly kvStoreGateway: KvStoreGateway, | ||
| ) { | ||
| super({ | ||
| jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
| ignoreExpiration: false, | ||
| secretOrKeyProvider: async ( | ||
| _request: any, | ||
| rawJwtToken: any, | ||
| done: any, | ||
| ) => { | ||
| try { | ||
| const payload = jwt.decode(rawJwtToken); | ||
| const chainId = this.configService.chainIdsEnabled[0]; | ||
| const address = (payload as any).reputation_network; | ||
| const pubKey = await this.kvStoreGateway.getReputationOraclePublicKey( | ||
| chainId, | ||
| address, | ||
| ); | ||
| done(null, pubKey); | ||
| } catch (error) { | ||
| console.error(error); | ||
| done(error); | ||
| } | ||
| }, | ||
| passReqToCallback: true, | ||
| }); | ||
| } | ||
|
|
||
| public async validate( | ||
| @Req() _request: any, | ||
| payload: { | ||
| user_id: string; | ||
| status: string; | ||
| wallet_address: string; | ||
| reputation_network: string; | ||
| qualifications?: string[]; | ||
| site_key?: string; | ||
| email?: string; | ||
| }, | ||
| ): Promise<JwtUserData> { | ||
| if (!payload.user_id) { | ||
| throw new UnauthorizedException('Invalid token: missing user id'); | ||
| } | ||
|
|
||
| return { | ||
|
dnechay marked this conversation as resolved.
|
||
| user_id: payload.user_id, | ||
| wallet_address: payload.wallet_address, | ||
| status: payload.status, | ||
| reputation_network: payload.reputation_network, | ||
| qualifications: payload.qualifications, | ||
| site_key: payload.site_key, | ||
| email: payload.email, | ||
| }; | ||
| } | ||
| } | ||
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,6 @@ | ||
| import { JwtUserData } from '../utils/jwt-token.model'; | ||
|
|
||
| export interface RequestWithUser extends Request { | ||
| user: JwtUserData; | ||
| token: string; | ||
| } |
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
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.