@kwiz/node
Version:
KWIZ utilities and helpers for node applications
55 lines • 2.54 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.GetMSALToken = void 0;
const msal_node_1 = require("@azure/msal-node");
const common_1 = require("@kwiz/common");
//find tenant id? https://login.microsoftonline.com/kwizcom.onmicrosoft.com/.well-known/openid-configuration
//https://stackoverflow.com/questions/54771270/msal-ad-token-not-valid-with-sharepoint-online-csom
var apps = {};
function GetApp(tenantInfo, auth) {
let key = `${tenantInfo.idOrName}|${auth.authenticationMode}`;
if (!apps[key]) {
auth.authenticationMode === common_1.AuthenticationModes.clientSecret
? apps[key] = new msal_node_1.ConfidentialClientApplication({
auth: {
clientId: auth.clientId,
authority: tenantInfo.authorityUrl,
clientSecret: auth.clientSecret
},
})
: apps[key] = new msal_node_1.ConfidentialClientApplication({
auth: {
clientId: auth.clientId,
authority: tenantInfo.authorityUrl,
clientCertificate: {
thumbprint: auth.thumbprint,
privateKey: auth.privateKey
}
},
});
}
return apps[key];
}
/** client secret not supported by SharePoint, must use certificate */
function GetMSALToken(tenantInfo, scope, auth, clearCache) {
return __awaiter(this, void 0, void 0, function* () {
const app = GetApp(tenantInfo, auth);
if (clearCache)
app.clearCache();
let token = yield app.acquireTokenByClientCredential({
scopes: [`${scope}/.default`]
});
return token.accessToken;
});
}
exports.GetMSALToken = GetMSALToken;
//# sourceMappingURL=msal.js.map