@@ -4,13 +4,13 @@ import {
44 ExecutionContext ,
55 UnauthorizedException ,
66 ForbiddenException ,
7- } from ' @nestjs/common' ;
8- import { Request } from ' express' ;
9- import { Reflector } from ' @nestjs/core' ;
10- import { ConfigService } from ' @nestjs/config' ;
11- import * as jwt from ' jsonwebtoken' ;
12- import { JwksClient } from ' jwks-rsa' ;
13- import { IS_PUBLIC_KEY } from ' ./public.decorator' ;
7+ } from " @nestjs/common" ;
8+ import { Request } from " express" ;
9+ import { Reflector } from " @nestjs/core" ;
10+ import { ConfigService } from " @nestjs/config" ;
11+ import * as jwt from " jsonwebtoken" ;
12+ import { JwksClient } from " jwks-rsa" ;
13+ import { IS_PUBLIC_KEY } from " ./public.decorator" ;
1414
1515interface User {
1616 idir_username ?: string ;
@@ -20,7 +20,7 @@ interface User {
2020 [ key : string ] : unknown ; // Allow additional properties from JWT
2121}
2222
23- declare module ' express' {
23+ declare module " express" {
2424 interface Request {
2525 user ?: User ;
2626 }
@@ -34,16 +34,20 @@ export class BCGovAuthGuard implements CanActivate {
3434 private configService : ConfigService ,
3535 private reflector : Reflector ,
3636 ) {
37- const ssoAuthServerUrl = this . configService . get < string > ( 'SSO_AUTH_SERVER_URL' ) ;
37+ const ssoAuthServerUrl = this . configService . get < string > (
38+ "SSO_AUTH_SERVER_URL" ,
39+ ) ;
3840
3941 // If SSO_AUTH_SERVER_URL includes the full OIDC path, extract the base realm URL
4042 let jwksUri : string ;
41- if ( ssoAuthServerUrl . includes ( ' /protocol/openid-connect' ) ) {
43+ if ( ssoAuthServerUrl . includes ( " /protocol/openid-connect" ) ) {
4244 // SSO_AUTH_SERVER_URL is the full OIDC endpoint
43- jwksUri = ssoAuthServerUrl . replace ( '/protocol/openid-connect' , '' ) + '/protocol/openid-connect/certs' ;
45+ jwksUri =
46+ ssoAuthServerUrl . replace ( "/protocol/openid-connect" , "" ) +
47+ "/protocol/openid-connect/certs" ;
4448 } else {
4549 // SSO_AUTH_SERVER_URL is the base Keycloak URL
46- const realm = this . configService . get < string > ( ' SSO_REALM' ) ;
50+ const realm = this . configService . get < string > ( " SSO_REALM" ) ;
4751 jwksUri = `${ ssoAuthServerUrl } /realms/${ realm } /protocol/openid-connect/certs` ;
4852 }
4953
@@ -66,10 +70,10 @@ export class BCGovAuthGuard implements CanActivate {
6670 }
6771
6872 const request = context . switchToHttp ( ) . getRequest < Request > ( ) ;
69- const authHeader = request . headers [ ' authorization' ] ;
73+ const authHeader = request . headers [ " authorization" ] ;
7074
71- if ( ! authHeader || ! authHeader . startsWith ( ' Bearer ' ) ) {
72- throw new UnauthorizedException ( ' No Bearer token provided' ) ;
75+ if ( ! authHeader || ! authHeader . startsWith ( " Bearer " ) ) {
76+ throw new UnauthorizedException ( " No Bearer token provided" ) ;
7377 }
7478
7579 const token = authHeader . substring ( 7 ) ;
@@ -80,7 +84,7 @@ export class BCGovAuthGuard implements CanActivate {
8084 request . user = user ;
8185 return true ;
8286 } catch {
83- throw new ForbiddenException ( ' Invalid token' ) ;
87+ throw new ForbiddenException ( " Invalid token" ) ;
8488 }
8589 }
8690
@@ -89,34 +93,39 @@ export class BCGovAuthGuard implements CanActivate {
8993 // Decode token header to get key ID
9094 const decoded = jwt . decode ( token , { complete : true } ) ;
9195 if ( ! decoded || ! decoded . header . kid ) {
92- throw new UnauthorizedException ( ' Invalid token format' ) ;
96+ throw new UnauthorizedException ( " Invalid token format" ) ;
9397 }
9498
9599 // Get signing key
96100 const key = await this . jwksClient . getSigningKey ( decoded . header . kid ) ;
97101 const signingKey = key . getPublicKey ( ) ;
98102
99103 // Determine the correct issuer
100- const ssoAuthServerUrl = this . configService . get < string > ( 'SSO_AUTH_SERVER_URL' ) ;
104+ const ssoAuthServerUrl = this . configService . get < string > (
105+ "SSO_AUTH_SERVER_URL" ,
106+ ) ;
101107 let expectedIssuer : string ;
102- if ( ssoAuthServerUrl . includes ( ' /protocol/openid-connect' ) ) {
108+ if ( ssoAuthServerUrl . includes ( " /protocol/openid-connect" ) ) {
103109 // SSO_AUTH_SERVER_URL is the full OIDC endpoint, issuer is the realm URL
104- expectedIssuer = ssoAuthServerUrl . replace ( '/protocol/openid-connect' , '' ) ;
110+ expectedIssuer = ssoAuthServerUrl . replace (
111+ "/protocol/openid-connect" ,
112+ "" ,
113+ ) ;
105114 } else {
106115 // SSO_AUTH_SERVER_URL is the base Keycloak URL
107- const realm = this . configService . get < string > ( ' SSO_REALM' ) ;
116+ const realm = this . configService . get < string > ( " SSO_REALM" ) ;
108117 expectedIssuer = `${ ssoAuthServerUrl } /realms/${ realm } ` ;
109118 }
110119
111120 // Verify and decode token
112121 const verified = jwt . verify ( token , signingKey , {
113- algorithms : [ ' RS256' ] ,
122+ algorithms : [ " RS256" ] ,
114123 issuer : expectedIssuer ,
115124 } ) ;
116125
117126 return verified as User ;
118127 } catch {
119- throw new UnauthorizedException ( ' Token validation failed' ) ;
128+ throw new UnauthorizedException ( " Token validation failed" ) ;
120129 }
121130 }
122131}
0 commit comments