-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: JWT Payload validation for 2FA Strategy (#235)
* fix: corrected jwt validation * style: lint fixes
- Loading branch information
Showing
4 changed files
with
32 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
|
@@ -159,7 +159,9 @@ describe('AuthService', () => { | |
|
||
it('should verify the JWT payload', async () => { | ||
const payload: JwtPayload = { | ||
sub: '[email protected]', | ||
sub: '[email protected]', | ||
email: '[email protected]', | ||
isTwoFactorAuthenticationEnabled: false, | ||
iat: 0, | ||
exp: 0 | ||
} | ||
|
@@ -176,6 +178,8 @@ describe('AuthService', () => { | |
it("should throw on verify when JWT's subject not exist", async () => { | ||
const payload: JwtPayload = { | ||
sub: '[email protected]', | ||
email: '[email protected]', | ||
isTwoFactorAuthenticationEnabled: false, | ||
iat: 0, | ||
exp: 0 | ||
} | ||
|
This file contains 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 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,32 +1,19 @@ | ||
import { ExtractJwt, Strategy } from 'passport-jwt' | ||
import { PassportStrategy } from '@nestjs/passport' | ||
import { Injectable } from '@nestjs/common' | ||
import { UserService } from '../../user/user.service' | ||
import { LoginWith2FAPayload } from '@isomera/interfaces' | ||
import { JwtPayload } from '@isomera/interfaces' | ||
import { AuthService } from '../auth.service' | ||
|
||
@Injectable() | ||
export class Jwt2faStrategy extends PassportStrategy(Strategy, 'jwt-2fa') { | ||
constructor(private readonly userService: UserService) { | ||
constructor(private readonly authService: AuthService) { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
secretOrKey: process.env.APP_SECRET | ||
}) | ||
} | ||
|
||
async validate(payload: LoginWith2FAPayload) { | ||
const user = await this.userService.findOne({ | ||
where: { email: payload.email } | ||
}) | ||
|
||
if (!user.isTwoFAEnabled) { | ||
return user | ||
} | ||
|
||
if (payload.isTwoFactorAuthenticated) { | ||
return { | ||
...user, | ||
isTwoFactorAuthenticated: payload.isTwoFactorAuthenticated | ||
} | ||
} | ||
async validate(payload: JwtPayload) { | ||
return this.authService.verifyPayload(payload) | ||
} | ||
} |
This file contains 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