chore(repo): fix jwt token strategies + fix AccountsEntity
This commit is contained in:
parent
0148082d0a
commit
d6ffa909aa
@ -1,5 +1,5 @@
|
|||||||
export interface TokenPayload {
|
export interface TokenPayload {
|
||||||
userId: number;
|
sub: number;
|
||||||
iat?: number;
|
iat?: number;
|
||||||
exp?: number;
|
exp?: number;
|
||||||
}
|
}
|
||||||
|
@ -2,9 +2,9 @@ import { Inject, Injectable, UnauthorizedException } from '@nestjs/common';
|
|||||||
import { PassportStrategy } from '@nestjs/passport';
|
import { PassportStrategy } from '@nestjs/passport';
|
||||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||||
import { AuthConfig, authConfig } from '../../auth.config';
|
import { AuthConfig, authConfig } from '../../auth.config';
|
||||||
import { RequestWithUser } from '../../constants/request-with-user';
|
|
||||||
import { TokenPayload } from '../../constants/token-payload.interface';
|
import { TokenPayload } from '../../constants/token-payload.interface';
|
||||||
import { UsersAuthService } from '../../users/users-auth.service';
|
import { UsersAuthService } from '../../users/users-auth.service';
|
||||||
|
import { FastifyRequest } from 'fastify';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class JwtAccessTokenAuthStrategy extends PassportStrategy(Strategy, 'jwt-access-token') {
|
export class JwtAccessTokenAuthStrategy extends PassportStrategy(Strategy, 'jwt-access-token') {
|
||||||
@ -16,8 +16,8 @@ export class JwtAccessTokenAuthStrategy extends PassportStrategy(Strategy, 'jwt-
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async validate(request: RequestWithUser, payload: TokenPayload) {
|
async validate(request: FastifyRequest, payload: TokenPayload) {
|
||||||
const account = await this.userAuthService.getUserById(payload.userId);
|
const account = await this.userAuthService.getUserById(payload.sub);
|
||||||
if (!account) throw new UnauthorizedException('Access Token Guard');
|
if (!account) throw new UnauthorizedException('Access Token Guard');
|
||||||
return account;
|
return account;
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,6 @@ import { PassportStrategy } from '@nestjs/passport';
|
|||||||
import { FastifyRequest } from 'fastify';
|
import { FastifyRequest } from 'fastify';
|
||||||
import { ExtractJwt, Strategy } from 'passport-jwt';
|
import { ExtractJwt, Strategy } from 'passport-jwt';
|
||||||
import { authConfig, AuthConfig } from '../../auth.config';
|
import { authConfig, AuthConfig } from '../../auth.config';
|
||||||
import { RequestWithUser } from '../../constants/request-with-user';
|
|
||||||
import { TokenPayload } from '../../constants/token-payload.interface';
|
import { TokenPayload } from '../../constants/token-payload.interface';
|
||||||
import { UsersAuthService } from '../../users/users-auth.service';
|
import { UsersAuthService } from '../../users/users-auth.service';
|
||||||
|
|
||||||
@ -11,19 +10,15 @@ import { UsersAuthService } from '../../users/users-auth.service';
|
|||||||
export class JwtRefreshTokenAuthStrategy extends PassportStrategy(Strategy, 'jwt-refresh-token') {
|
export class JwtRefreshTokenAuthStrategy extends PassportStrategy(Strategy, 'jwt-refresh-token') {
|
||||||
constructor(@Inject(authConfig.KEY) authConfig: AuthConfig, private readonly usersAuthService: UsersAuthService) {
|
constructor(@Inject(authConfig.KEY) authConfig: AuthConfig, private readonly usersAuthService: UsersAuthService) {
|
||||||
super({
|
super({
|
||||||
jwtFromRequest: ExtractJwt.fromExtractors([
|
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
|
||||||
(request: FastifyRequest) => {
|
|
||||||
return request?.headers?.Bearer as string;
|
|
||||||
},
|
|
||||||
]),
|
|
||||||
secretOrKey: authConfig.refreshToken.secret,
|
secretOrKey: authConfig.refreshToken.secret,
|
||||||
passReqToCallback: true,
|
passReqToCallback: true,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
async validate(request: RequestWithUser, payload: TokenPayload) {
|
async validate(request: FastifyRequest, payload: TokenPayload) {
|
||||||
const refreshToken = request.headers?.Refresh as string;
|
const refreshToken = (request.headers?.authorization as string| undefined)?.replace('Bearer', '')?.trim();
|
||||||
const account = this.usersAuthService.getUserByIdAndRefreshTokenPair(payload.userId, refreshToken);
|
const account = this.usersAuthService.getUserByIdAndRefreshTokenPair(payload.sub, refreshToken);
|
||||||
if (!account) throw new UnauthorizedException('Refresh Token Guard');
|
if (!account) throw new UnauthorizedException('Refresh Token Guard');
|
||||||
return account;
|
return account;
|
||||||
}
|
}
|
||||||
|
@ -22,9 +22,6 @@ export class AccountsEntity {
|
|||||||
@Column('date', { name: 'data_scadenza', nullable: true })
|
@Column('date', { name: 'data_scadenza', nullable: true })
|
||||||
dataScadenza: Date | null;
|
dataScadenza: Date | null;
|
||||||
|
|
||||||
@Column('bit', { name: 'flag_primo_accesso', default: () => '(1)' })
|
|
||||||
flagPrimoAccesso: boolean;
|
|
||||||
|
|
||||||
@Column('varchar', {
|
@Column('varchar', {
|
||||||
name: 'ultimo_hash_refresh_token',
|
name: 'ultimo_hash_refresh_token',
|
||||||
nullable: true,
|
nullable: true,
|
||||||
|
Reference in New Issue
Block a user