Skip to content
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

Development #497

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ AUTH_JWT_SUBJECT=AckDevelopment
AUTH_JWT_ISSUER=ack
AUTH_JWT_AUDIENCE=https://example.com

AUTH_JWT_ACCESS_TOKEN_SECRET_KEY=1234567890
AUTH_JWT_ACCESS_TOKEN_EXPIRED=1h
AUTH_JWT_ACCESS_TOKEN_SECRET_KEY=1234567890
AUTH_JWT_REFRESH_TOKEN_EXPIRED=182d
AUTH_JWT_REFRESH_TOKEN_SECRET_KEY=0987654321

AUTH_JWT_PAYLOAD_ENCRYPT=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class AuthJwtRefreshStrategy extends PassportStrategy(
jwtFromRequest: ExtractJwt.fromAuthHeaderWithScheme(
configService.get<string>('auth.prefixAuthorization')
),
ignoreExpiration: true,
ignoreExpiration: false,
jsonWebTokenOptions: {
ignoreNotBefore: true,
audience: configService.get<string>('auth.audience'),
Expand Down
6 changes: 5 additions & 1 deletion src/common/auth/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class AuthService implements IAuthService {
private readonly accessTokenEncryptIv: string;

private readonly refreshTokenSecretKey: string;
private readonly refreshTokenExpirationTime: number;
private readonly refreshTokenEncryptKey: string;
private readonly refreshTokenEncryptIv: string;

Expand Down Expand Up @@ -61,6 +62,9 @@ export class AuthService implements IAuthService {
this.refreshTokenSecretKey = this.configService.get<string>(
'auth.refreshToken.secretKey'
);
this.refreshTokenExpirationTime = this.configService.get<number>(
'auth.refreshToken.expirationTime'
);
this.refreshTokenEncryptKey = this.configService.get<string>(
'auth.refreshToken.encryptKey'
);
Expand Down Expand Up @@ -172,7 +176,7 @@ export class AuthService implements IAuthService {
{ data: payloadHashed },
{
secretKey: this.refreshTokenSecretKey,
expiredIn: '0',
expiredIn: this.refreshTokenExpirationTime,
audience: this.audience,
issuer: this.issuer,
subject: this.subject,
Expand Down
7 changes: 5 additions & 2 deletions src/common/common.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ import { PolicyModule } from 'src/common/policy/policy.module';
AUTH_JWT_AUDIENCE: Joi.string().required(),
AUTH_JWT_ISSUER: Joi.string().required(),

AUTH_JWT_ACCESS_TOKEN_EXPIRED: Joi.string()
.default('15m')
.required(),
AUTH_JWT_ACCESS_TOKEN_SECRET_KEY: Joi.string()
.alphanum()
.min(5)
.max(50)
.required(),
AUTH_JWT_ACCESS_TOKEN_EXPIRED: Joi.string()
.default('15m')
AUTH_JWT_REFRESH_TOKEN_EXPIRED: Joi.string()
.default('182d')
.required(),
AUTH_JWT_REFRESH_TOKEN_SECRET_KEY: Joi.string()
.alphanum()
Expand Down
4 changes: 3 additions & 1 deletion src/common/helper/interfaces/helper.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export interface IHelperJwtVerifyOptions {
issuer: string;
subject: string;
secretKey: string;
ignoreExpiration?: boolean;
}

export interface IHelperJwtOptions extends IHelperJwtVerifyOptions {
export interface IHelperJwtOptions
extends Omit<IHelperJwtVerifyOptions, 'ignoreExpiration'> {
expiredIn: number | string;
notBefore?: number | string;
}
Expand Down
1 change: 1 addition & 0 deletions src/common/helper/services/helper.encryption.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class HelperEncryptionService implements IHelperEncryptionService {
audience: options.audience,
issuer: options.issuer,
subject: options.subject,
ignoreExpiration: options.ignoreExpiration ?? false,
});

return true;
Expand Down
3 changes: 3 additions & 0 deletions src/configs/auth.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ export default registerAs(
refreshToken: {
secretKey:
process.env.AUTH_JWT_REFRESH_TOKEN_SECRET_KEY ?? '123456000',
expirationTime: seconds(
process.env.AUTH_JWT_REFRESH_TOKEN_EXPIRED ?? '182d'
), // 1 hours

encryptKey: process.env.AUTH_JWT_PAYLOAD_REFRESH_TOKEN_ENCRYPT_KEY,
encryptIv: process.env.AUTH_JWT_PAYLOAD_REFRESH_TOKEN_ENCRYPT_IV,
Expand Down