feat: add generic from payload data
Some checks failed
Publish / Publish Job (push) Has been cancelled
Some checks failed
Publish / Publish Job (push) Has been cancelled
This commit is contained in:
@@ -2,7 +2,7 @@ import { Inject, Injectable } from '@nestjs/common'
|
||||
import { createHmac, randomUUID } from 'crypto'
|
||||
|
||||
import { PASSPORT_OPTIONS } from './constants'
|
||||
import { PassportOptions, TokenRolePayload } from './interfaces'
|
||||
import { PassportOptions } from './interfaces'
|
||||
import { base64UrlDecode, base64UrlEncode, constantTimeEqual } from './utils'
|
||||
|
||||
const TOKEN_TYPES = {
|
||||
@@ -24,9 +24,8 @@ export class PassportService {
|
||||
this.SECRET_KEY = options.secretKey
|
||||
}
|
||||
|
||||
public generate(
|
||||
userId: string,
|
||||
role: TokenRolePayload,
|
||||
public generate<T extends Record<string, any>>(
|
||||
payload: T,
|
||||
ttl: number,
|
||||
tokenType: TokenType = TOKEN_TYPES.AUTH
|
||||
) {
|
||||
@@ -35,38 +34,38 @@ export class PassportService {
|
||||
|
||||
const jti = randomUUID()
|
||||
|
||||
const userPart = base64UrlEncode(userId)
|
||||
const rolePart = base64UrlEncode(JSON.stringify(role))
|
||||
const payloadPart = base64UrlEncode(JSON.stringify(payload))
|
||||
const iatPart = base64UrlEncode(String(issuedAt))
|
||||
const expPart = base64UrlEncode(String(expiresAt))
|
||||
const jtiPart = base64UrlEncode(jti)
|
||||
|
||||
const serialized = this.serialize(
|
||||
tokenType,
|
||||
userPart,
|
||||
rolePart,
|
||||
payloadPart,
|
||||
iatPart,
|
||||
expPart,
|
||||
jtiPart
|
||||
)
|
||||
const mac = this.computeHmac(this.SECRET_KEY, serialized)
|
||||
|
||||
return `${userPart}.${rolePart}.${iatPart}.${expPart}.${jtiPart}.${mac}`
|
||||
return `${payloadPart}.${iatPart}.${expPart}.${jtiPart}.${mac}`
|
||||
}
|
||||
|
||||
public verify(token: string, expectedType: TokenType = TOKEN_TYPES.AUTH) {
|
||||
public verify<T = any>(
|
||||
token: string,
|
||||
expectedType: TokenType = TOKEN_TYPES.AUTH
|
||||
) {
|
||||
try {
|
||||
const parts = token.split('.')
|
||||
|
||||
if (parts.length !== 6)
|
||||
if (parts.length !== 5)
|
||||
return { valid: false, reason: 'Не верный формат токена.' }
|
||||
|
||||
const [userPart, rolePart, iatPart, expPart, jtiPart, mac] = parts
|
||||
const [payloadPart, iatPart, expPart, jtiPart, mac] = parts
|
||||
|
||||
const serialized = this.serialize(
|
||||
expectedType,
|
||||
userPart,
|
||||
rolePart,
|
||||
payloadPart,
|
||||
iatPart,
|
||||
expPart,
|
||||
jtiPart
|
||||
@@ -87,13 +86,10 @@ export class PassportService {
|
||||
if (this.now() > expNumber)
|
||||
return { valid: false, reason: 'Срок действия токена истёк.' }
|
||||
|
||||
const decodedRole = JSON.parse(
|
||||
base64UrlDecode(rolePart)
|
||||
) as TokenRolePayload
|
||||
const decodedPayload = JSON.parse(base64UrlDecode(payloadPart)) as T
|
||||
return {
|
||||
valid: true,
|
||||
userId: base64UrlDecode(userPart),
|
||||
role: decodedRole,
|
||||
payload: decodedPayload,
|
||||
jti: base64UrlDecode(jtiPart)
|
||||
}
|
||||
} catch (error) {
|
||||
@@ -107,13 +103,12 @@ export class PassportService {
|
||||
|
||||
private serialize(
|
||||
domain: string,
|
||||
user: string,
|
||||
role: string,
|
||||
payload: string,
|
||||
iat: string,
|
||||
exp: string,
|
||||
jti: string
|
||||
) {
|
||||
return [domain, user, role, iat, exp, jti].join(
|
||||
return [domain, payload, iat, exp, jti].join(
|
||||
PassportService.INTERNAL_SEPARATOR
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user