Skip to content

Commit a2f1683

Browse files
committed
rename method & add doc comments
1 parent 5f404f0 commit a2f1683

File tree

4 files changed

+46
-10
lines changed

4 files changed

+46
-10
lines changed

src/core/auth/guards/auth.guard.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import { 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+
*/
410
export const authGuard = async (req: Request) => {
5-
if (!(await decode(req.headers.authorization ?? ''))) {
11+
if (!(await decodeAuthToken(req.headers.authorization ?? ''))) {
612
return false;
713
}
814

src/core/auth/guards/guards.utils.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import * as jwt from 'jsonwebtoken';
2-
// import { UnauthorizedException } from '@nestjs/common';
32
import { Logger } from 'src/shared/global';
43
import { getSigningKey } from '../jwt';
54

65
const 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;

src/core/auth/guards/m2m-scope.guard.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
import { Request } from 'express';
2-
import { decode } from './guards.utils';
2+
import { decodeAuthToken } from './guards.utils';
33
import { JwtPayload } from 'jsonwebtoken';
44
import { 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+
*/
617
export 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
' ',

src/core/auth/guards/role.guard.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
import { Request } from 'express';
2-
import { decode } from './guards.utils';
2+
import { decodeAuthToken } from './guards.utils';
33
import { 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+
*/
516
export 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(/claims\/roles$/gi)) {

0 commit comments

Comments
 (0)