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 1
1
import { Request } from 'express' ;
2
- import { decode } from './guards.utils' ;
2
+ import { decodeAuthToken } from './guards.utils' ;
3
3
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
+ */
4
10
export const authGuard = async ( req : Request ) => {
5
- if ( ! ( await decode ( req . headers . authorization ?? '' ) ) ) {
11
+ if ( ! ( await decodeAuthToken ( req . headers . authorization ?? '' ) ) ) {
6
12
return false ;
7
13
}
8
14
Original file line number Diff line number Diff line change 1
1
import * as jwt from 'jsonwebtoken' ;
2
- // import { UnauthorizedException } from '@nestjs/common';
3
2
import { Logger } from 'src/shared/global' ;
4
3
import { getSigningKey } from '../jwt' ;
5
4
6
5
const logger = new Logger ( 'guards.utils()' ) ;
7
6
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 > => {
9
19
const [ type , idToken ] = authHeader ?. split ( ' ' ) ?? [ ] ;
10
20
11
21
if ( type !== 'Bearer' || ! idToken ) {
12
22
return false ;
13
- // throw new UnauthorizedException('Missing Authorization header!');
14
23
}
15
24
16
25
let decoded : jwt . JwtPayload | string ;
@@ -20,7 +29,6 @@ export const decode = async (authHeader: string) => {
20
29
} catch ( error ) {
21
30
logger . error ( 'Error verifying JWT' , error ) ;
22
31
return false ;
23
- // throw new UnauthorizedException('Invalid or expired JWT!');
24
32
}
25
33
26
34
return decoded ;
Original file line number Diff line number Diff line change 1
1
import { Request } from 'express' ;
2
- import { decode } from './guards.utils' ;
2
+ import { decodeAuthToken } from './guards.utils' ;
3
3
import { JwtPayload } from 'jsonwebtoken' ;
4
4
import { M2mScope } from '../auth.constants' ;
5
5
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
+ */
6
17
export const checkM2MScope =
7
18
( ...requiredM2mScopes : M2mScope [ ] ) =>
8
19
async ( req : Request ) => {
9
- const decodedAuth = await decode ( req . headers . authorization ?? '' ) ;
20
+ const decodedAuth = await decodeAuthToken ( req . headers . authorization ?? '' ) ;
10
21
11
22
const authorizedScopes = ( ( decodedAuth as JwtPayload ) . scope ?? '' ) . split (
12
23
' ' ,
Original file line number Diff line number Diff line change 1
1
import { Request } from 'express' ;
2
- import { decode } from './guards.utils' ;
2
+ import { decodeAuthToken } from './guards.utils' ;
3
3
import { Role } from '../auth.constants' ;
4
4
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
+ */
5
16
export const checkHasUserRole =
6
17
( ...requiredUserRoles : Role [ ] ) =>
7
18
async ( req : Request ) => {
8
- const decodedAuth = await decode ( req . headers . authorization ?? '' ) ;
19
+ const decodedAuth = await decodeAuthToken ( req . headers . authorization ?? '' ) ;
9
20
10
21
const decodedUserRoles = Object . keys ( decodedAuth ) . reduce ( ( roles , key ) => {
11
22
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