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,71 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import {
AccountEntity,
ICachePlugin,
TokenCacheContext,
} from "@azure/msal-common/node";
import { TokenCache } from "../TokenCache.js";
import { IPartitionManager } from "./IPartitionManager.js";
import { ICacheClient } from "./ICacheClient.js";
/**
* Cache plugin that serializes data to the cache and deserializes data from the cache
* @public
*/
export class DistributedCachePlugin implements ICachePlugin {
private client: ICacheClient;
private partitionManager: IPartitionManager;
constructor(client: ICacheClient, partitionManager: IPartitionManager) {
this.client = client;
this.partitionManager = partitionManager;
}
/**
* Deserializes the cache before accessing it
* @param cacheContext - TokenCacheContext
*/
public async beforeCacheAccess(
cacheContext: TokenCacheContext
): Promise<void> {
const partitionKey = await this.partitionManager.getKey();
const cacheData = await this.client.get(partitionKey);
cacheContext.tokenCache.deserialize(cacheData);
}
/**
* Serializes the cache after accessing it
* @param cacheContext - TokenCacheContext
*/
public async afterCacheAccess(
cacheContext: TokenCacheContext
): Promise<void> {
if (cacheContext.cacheHasChanged) {
const kvStore = (
cacheContext.tokenCache as TokenCache
).getKVStore();
const accountEntities = Object.values(kvStore).filter((value) =>
AccountEntity.isAccountEntity(value as object)
);
let partitionKey: string;
if (accountEntities.length > 0) {
const accountEntity = accountEntities[0] as AccountEntity;
partitionKey = await this.partitionManager.extractKey(
accountEntity
);
} else {
partitionKey = await this.partitionManager.getKey();
}
await this.client.set(
partitionKey,
cacheContext.tokenCache.serialize()
);
}
}
}
+27
View File
@@ -0,0 +1,27 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
/**
* Interface for the cache that defines a getter and setter
* @public
*/
export interface ICacheClient {
/**
* Retrieve the value from the cache
*
* @param key - key of item in the cache
* @returns Promise<string>
*/
get(key: string): Promise<string>;
/**
* Save the required value using the provided key to cache
*
* @param key - key of item in the cache
* @param value - value of item to be saved in the cache
* @returns Promise<string>
*/
set(key: string, value: string): Promise<string>;
}
@@ -0,0 +1,39 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { AccountEntity } from "@azure/msal-common/node";
/**
* Interface that defines getter methods to get keys used to identity data in the cache
* @public
*/
export interface IPartitionManager {
/**
* This function should return the correct key from which to read
* the specific user's information from cache.
*
* Example: Your application may be partitioning the user's cache
* information for each user using the homeAccountId and thus
* this function would return the homeAccountId for
* the user in question
*
* @returns Promise<string>
*/
getKey(): Promise<string>;
/**
* This function should return the correct key being used to save each
* user's cache information to cache - given an AccountEntity
*
* Example: Your application may be partitioning the user's cache
* information for each user using the homeAccountId thus
* this function would return the homeAccountId from
* the provided AccountEntity
*
* @param accountEntity - AccountEntity
* @returns Promise<string>
*/
extractKey(accountEntity: AccountEntity): Promise<string>;
}