@@ -10,51 +10,46 @@ import { JwtStrategyName } from "./constants";
1010
1111@Injectable ( )
1212export class JwtStrategy extends PassportStrategy ( Strategy , JwtStrategyName ) {
13- constructor (
14- private permissionService : PermissionService ,
15- private userService : UserService ,
16- private configService : ConfigService
17- ) {
18- super ( {
19- // Configure the strategy to look for the JWT token in the Authorization header
20- jwtFromRequest : ExtractJwt . fromExtractors ( [
21- ExtractJwt . fromAuthHeaderAsBearerToken ( ) ,
22- ExtractJwt . fromUrlQueryParameter ( "secret_token" ) ,
23- ] ) ,
24- ignoreExpiration : false ,
25- secretOrKey : configService . get < string > ( "jwt.secret" ) ,
26- } ) ;
27- }
28- private readonly logger = new Logger ( JwtStrategy . name ) ;
29-
30- private readonly NAME_ID_FORMAT =
31- "urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName" ;
13+ constructor (
14+ private permissionService : PermissionService ,
15+ private userService : UserService ,
16+ private configService : ConfigService
17+ ) {
18+ super ( {
19+ // Configure the strategy to look for the JWT token in the Authorization header
20+ jwtFromRequest : ExtractJwt . fromExtractors ( [
21+ ExtractJwt . fromAuthHeaderAsBearerToken ( ) ,
22+ ExtractJwt . fromUrlQueryParameter ( "secret_token" ) ,
23+ ] ) ,
24+ ignoreExpiration : false ,
25+ secretOrKey : configService . get < string > ( "jwt.secret" ) ,
26+ } ) ;
27+ }
28+ private readonly logger = new Logger ( JwtStrategy . name ) ;
3229
33- async validate ( payload : JwtPayloadDto ) : Promise < AuthenticatedUser > {
34- // Does the user still exist?
35- const exists = await this . userService . findOne ( payload . sub ) ;
36- if ( ! exists ) {
37- this . logger . warn (
38- `Authorization for user with id: ${ payload . sub } failed, since they no longer exists`
39- ) ;
40- throw new UnauthorizedException ( ) ;
41- }
30+ private readonly NAME_ID_FORMAT = "urn:oasis:names:tc:SAML:1.1:nameid-format:X509SubjectName" ;
4231
43- const result : AuthenticatedUser = {
44- userId : payload . sub ,
45- username : payload . username ,
46- } ;
32+ async validate ( payload : JwtPayloadDto ) : Promise < AuthenticatedUser > {
33+ // Does the user still exist?
34+ const exists = await this . userService . findOne ( payload . sub ) ;
35+ if ( ! exists ) {
36+ this . logger . warn ( `Authorization for user with id: ${ payload . sub } failed, since they no longer exists` ) ;
37+ throw new UnauthorizedException ( ) ;
38+ }
4739
48- if ( exists . nameId ) {
49- // Add SAML stuff
50- result . nameID = exists . nameId ;
51- result . nameIDFormat = this . NAME_ID_FORMAT ;
52- }
53- // This data is already validated
54- result . permissions = await this . permissionService . findPermissionGroupedByLevelForUser (
55- payload . sub
56- ) ;
40+ const result : AuthenticatedUser = {
41+ userId : payload . sub ,
42+ username : payload . username ,
43+ } ;
5744
58- return result ;
45+ if ( exists . nameId ) {
46+ // Add SAML stuff
47+ result . nameID = exists . nameId ;
48+ result . nameIDFormat = this . NAME_ID_FORMAT ;
5949 }
50+ // This data is already validated
51+ result . permissions = await this . permissionService . findPermissionGroupedByLevelForUser ( payload . sub ) ;
52+
53+ return result ;
54+ }
6055}
0 commit comments