File tree Expand file tree Collapse file tree 4 files changed +46
-10
lines changed Expand file tree Collapse file tree 4 files changed +46
-10
lines changed Original file line number Diff line number Diff line change 11import { Request } from 'express' ;
2- import { decode } from './guards.utils' ;
2+ import { decodeAuthToken } from './guards.utils' ;
33
4+ /**
5+ * Auth guard function to validate the authorization token from the request headers.
6+ *
7+ * @param req - The incoming HTTP request object.
8+ * @returns A promise that resolves to `true` if the authorization token is valid, otherwise `false`.
9+ */
410export const authGuard = async ( req : Request ) => {
5- if ( ! ( await decode ( req . headers . authorization ?? '' ) ) ) {
11+ if ( ! ( await decodeAuthToken ( req . headers . authorization ?? '' ) ) ) {
612 return false ;
713 }
814
Original file line number Diff line number Diff line change 11import * as jwt from 'jsonwebtoken' ;
2- // import { UnauthorizedException } from '@nestjs/common';
32import { Logger } from 'src/shared/global' ;
43import { getSigningKey } from '../jwt' ;
54
65const logger = new Logger ( 'guards.utils()' ) ;
76
8- export const decode = async ( authHeader : string ) => {
7+ /**
8+ * Decodes and verifies a JWT token from the provided authorization header.
9+ *
10+ * @param authHeader - The authorization header containing the token, expected in the format "Bearer <token>".
11+ * @returns A promise that resolves to the decoded JWT payload if the token is valid,
12+ * a string if the payload is a string, or `false` if the token is invalid or the header is improperly formatted.
13+ *
14+ * @throws This function does not throw directly but will return `false` if an error occurs during verification.
15+ */
16+ export const decodeAuthToken = async (
17+ authHeader : string ,
18+ ) : Promise < boolean | jwt . JwtPayload | string > => {
919 const [ type , idToken ] = authHeader ?. split ( ' ' ) ?? [ ] ;
1020
1121 if ( type !== 'Bearer' || ! idToken ) {
1222 return false ;
13- // throw new UnauthorizedException('Missing Authorization header!');
1423 }
1524
1625 let decoded : jwt . JwtPayload | string ;
@@ -20,7 +29,6 @@ export const decode = async (authHeader: string) => {
2029 } catch ( error ) {
2130 logger . error ( 'Error verifying JWT' , error ) ;
2231 return false ;
23- // throw new UnauthorizedException('Invalid or expired JWT!');
2432 }
2533
2634 return decoded ;
Original file line number Diff line number Diff line change 11import { Request } from 'express' ;
2- import { decode } from './guards.utils' ;
2+ import { decodeAuthToken } from './guards.utils' ;
33import { JwtPayload } from 'jsonwebtoken' ;
44import { M2mScope } from '../auth.constants' ;
55
6+ /**
7+ * A utility function to check if the required M2M (Machine-to-Machine) scopes are present
8+ * in the authorization token provided in the request headers.
9+ *
10+ * @param {...M2mScope[] } requiredM2mScopes - The list of required M2M scopes to validate against.
11+ * @returns {Promise<(req: Request) => boolean> } A function that takes an Express `Request` object
12+ * and returns a boolean indicating whether the required scopes are present.
13+ *
14+ * The function decodes the authorization token from the request headers and checks if
15+ * the required scopes are included in the token's scope claim.
16+ */
617export const checkM2MScope =
718 ( ...requiredM2mScopes : M2mScope [ ] ) =>
819 async ( req : Request ) => {
9- const decodedAuth = await decode ( req . headers . authorization ?? '' ) ;
20+ const decodedAuth = await decodeAuthToken ( req . headers . authorization ?? '' ) ;
1021
1122 const authorizedScopes = ( ( decodedAuth as JwtPayload ) . scope ?? '' ) . split (
1223 ' ' ,
Original file line number Diff line number Diff line change 11import { Request } from 'express' ;
2- import { decode } from './guards.utils' ;
2+ import { decodeAuthToken } from './guards.utils' ;
33import { Role } from '../auth.constants' ;
44
5+ /**
6+ * A utility function to check if the required user role are present
7+ * in the authorization token provided in the request headers.
8+ *
9+ * @param {...Role[] } requiredUserRoles - The list of required user roles to validate against.
10+ * @returns {Promise<(req: Request) => boolean> } A function that takes an Express `Request` object
11+ * and returns a boolean indicating whether the required scopes are present.
12+ *
13+ * The function decodes the authorization token from the request headers and checks if
14+ * the required user roles are included in the token's scope claim.
15+ */
516export const checkHasUserRole =
617 ( ...requiredUserRoles : Role [ ] ) =>
718 async ( req : Request ) => {
8- const decodedAuth = await decode ( req . headers . authorization ?? '' ) ;
19+ const decodedAuth = await decodeAuthToken ( req . headers . authorization ?? '' ) ;
920
1021 const decodedUserRoles = Object . keys ( decodedAuth ) . reduce ( ( roles , key ) => {
1122 if ( key . match ( / c l a i m s \/ r o l e s $ / gi) ) {
You can’t perform that action at this time.
0 commit comments