From 30b500bd7f4afa4061c6b11d996473e3cc31a2dd Mon Sep 17 00:00:00 2001 From: Noah Spannbauer Date: Sat, 1 Feb 2025 11:07:55 -0600 Subject: [PATCH] refactoring --- libs/modules/src/auth/auth.gaurd.ts | 3 ++- libs/modules/src/auth/auth.strategy.ts | 29 ++++++++++++++++++++-- libs/modules/src/auth/public.decorator.ts | 3 ++- package-lock.json | 30 +++++++++++++++-------- package.json | 4 ++- 5 files changed, 54 insertions(+), 15 deletions(-) diff --git a/libs/modules/src/auth/auth.gaurd.ts b/libs/modules/src/auth/auth.gaurd.ts index 5313256..c974a1a 100644 --- a/libs/modules/src/auth/auth.gaurd.ts +++ b/libs/modules/src/auth/auth.gaurd.ts @@ -3,6 +3,7 @@ import { ExecutionContext, Injectable } from '@nestjs/common'; import { Reflector } from '@nestjs/core'; import { AuthGuard as PassportAuthGuard } from '@nestjs/passport'; import { Observable } from 'rxjs'; +import { IS_PUBLIC_KEY } from './public.decorator'; @Injectable() export class AuthGuard extends PassportAuthGuard('azure-ad') implements CanActivate { @@ -12,7 +13,7 @@ export class AuthGuard extends PassportAuthGuard('azure-ad') implements CanActiv canActivate(context: ExecutionContext): boolean | Promise | Observable { const isPublic = this.reflector.get( - 'isPublic', + IS_PUBLIC_KEY, context.getHandler() ); const req = context.switchToHttp().getRequest(); diff --git a/libs/modules/src/auth/auth.strategy.ts b/libs/modules/src/auth/auth.strategy.ts index bfadd18..b9d2930 100644 --- a/libs/modules/src/auth/auth.strategy.ts +++ b/libs/modules/src/auth/auth.strategy.ts @@ -1,8 +1,10 @@ import { Inject, Injectable } from "@nestjs/common"; import { PassportStrategy } from "@nestjs/passport"; -import { BearerStrategy } from 'passport-azure-ad'; import { AuthModuleOptions } from './auth.interface' import { MODULE_OPTIONS_TOKEN } from "./auth.module-definition"; +import { BearerStrategy } from 'passport-azure-ad' +import { ExtractJwt, Strategy } from 'passport-jwt'; +import * as jwksRsa from 'jwks-rsa'; @Injectable() export class AzureAdStrategy extends PassportStrategy( @@ -23,4 +25,27 @@ export class AzureAdStrategy extends PassportStrategy( async validate(data: any): Promise { return data; } -} \ No newline at end of file +} + +// @Injectable() +// export class AzureAdStrategy extends PassportStrategy(Strategy, 'azure-ad') { +// constructor(@Inject(MODULE_OPTIONS_TOKEN) authModuleOptions: AuthModuleOptions) { +// super({ +// jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken(), +// audience: `api://${authModuleOptions.clientId}`, +// issuer: `https://sts.windows.net/${authModuleOptions.tenantId}`, +// algorithms: ['RS256'], +// ignoreExpiration: true, +// secretOrKeyProvider: jwksRsa.passportJwtSecret({ +// cache: true, +// rateLimit: true, +// jwksRequestsPerMinute: 5, +// jwksUri: `https://login.microsoftonline.com/${authModuleOptions.tenantId}` +// }) +// }) +// } + +// validate(payload: any) { +// return payload; +// } +// } \ No newline at end of file diff --git a/libs/modules/src/auth/public.decorator.ts b/libs/modules/src/auth/public.decorator.ts index 5da0443..96e4125 100644 --- a/libs/modules/src/auth/public.decorator.ts +++ b/libs/modules/src/auth/public.decorator.ts @@ -1,3 +1,4 @@ import { SetMetadata } from "@nestjs/common"; -export const Public = () => SetMetadata('isPublic', true); \ No newline at end of file +export const IS_PUBLIC_KEY = 'isPublic' +export const Public = () => SetMetadata(IS_PUBLIC_KEY, true); diff --git a/package-lock.json b/package-lock.json index 8d187af..04eaa85 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@noahspan/noahspan-modules", - "version": "0.5.2", + "version": "0.6.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@noahspan/noahspan-modules", - "version": "0.5.2", + "version": "0.6.1", "license": "UNLICENSED", "dependencies": { "@azure/app-configuration": "^1.6.0", @@ -22,7 +22,9 @@ "axios": "^1.7.2", "express-jwt": "^8.5.1", "jwks-rsa": "^3.1.0", + "passport": "^0.7.0", "passport-azure-ad": "^4.3.5", + "passport-jwt": "^4.0.1", "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1" }, @@ -3373,9 +3375,9 @@ "dev": true }, "node_modules/async": { - "version": "3.2.5", - "resolved": "https://registry.npmjs.org/async/-/async-3.2.5.tgz", - "integrity": "sha512-baNZyqaaLhyLVKm/DlvdW051MSgO6b8eVfIezl9E5PqWxFgzLm/wQntEW4zOytVburDEr0JlALEpdOFwvErLsg==" + "version": "3.2.6", + "resolved": "https://registry.npmjs.org/async/-/async-3.2.6.tgz", + "integrity": "sha512-htCUDlxyyCLMgaM3xXg0C0LW2xqfuQ6p05pCEIsXuyQ+a1koYKTuBMzRNwmybfLgvJDMd0r1LTn4+E0Ti6C2AA==" }, "node_modules/asynckit": { "version": "0.4.0", @@ -7410,9 +7412,9 @@ } }, "node_modules/nan": { - "version": "2.19.0", - "resolved": "https://registry.npmjs.org/nan/-/nan-2.19.0.tgz", - "integrity": "sha512-nO1xXxfh/RWNxfd/XPfbIfFk5vgLsAxUR9y5O0cHMJu/AW9U95JLXqthYHjEp+8gQ5p96K9jUp8nbVOxCdRbtw==", + "version": "2.22.0", + "resolved": "https://registry.npmjs.org/nan/-/nan-2.22.0.tgz", + "integrity": "sha512-nbajikzWTMwsW+eSsNm3QwlOs7het9gGJU5dDZzRTQGk03vyBOauxgI4VakDzE0PtsGTmXPsXTbbjVhRwR5mpw==", "optional": true }, "node_modules/natural-compare": { @@ -7798,7 +7800,6 @@ "version": "0.7.0", "resolved": "https://registry.npmjs.org/passport/-/passport-0.7.0.tgz", "integrity": "sha512-cPLl+qZpSc+ireUvt+IzqbED1cHHkDoVYMo30jbJIdOOjQ1MQYZBPiNvmi8UM6lJuOpTPXJGZQk0DtC4y61MYQ==", - "peer": true, "dependencies": { "passport-strategy": "1.x.x", "pause": "0.0.1", @@ -7816,7 +7817,7 @@ "version": "4.3.5", "resolved": "https://registry.npmjs.org/passport-azure-ad/-/passport-azure-ad-4.3.5.tgz", "integrity": "sha512-LBpXEght7hCMuMNFK4oegdN0uPBa3lpDMy71zQoB0zPg1RrGwdzpjwTiN1WzN0hY77fLyjz9tBr3TGAxnSgtEg==", - "deprecated": "Package no longer supported. Contact Support at https://www.npmjs.com/support for more info.", + "deprecated": "This package is deprecated and no longer supported. For more please visit https://github.com/AzureAD/passport-azure-ad?tab=readme-ov-file#node-js-validation-replacement-for-passportjs", "dependencies": { "async": "^3.2.3", "base64url": "^3.0.0", @@ -7874,6 +7875,15 @@ "url": "https://github.com/sponsors/jaredhanson" } }, + "node_modules/passport-jwt": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/passport-jwt/-/passport-jwt-4.0.1.tgz", + "integrity": "sha512-UCKMDYhNuGOBE9/9Ycuoyh7vP6jpeTp/+sfMJl7nLff/t6dps+iaeE0hhNkKN8/HZHcJ7lCdOyDxHdDoxoSvdQ==", + "dependencies": { + "jsonwebtoken": "^9.0.0", + "passport-strategy": "^1.0.0" + } + }, "node_modules/passport-strategy": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/passport-strategy/-/passport-strategy-1.0.0.tgz", diff --git a/package.json b/package.json index daa4435..3beffe2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@noahspan/noahspan-modules", - "version": "0.5.3", + "version": "0.6.2", "description": "", "author": "", "license": "UNLICENSED", @@ -37,7 +37,9 @@ "axios": "^1.7.2", "express-jwt": "^8.5.1", "jwks-rsa": "^3.1.0", + "passport": "^0.7.0", "passport-azure-ad": "^4.3.5", + "passport-jwt": "^4.0.1", "reflect-metadata": "^0.2.0", "rxjs": "^7.8.1" },