-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
646df2b
commit 51332f6
Showing
12 changed files
with
138 additions
and
84 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
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
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
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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class JwtRefreshGuard extends AuthGuard('jwt-refresh') {} |
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
38 changes: 38 additions & 0 deletions
38
packages/api-gateway/src/auth/strategies/jwt-refresh.strategy.ts
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 |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { Injectable, UnauthorizedException } from '@nestjs/common'; | ||
import { PassportStrategy } from '@nestjs/passport'; | ||
import { Strategy, ExtractJwt } from 'passport-jwt'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { User } from '../../user/user.entity'; | ||
import { IJwtPayload } from '../token.service'; | ||
import { UserService } from '../../user/user.service'; | ||
import { SessionService } from '../session.service'; | ||
|
||
@Injectable() | ||
export class JwtRefreshStrategy extends PassportStrategy( | ||
Strategy, | ||
'jwt-refresh', | ||
) { | ||
constructor( | ||
readonly config: ConfigService, | ||
private readonly session: SessionService, | ||
private readonly user: UserService, | ||
) { | ||
super({ | ||
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), | ||
ignoreExpiration: false, | ||
secretOrKey: config.get<string>('auth.secretRefreshToken'), | ||
}); | ||
} | ||
|
||
async validate(payload: IJwtPayload): Promise<User> { | ||
// Check that the session exist. On logout the session is destroyed but the token may still be valid | ||
const session = await this.session.findById(payload.jti); | ||
if (session === null || session.expires.getTime() < Date.now()) | ||
throw new UnauthorizedException(); | ||
|
||
// Get user from DB and add the result on req.user | ||
const user = await this.user.findById(payload.sub); | ||
if (user === null) throw new UnauthorizedException(); | ||
return user; | ||
} | ||
} |
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
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,29 +1,29 @@ | ||
export type EmptyObject = { | ||
[K in any]: never | ||
} | ||
[K in any]: never; | ||
}; | ||
|
||
export interface AppConfig { | ||
app: { | ||
name: string | ||
version: string | ||
} | ||
name: string; | ||
version: string; | ||
}; | ||
auth: { | ||
expAccessToken: string | ||
expRefreshToken: string | ||
expVerifyMail: string | ||
expResetPassword: string | ||
secretAccessToken: string | ||
cookieSecret: string | ||
} | ||
expAccessToken: string; | ||
expRefreshToken: string; | ||
expVerifyMail: string; | ||
expResetPassword: string; | ||
secretAccessToken: string; | ||
secretRefreshToken: string; | ||
}; | ||
db: { | ||
host: string | ||
port: number | ||
username: string | ||
password: string | ||
database: string | ||
} | ||
host: string; | ||
port: number; | ||
username: string; | ||
password: string; | ||
database: string; | ||
}; | ||
server: { | ||
address: string | ||
port: number | ||
} | ||
address: string; | ||
port: number; | ||
}; | ||
} |