updating getMsGraphAuth with scopes parameter #2

Merged
noahspannbauer merged 1 commits from feature/1-update-getmsgraphauth-with-scopes-parameter into main 2024-07-14 15:30:49 -04:00
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) {
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
})

View File

@@ -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<Client> {
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<string> {
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<Client> {
const client = await Client.init({
authProvider: (done) => {
done(null, accessToken);
}
});
return client;

21
package-lock.json generated
View File

@@ -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",

View File

@@ -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",