Spaces:
Runtime error
Runtime error
File size: 2,553 Bytes
97dab2a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import { Injectable } from '@nestjs/common';
import { ConfidentialClientApplication, Configuration } from '@azure/msal-node';
import { ManagedIdentityCredential } from '@azure/identity';
@Injectable()
export class AuthService {
private msalClient: ConfidentialClientApplication;
private msalConfig: Configuration;
constructor() {
this.initialize();
}
private async initialize() {
if (process.env.AZURE_CLIENT_SECRET && process.env.AZURE_CLIENT_SECRET.length > 0) {
this.msalConfig = {
auth: {
clientId: process.env.AZURE_CLIENT_ID || (() => { throw new Error('AZURE_CLIENT_ID is not defined'); })(),
authority: `https://login.microsoftonline.com/${process.env.AZURE_TENANT_ID}`,
clientSecret: process.env.AZURE_CLIENT_SECRET,
},
system: {
loggerOptions: {
loggerCallback(loglevel, message) {
console.log(message);
},
piiLoggingEnabled: false,
logLevel: 2,
},
},
};
} else {
const managedIdentityCredential = new ManagedIdentityCredential(
process.env.MANAGED_IDENTITY_CLIENT_ID ? { clientId: process.env.MANAGED_IDENTITY_CLIENT_ID } : undefined
);
const tokenResponse = await managedIdentityCredential.getToken(["api://AzureADTokenExchange"]);
if (tokenResponse && tokenResponse.token) {
console.log("Authorization Step0: Token Issued by Managed Identity: " + tokenResponse.token);
}
this.msalConfig = {
auth: {
clientId: process.env.AZURE_CLIENT_ID || (() => { throw new Error('AZURE_CLIENT_ID is not defined'); })(),
authority: `https://login.microsoftonline.com/${process.env.AZURE_TENANT_ID}`,
clientAssertion: tokenResponse.token,
},
system: {
loggerOptions: {
loggerCallback(loglevel, message) {
console.log(message);
},
piiLoggingEnabled: false,
logLevel: 2,
},
},
};
}
this.msalClient = new ConfidentialClientApplication(this.msalConfig);
}
getClient() {
return this.msalClient;
}
}
|