updating getMsGraphAuth with scopes parameter

This commit is contained in:
2024-07-14 14:29:23 -05:00
parent db0e8bfe5d
commit f0d66234fc
4 changed files with 46 additions and 16 deletions

View File

@@ -11,8 +11,9 @@ export class AzureAdStrategy extends PassportStrategy(
) { ) {
constructor(@Inject(AUTH_OPTIONS) authOptions: AuthOptions) { constructor(@Inject(AUTH_OPTIONS) authOptions: AuthOptions) {
super({ 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, clientID: authOptions.clientId,
audience: `api://${authOptions.clientId}`,
loggingLevel: 'info', loggingLevel: 'info',
loggingNoPII: false loggingNoPII: false
}) })

View File

@@ -1,20 +1,39 @@
import { Inject, Injectable } from '@nestjs/common'; import { Inject, Injectable } from '@nestjs/common';
import { MsGraphOptions } from './ms-graph.interface'; import { MsGraphOptions } from './ms-graph.interface';
import { MS_GRAPH_OPTIONS } from './ms-graph.constants'; import { MS_GRAPH_OPTIONS } from './ms-graph.constants';
import { ClientSecretCredential } from '@azure/identity';
import { Client } from '@microsoft/microsoft-graph-client'; 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() @Injectable()
export class MsGraphService { export class MsGraphService {
constructor(@Inject(MS_GRAPH_OPTIONS) private msGraphOptions: MsGraphOptions) {} constructor(@Inject(MS_GRAPH_OPTIONS) private msGraphOptions: MsGraphOptions) {}
async getMsGraphClient(): Promise<Client> { async getMsGraphAuth(accessToken: string, scopes: string[]): Promise<string> {
const credential = new ClientSecretCredential(this.msGraphOptions.tenantId, this.msGraphOptions.clientId, this.msGraphOptions.clientSecret); try {
const authProvider = new TokenCredentialAuthenticationProvider(credential, { scopes: ['.default'] }); const oboRequest: OnBehalfOfRequest = {
const client = Client.initWithMiddleware({ oboAssertion: accessToken,
debugLogging: true, scopes: scopes
authProvider }
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<Client> {
const client = await Client.init({
authProvider: (done) => {
done(null, accessToken);
}
}); });
return client; return client;

21
package-lock.json generated
View File

@@ -1,16 +1,17 @@
{ {
"name": "@noahspan/noahspan-modules", "name": "@noahspan/noahspan-modules",
"version": "0.2.1", "version": "0.2.9",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@noahspan/noahspan-modules", "name": "@noahspan/noahspan-modules",
"version": "0.2.1", "version": "0.2.9",
"license": "UNLICENSED", "license": "UNLICENSED",
"dependencies": { "dependencies": {
"@azure/app-configuration": "^1.6.0", "@azure/app-configuration": "^1.6.0",
"@azure/identity": "^4.2.0", "@azure/identity": "^4.2.0",
"@azure/msal-node": "^2.9.2",
"@microsoft/microsoft-graph-client": "^3.0.7", "@microsoft/microsoft-graph-client": "^3.0.7",
"@nestjs/common": "^10.0.0", "@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0", "@nestjs/core": "^10.0.0",
@@ -511,11 +512,11 @@
} }
}, },
"node_modules/@azure/msal-node": { "node_modules/@azure/msal-node": {
"version": "2.9.1", "version": "2.9.2",
"resolved": "https://registry.npmjs.org/@azure/msal-node/-/msal-node-2.9.1.tgz", "resolved": "https://registry.npmjs.org/@azure/msal-node/-/msal-node-2.9.2.tgz",
"integrity": "sha512-I9Pc78mXwj/K8ydSgTfZ5A20vQ/xvfgnnhSCkienZ29b59zFy/hb2Vxmc6Gvg5pNkimSqkPnAtGoBMxYOLBm1A==", "integrity": "sha512-8tvi6Cos3m+0KmRbPjgkySXi+UQU/QiuVRFnrxIwt5xZlEEFa69O04RTaNESGgImyBBlYbo2mfE8/U8Bbdk1WQ==",
"dependencies": { "dependencies": {
"@azure/msal-common": "14.11.0", "@azure/msal-common": "14.12.0",
"jsonwebtoken": "^9.0.0", "jsonwebtoken": "^9.0.0",
"uuid": "^8.3.0" "uuid": "^8.3.0"
}, },
@@ -523,6 +524,14 @@
"node": ">=16" "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": { "node_modules/@azure/msal-node/node_modules/uuid": {
"version": "8.3.2", "version": "8.3.2",
"resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz", "resolved": "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz",

View File

@@ -1,6 +1,6 @@
{ {
"name": "@noahspan/noahspan-modules", "name": "@noahspan/noahspan-modules",
"version": "0.2.7", "version": "0.3.5",
"description": "", "description": "",
"author": "", "author": "",
"license": "UNLICENSED", "license": "UNLICENSED",
@@ -26,6 +26,7 @@
"dependencies": { "dependencies": {
"@azure/app-configuration": "^1.6.0", "@azure/app-configuration": "^1.6.0",
"@azure/identity": "^4.2.0", "@azure/identity": "^4.2.0",
"@azure/msal-node": "^2.9.2",
"@microsoft/microsoft-graph-client": "^3.0.7", "@microsoft/microsoft-graph-client": "^3.0.7",
"@nestjs/common": "^10.0.0", "@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0", "@nestjs/core": "^10.0.0",