-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: 🐞 secrets are generated with JWT
secrets are generated with JWT
- Loading branch information
tal-rofe
committed
Jul 29, 2022
1 parent
dc80bcf
commit 3299742
Showing
11 changed files
with
79 additions
and
19 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
9 changes: 6 additions & 3 deletions
9
apps/backend/src/modules/user/modules/secrets/classes/create-secret.dto.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 |
---|---|---|
@@ -1,13 +1,16 @@ | ||
import { IsNumber, IsString, MinLength } from 'class-validator'; | ||
import { IsISO8601, IsString, MinLength } from 'class-validator'; | ||
|
||
import { IsNullable } from '@/decorators/is-nullable.decorator'; | ||
|
||
import { IsValidExpiration } from '../decorators/valid-expiration.decorator'; | ||
|
||
export class CreateSecretDto { | ||
@IsString() | ||
@MinLength(1) | ||
readonly label!: string; | ||
|
||
@IsNumber() | ||
@IsISO8601() | ||
@IsValidExpiration() | ||
@IsNullable() | ||
readonly expiration!: number | null; | ||
readonly expiration!: string | null; | ||
} |
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
27 changes: 27 additions & 0 deletions
27
apps/backend/src/modules/user/modules/secrets/decorators/valid-expiration.decorator.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,27 @@ | ||
import { registerDecorator } from 'class-validator'; | ||
|
||
export function IsValidExpiration() { | ||
return function (object: object, propertyName: string) { | ||
registerDecorator({ | ||
name: 'validExpiration', | ||
target: object.constructor, | ||
propertyName: propertyName, | ||
validator: { | ||
validate(value: unknown) { | ||
if (typeof value !== 'string') { | ||
return false; | ||
} | ||
|
||
const currentDate = new Date(); | ||
const inputDate = new Date(value); | ||
|
||
if (isNaN(inputDate.getTime())) { | ||
return false; | ||
} | ||
|
||
return inputDate >= currentDate; | ||
}, | ||
}, | ||
}); | ||
}; | ||
} |
3 changes: 2 additions & 1 deletion
3
apps/backend/src/modules/user/modules/secrets/queries/contracts/create-secret.cotract.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 |
---|---|---|
@@ -1,7 +1,8 @@ | ||
export class CreateSecretContract { | ||
constructor( | ||
public readonly userId: string, | ||
public readonly email: string, | ||
public readonly label: string, | ||
public readonly expiration: Date | null, | ||
public readonly expiration: number | null, | ||
) {} | ||
} |
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
25 changes: 20 additions & 5 deletions
25
apps/backend/src/modules/user/modules/secrets/secrets.service.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 |
---|---|---|
@@ -1,12 +1,27 @@ | ||
import crypto from 'crypto'; | ||
|
||
import { Injectable } from '@nestjs/common'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { JwtService } from '@nestjs/jwt'; | ||
|
||
import type { IEnvironment } from '@/config/env.interface'; | ||
|
||
@Injectable() | ||
export class SecretsService { | ||
public generateSecret() { | ||
const secret = crypto.randomUUID(); | ||
constructor( | ||
private readonly configService: ConfigService<IEnvironment, true>, | ||
private readonly jwtService: JwtService, | ||
) {} | ||
|
||
public async generateSecret(userId: string, email: string, expiration: number | null) { | ||
const jwtPayload = { | ||
sub: userId, | ||
email, | ||
}; | ||
|
||
const token = await this.jwtService.signAsync(jwtPayload, { | ||
secret: this.configService.get('cliTokenJwtKey', { infer: true }), | ||
...(expiration ? { expiresIn: expiration } : {}), | ||
}); | ||
|
||
return secret; | ||
return token; | ||
} | ||
} |