feat(credentials): add jwt token verification and signing

In the credentials.service.ts file, jose is imported for jwt token operations. Two new methods are added. One method for jwt token verification and another for token signing. Token metadata such as IssuedAt, ExpirationTime, Issuer, Audience, and Subject are configured.
This commit is contained in:
Mathis H (Avnyr) 2024-07-09 16:03:20 +02:00
parent 17b07e9432
commit 3ac89964ab
Signed by: Mathis
GPG Key ID: DD9E0666A747D126

View File

@ -1,5 +1,6 @@
import { BadRequestException, Injectable } from "@nestjs/common";
import * as argon from "argon2";
import * as jose from "jose"
// biome-ignore lint/style/useImportType: used by Next.js
import { ConfigService } from "@nestjs/config";
@ -21,4 +22,23 @@ export class CredentialsService {
secret: this.configService.get("APP_HASH_SECRET"),
})
}
async verifyAuthToken(token: string) {
const verifyRes = await jose.jwtVerify(token, Uint8Array.from(this.configService.get("APP_TOKEN_SECRET")), {
subject: "auth",
audience: "user",
issuer: "ShouldStick"
})
}
async signAuthToken() {
return new jose.SignJWT({})
.setProtectedHeader({ alg: 'dir', enc: 'A128CBC-HS256' })
.setIssuedAt()
.setExpirationTime('3 day')
.setIssuer("ShouldStick")
.setAudience("user")
.setSubject("auth")
.sign(Uint8Array.from(this.configService.get("APP_TOKEN_SECRET")))
}
}