Refactor createPca function to dynamically import msal-node-extensions and improve cache plugin fallback handling
This commit is contained in:
@@ -4,12 +4,7 @@ import fs from "node:fs";
|
||||
import path from "node:path";
|
||||
|
||||
import { PublicClientApplication } from "@azure/msal-node";
|
||||
import {
|
||||
DataProtectionScope,
|
||||
Environment,
|
||||
PersistenceCachePlugin,
|
||||
PersistenceCreator,
|
||||
} from "@azure/msal-node-extensions";
|
||||
import os from "node:os";
|
||||
|
||||
function fileCachePlugin(cachePath) {
|
||||
return {
|
||||
@@ -28,13 +23,23 @@ function fileCachePlugin(cachePath) {
|
||||
}
|
||||
|
||||
async function createPca({ tenantId, clientId }) {
|
||||
const cacheRoot = Environment.isWindowsPlatform()
|
||||
? path.join(Environment.getUserRootDirectory(), "sk-az-tools")
|
||||
: path.join(Environment.getUserRootDirectory(), ".config", "sk-az-tools");
|
||||
const isWindows = process.platform === "win32";
|
||||
const userRoot = isWindows
|
||||
? process.env.LOCALAPPDATA || os.homedir()
|
||||
: os.homedir();
|
||||
const cacheRoot = isWindows
|
||||
? path.join(userRoot, "sk-az-tools")
|
||||
: path.join(userRoot, ".config", "sk-az-tools");
|
||||
|
||||
const cachePath = path.join(cacheRoot, `${clientId}-msal.cache`);
|
||||
let cachePlugin;
|
||||
try {
|
||||
const {
|
||||
DataProtectionScope,
|
||||
PersistenceCachePlugin,
|
||||
PersistenceCreator,
|
||||
} = await import("@azure/msal-node-extensions");
|
||||
|
||||
const persistence = await PersistenceCreator.createPersistence({
|
||||
cachePath,
|
||||
dataProtectionScope: DataProtectionScope.CurrentUser,
|
||||
@@ -44,7 +49,7 @@ async function createPca({ tenantId, clientId }) {
|
||||
});
|
||||
cachePlugin = new PersistenceCachePlugin(persistence);
|
||||
} catch (err) {
|
||||
// Fallback for Linux environments where libsecret integration is unavailable.
|
||||
// Fallback when msal-node-extensions/keytar/libsecret are unavailable.
|
||||
cachePlugin = fileCachePlugin(cachePath);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user