From f0d66234fc1b2a1d9edc60d1498bd60b7ab8a2fb Mon Sep 17 00:00:00 2001 From: Noah Spannbauer Date: Sun, 14 Jul 2024 14:29:23 -0500 Subject: [PATCH] updating getMsGraphAuth with scopes parameter --- libs/modules/src/auth/auth.strategy.ts | 3 +- libs/modules/src/msGraph/ms-graph.service.ts | 35 +++++++++++++++----- package-lock.json | 21 ++++++++---- package.json | 3 +- 4 files changed, 46 insertions(+), 16 deletions(-) diff --git a/libs/modules/src/auth/auth.strategy.ts b/libs/modules/src/auth/auth.strategy.ts index 70537b7..3b842fd 100644 --- a/libs/modules/src/auth/auth.strategy.ts +++ b/libs/modules/src/auth/auth.strategy.ts @@ -11,8 +11,9 @@ export class AzureAdStrategy extends PassportStrategy( ) { constructor(@Inject(AUTH_OPTIONS) authOptions: AuthOptions) { super({ - identityMetadata: `https://login.microsoftonline.com/${authOptions.tenantId}/v2.0/.well-known/openid-configuration`, + identityMetadata: `https://login.microsoftonline.com/${authOptions.tenantId}/.well-known/openid-configuration`, clientID: authOptions.clientId, + audience: `api://${authOptions.clientId}`, loggingLevel: 'info', loggingNoPII: false }) diff --git a/libs/modules/src/msGraph/ms-graph.service.ts b/libs/modules/src/msGraph/ms-graph.service.ts index aacd039..f4b64c5 100644 --- a/libs/modules/src/msGraph/ms-graph.service.ts +++ b/libs/modules/src/msGraph/ms-graph.service.ts @@ -1,20 +1,39 @@ import { Inject, Injectable } from '@nestjs/common'; import { MsGraphOptions } from './ms-graph.interface'; import { MS_GRAPH_OPTIONS } from './ms-graph.constants'; -import { ClientSecretCredential } from '@azure/identity'; import { Client } from '@microsoft/microsoft-graph-client'; -import { TokenCredentialAuthenticationProvider } from '@microsoft/microsoft-graph-client/authProviders/azureTokenCredentials' +import { AuthenticationResult, ConfidentialClientApplication, OnBehalfOfRequest } from '@azure/msal-node'; @Injectable() export class MsGraphService { constructor(@Inject(MS_GRAPH_OPTIONS) private msGraphOptions: MsGraphOptions) {} - async getMsGraphClient(): Promise { - const credential = new ClientSecretCredential(this.msGraphOptions.tenantId, this.msGraphOptions.clientId, this.msGraphOptions.clientSecret); - const authProvider = new TokenCredentialAuthenticationProvider(credential, { scopes: ['.default'] }); - const client = Client.initWithMiddleware({ - debugLogging: true, - authProvider + async getMsGraphAuth(accessToken: string, scopes: string[]): Promise { + try { + const oboRequest: OnBehalfOfRequest = { + oboAssertion: accessToken, + scopes: scopes + } + const cca = new ConfidentialClientApplication({ + auth: { + clientId: this.msGraphOptions.clientId, + clientSecret: this.msGraphOptions.clientSecret, + authority: `https://login.microsoftonline.com/${this.msGraphOptions.tenantId}` + } + }); + const authenticationResult: AuthenticationResult = await cca.acquireTokenOnBehalfOf(oboRequest); + + return authenticationResult.accessToken + } catch (error) { + return error + } + } + + async getMsGraphClientDelegated(accessToken): Promise { + const client = await Client.init({ + authProvider: (done) => { + done(null, accessToken); + } }); return client; diff --git a/package-lock.json b/package-lock.json index f48564a..006e21b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,16 +1,17 @@ { "name": "@noahspan/noahspan-modules", - "version": "0.2.1", + "version": "0.2.9", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@noahspan/noahspan-modules", - "version": "0.2.1", + "version": "0.2.9", "license": "UNLICENSED", "dependencies": { "@azure/app-configuration": "^1.6.0", "@azure/identity": "^4.2.0", + "@azure/msal-node": "^2.9.2", "@microsoft/microsoft-graph-client": "^3.0.7", "@nestjs/common": "^10.0.0", "@nestjs/core": "^10.0.0", @@ -511,11 +512,11 @@ } }, "node_modules/@azure/msal-node": { - "version": "2.9.1", - "resolved": "https://registry.npmjs.org/@azure/msal-node/-/msal-node-2.9.1.tgz", - "integrity": "sha512-I9Pc78mXwj/K8ydSgTfZ5A20vQ/xvfgnnhSCkienZ29b59zFy/hb2Vxmc6Gvg5pNkimSqkPnAtGoBMxYOLBm1A==", + "version": "2.9.2", + "resolved": "https://registry.npmjs.org/@azure/msal-node/-/msal-node-2.9.2.tgz", + "integrity": "sha512-8tvi6Cos3m+0KmRbPjgkySXi+UQU/QiuVRFnrxIwt5xZlEEFa69O04RTaNESGgImyBBlYbo2mfE8/U8Bbdk1WQ==", "dependencies": { - "@azure/msal-common": "14.11.0", + "@azure/msal-common": "14.12.0", "jsonwebtoken": "^9.0.0", "uuid": "^8.3.0" }, @@ -523,6 +524,14 @@ "node": ">=16" } }, + "node_modules/@azure/msal-node/node_modules/@azure/msal-common": { + "version": "14.12.0", + "resolved": "https://registry.npmjs.org/@azure/msal-common/-/msal-common-14.12.0.tgz", + "integrity": "sha512-IDDXmzfdwmDkv4SSmMEyAniJf6fDu3FJ7ncOjlxkDuT85uSnLEhZi3fGZpoR7T4XZpOMx9teM9GXBgrfJgyeBw==", + "engines": { + "node": ">=0.8.0" + } + }, "node_modules/@azure/msal-node/node_modules/uuid": { "version": "8.3.2", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", diff --git a/package.json b/package.json index 06c204c..a83f5d7 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@noahspan/noahspan-modules", - "version": "0.2.7", + "version": "0.3.5", "description": "", "author": "", "license": "UNLICENSED", @@ -26,6 +26,7 @@ "dependencies": { "@azure/app-configuration": "^1.6.0", "@azure/identity": "^4.2.0", + "@azure/msal-node": "^2.9.2", "@microsoft/microsoft-graph-client": "^3.0.7", "@nestjs/common": "^10.0.0", "@nestjs/core": "^10.0.0",