Initial commit

This commit is contained in:
2025-03-07 19:22:02 +01:00
commit 4a98255d83
55743 changed files with 5280367 additions and 0 deletions
@@ -0,0 +1,139 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { Logger, LogLevel } from "@azure/msal-common/browser";
import {
BrowserConfiguration,
buildConfiguration,
Configuration,
} from "../config/Configuration.js";
import { version, name } from "../packageMetadata.js";
import {
BrowserCacheLocation,
LOG_LEVEL_CACHE_KEY,
LOG_PII_CACHE_KEY,
} from "../utils/BrowserConstants.js";
/**
* Base class for operating context
* Operating contexts are contexts in which MSAL.js is being run
* More than one operating context may be available at a time
* It's important from a logging and telemetry point of view for us to be able to identify the operating context.
* For example: Some operating contexts will pre-cache tokens impacting performance telemetry
*/
export abstract class BaseOperatingContext {
protected logger: Logger;
protected config: BrowserConfiguration;
protected available: boolean;
protected browserEnvironment: boolean;
protected static loggerCallback(level: LogLevel, message: string): void {
switch (level) {
case LogLevel.Error:
// eslint-disable-next-line no-console
console.error(message);
return;
case LogLevel.Info:
// eslint-disable-next-line no-console
console.info(message);
return;
case LogLevel.Verbose:
// eslint-disable-next-line no-console
console.debug(message);
return;
case LogLevel.Warning:
// eslint-disable-next-line no-console
console.warn(message);
return;
default:
// eslint-disable-next-line no-console
console.log(message);
return;
}
}
constructor(config: Configuration) {
/*
* If loaded in an environment where window is not available,
* set internal flag to false so that further requests fail.
* This is to support server-side rendering environments.
*/
this.browserEnvironment = typeof window !== "undefined";
this.config = buildConfiguration(config, this.browserEnvironment);
let sessionStorage: Storage | undefined;
try {
sessionStorage = window[BrowserCacheLocation.SessionStorage];
// Mute errors if it's a non-browser environment or cookies are blocked.
} catch (e) {}
const logLevelKey = sessionStorage?.getItem(LOG_LEVEL_CACHE_KEY);
const piiLoggingKey = sessionStorage
?.getItem(LOG_PII_CACHE_KEY)
?.toLowerCase();
const piiLoggingEnabled =
piiLoggingKey === "true"
? true
: piiLoggingKey === "false"
? false
: undefined;
const loggerOptions = { ...this.config.system.loggerOptions };
const logLevel =
logLevelKey && Object.keys(LogLevel).includes(logLevelKey)
? LogLevel[logLevelKey]
: undefined;
if (logLevel) {
loggerOptions.loggerCallback = BaseOperatingContext.loggerCallback;
loggerOptions.logLevel = logLevel;
}
if (piiLoggingEnabled !== undefined) {
loggerOptions.piiLoggingEnabled = piiLoggingEnabled;
}
this.logger = new Logger(loggerOptions, name, version);
this.available = false;
}
/**
* returns the name of the module containing the API controller associated with this operating context
*/
abstract getModuleName(): string;
/**
* returns the string identifier of this operating context
*/
abstract getId(): string;
/**
* returns a boolean indicating whether this operating context is present
*/
abstract initialize(): Promise<boolean>;
/**
* Return the MSAL config
* @returns BrowserConfiguration
*/
getConfig(): BrowserConfiguration {
return this.config;
}
/**
* Returns the MSAL Logger
* @returns Logger
*/
getLogger(): Logger {
return this.logger;
}
isAvailable(): boolean {
return this.available;
}
isBrowserEnvironment(): boolean {
return this.browserEnvironment;
}
}
@@ -0,0 +1,88 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { BaseOperatingContext } from "./BaseOperatingContext.js";
import { IBridgeProxy } from "../naa/IBridgeProxy.js";
import { BridgeProxy } from "../naa/BridgeProxy.js";
import { AccountContext } from "../naa/BridgeAccountContext.js";
declare global {
interface Window {
__initializeNestedAppAuth?(): Promise<void>;
}
}
export class NestedAppOperatingContext extends BaseOperatingContext {
protected bridgeProxy: IBridgeProxy | undefined = undefined;
protected accountContext: AccountContext | null = null;
/*
* TODO: Once we have determine the bundling code return here to specify the name of the bundle
* containing the implementation for this operating context
*/
static readonly MODULE_NAME: string = "";
/**
* Unique identifier for the operating context
*/
static readonly ID: string = "NestedAppOperatingContext";
/**
* Return the module name. Intended for use with import() to enable dynamic import
* of the implementation associated with this operating context
* @returns
*/
getModuleName(): string {
return NestedAppOperatingContext.MODULE_NAME;
}
/**
* Returns the unique identifier for this operating context
* @returns string
*/
getId(): string {
return NestedAppOperatingContext.ID;
}
/**
* Returns the current BridgeProxy
* @returns IBridgeProxy | undefined
*/
getBridgeProxy(): IBridgeProxy | undefined {
return this.bridgeProxy;
}
/**
* Checks whether the operating context is available.
* Confirms that the code is running a browser rather. This is required.
* @returns Promise<boolean> indicating whether this operating context is currently available.
*/
async initialize(): Promise<boolean> {
try {
if (typeof window !== "undefined") {
if (typeof window.__initializeNestedAppAuth === "function") {
await window.__initializeNestedAppAuth();
}
const bridgeProxy: IBridgeProxy = await BridgeProxy.create();
/*
* Because we want single sign on we expect the host app to provide the account context
* with a min set of params that can be used to identify the account
* this.account = nestedApp.getAccountByFilter(bridgeProxy.getAccountContext());
*/
this.accountContext = bridgeProxy.getAccountContext();
this.bridgeProxy = bridgeProxy;
this.available = bridgeProxy !== undefined;
}
} catch (ex) {
this.logger.infoPii(
`Could not initialize Nested App Auth bridge (${ex})`
);
}
this.logger.info(`Nested App Auth Bridge available: ${this.available}`);
return this.available;
}
}
@@ -0,0 +1,50 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { BaseOperatingContext } from "./BaseOperatingContext.js";
export class StandardOperatingContext extends BaseOperatingContext {
/*
* TODO: Once we have determine the bundling code return here to specify the name of the bundle
* containing the implementation for this operating context
*/
static readonly MODULE_NAME: string = "";
/**
* Unique identifier for the operating context
*/
static readonly ID: string = "StandardOperatingContext";
/**
* Return the module name. Intended for use with import() to enable dynamic import
* of the implementation associated with this operating context
* @returns
*/
getModuleName(): string {
return StandardOperatingContext.MODULE_NAME;
}
/**
* Returns the unique identifier for this operating context
* @returns string
*/
getId(): string {
return StandardOperatingContext.ID;
}
/**
* Checks whether the operating context is available.
* Confirms that the code is running a browser rather. This is required.
* @returns Promise<boolean> indicating whether this operating context is currently available.
*/
async initialize(): Promise<boolean> {
this.available = typeof window !== "undefined";
return this.available;
/*
* NOTE: The standard context is available as long as there is a window. If/when we split out WAM from Browser
* We can move the current contents of the initialize method to here and verify that the WAM extension is available
*/
}
}
@@ -0,0 +1,49 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { BaseOperatingContext } from "./BaseOperatingContext.js";
export class UnknownOperatingContext extends BaseOperatingContext {
/*
* TODO: Once we have determine the bundling code return here to specify the name of the bundle
* containing the implementation for this operating context
*/
static readonly MODULE_NAME: string = "";
/**
* Unique identifier for the operating context
*/
static readonly ID: string = "UnknownOperatingContext";
/**
* Returns the unique identifier for this operating context
* @returns string
*/
getId(): string {
return UnknownOperatingContext.ID;
}
/**
* Return the module name. Intended for use with import() to enable dynamic import
* of the implementation associated with this operating context
* @returns
*/
getModuleName(): string {
return UnknownOperatingContext.MODULE_NAME;
}
/**
* Checks whether the operating context is available.
* Confirms that the code is running a browser rather. This is required.
* @returns Promise<boolean> indicating whether this operating context is currently available.
*/
async initialize(): Promise<boolean> {
/**
* This operating context is in use when we have not checked for what the operating context is.
* The context is unknown until we check it.
*/
return true;
}
}