Adiciona primeiros Guards de autenticacao

This commit is contained in:
LeoMortari
2025-09-11 18:46:59 -03:00
parent ee1e3bd7f8
commit 13b41d2f52
7 changed files with 83 additions and 18 deletions

View File

@@ -0,0 +1,54 @@
import { Injectable } from '@nestjs/common';
import { PassportStrategy } from '@nestjs/passport';
import { ExtractJwt, Strategy } from 'passport-jwt';
import * as jwksRsa from 'jwks-rsa';
export type JwtAudience = string | string[] | undefined;
export interface JwtRealmAccess {
roles: string[];
}
export interface JwtResourceAccessEntry {
roles: string[];
}
export type JwtResourceAccess =
| Record<string, JwtResourceAccessEntry>
| undefined;
export interface JwtPayload {
sub: string;
email?: string;
preferred_username?: string;
given_name?: string;
family_name?: string;
scope?: string;
realm_access?: JwtRealmAccess;
resource_access?: JwtResourceAccess;
iat: number;
exp: number;
iss: string;
aud?: JwtAudience;
[claim: string]: unknown;
}
@Injectable()
export class KeycloakJwtStrategy extends PassportStrategy(Strategy, 'jwt') {
constructor() {
super({
jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(),
secretOrKeyProvider: jwksRsa.passportJwtSecret({
cache: true,
rateLimit: true,
jwksRequestsPerMinute: 5,
jwksUri:
'https://auth.clipperia.com.br/realms/clipperia/protocol/openid-connect/certs',
}),
algorithms: ['RS256'],
audience: 'account',
issuer: 'https://auth.clipperia.com.br/realms/clipperia',
ignoreExpiration: false,
});
}
validate(payload: JwtPayload): JwtPayload {
return payload;
}
}