Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[msal-node][msal-common] Cache Lookup 1: PR defining lookup interface #1609

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AccessTokenEntity } from "./entities/AccessTokenEntity";
import { IdTokenEntity } from "./entities/IdTokenEntity";
import { RefreshTokenEntity } from "./entities/RefreshTokenEntity";
import { AccountEntity } from "./entities/AccountEntity";
import { ICacheStorage } from "../cache/ICacheStorage";
import { ICacheStorage } from "./interface/ICacheStorage";
import { Deserializer } from "./serialize/Deserializer";
import { Serializer } from "./serialize/Serializer";
import { AccountCache } from "./utils/CacheTypes";
Expand All @@ -22,7 +22,7 @@ export class UnifiedCacheManager {

constructor(cacheImpl: ICacheStorage) {
this.cacheStorage = cacheImpl;
this.readSerializedCache();
this.readSerializedCache().then().catch(() => { console.log("reading cache failed")});
}

async readSerializedCache(): Promise<void> {
Expand Down Expand Up @@ -79,7 +79,6 @@ export class UnifiedCacheManager {
];

const accountKey = accountCacheKey.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();

return this.inMemoryCache.accounts[accountKey] || null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { Credential } from "./Credential";
import { Separators } from "../../utils/Constants";
import { Separators, CredentialType } from "../../utils/Constants";
import { AuthenticationResult } from "../../response/AuthenticationResult";

/**
Expand Down Expand Up @@ -52,7 +52,7 @@ export class AccessTokenEntity extends Credential {
const atEntity: AccessTokenEntity = new AccessTokenEntity();

atEntity.homeAccountId = homeAccountId;
atEntity.credentialType = "AccessToken";
atEntity.credentialType = CredentialType.ACCESS_TOKEN;
atEntity.secret = authenticationResult.accessToken;

const date = new Date();
Expand All @@ -62,12 +62,8 @@ export class AccessTokenEntity extends Credential {
// TODO: Crosscheck the exact conversion UTC
// Token expiry time.
// This value should be  calculated based on the current UTC time measured locally and the value  expires_in Represented as a string in JSON.
atEntity.expiresOn = authenticationResult.expiresOn
.getMilliseconds()
.toString();
atEntity.extendedExpiresOn = authenticationResult.extExpiresOn
.getMilliseconds()
.toString();
atEntity.expiresOn = (authenticationResult.expiresOn.getMilliseconds() / 1000).toString();
atEntity.extendedExpiresOn = (authenticationResult.extExpiresOn.getMilliseconds() / 1000).toString();

atEntity.environment = environment;
atEntity.clientId = clientId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,26 @@ export class AccountEntity {
return accountCacheKeyArray.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();
}

/**
* Generate Account Id key component as per the schema: <home_account_id>-<environment>
*/
public generateAccountId(): string {
const accountId: Array<string> = [
this.homeAccountId,
this.environment,
];

return accountId.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();
}

/**
* Generate Account Id key component as per the schema: <home_account_id>-<environment>
*/
public generateRealm(): string {
return this.realm.toLowerCase();
}


/**
* Build Account cache from IdToken, clientInfo and authority/policy
* @param clientInfo
Expand Down
67 changes: 67 additions & 0 deletions lib/msal-common/src/cache/entities/Credential.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import { Separators, CredentialType } from "../../utils/Constants";

/**
* Base type for credentials to be stored in the cache: eg: ACCESS_TOKEN, ID_TOKEN etc
*/
export class Credential {
homeAccountId: string;
environment: string;
credentialType: CredentialType;
clientId: string;
secret: string;
familyId?: string;
realm?: string;
target?: string;

/**
* Generate Account Id key component as per the schema: <home_account_id>-<environment>
*/
generateAccountId(): string {
const accountId: Array<string> = [
this.homeAccountId,
this.environment,
];

return accountId.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();
}

/**
* Generate Credential Id key component as per the schema: <credential_type>-<client_id>-<realm>
*/
generateCredentialId(): string {
const clientOrFamilyId = CredentialType.REFRESH_TOKEN? this.familyId || this.clientId : this.clientId;
const credentialId: Array<string> = [
this.credentialType,
clientOrFamilyId,
this.realm
];

return credentialId.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();
}

/**
* Generate target key component as per schema: <target>
*/
generateTarget(): string {
return this.target.toLowerCase();
}

/**
* generates credential key
*/
generateCredentialKey(): string {
const credentialKey = [
this.generateAccountId(),
this.generateCredentialId(),
this.generateTarget()
];

return credentialKey.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();
}

};
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { Credential } from "./Credential";
import { Separators } from "../../utils/Constants";
import { Separators, CredentialType } from "../../utils/Constants";
import { AuthenticationResult } from "../../response/AuthenticationResult";

/**
Expand Down Expand Up @@ -44,7 +44,7 @@ export class IdTokenEntity extends Credential {
): IdTokenEntity {
const idTokenEntity = new IdTokenEntity();

idTokenEntity.credentialType = "IdToken";
idTokenEntity.credentialType = CredentialType.ID_TOKEN;
idTokenEntity.homeAccountId = homeAccountId;
idTokenEntity.environment = environment;
idTokenEntity.clientId = clientId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

import { Credential } from "./Credential";
import { Separators } from "../../utils/Constants";
import { Separators, CredentialType } from "../../utils/Constants";
import { AuthenticationResult } from "../../response/AuthenticationResult";

/**
Expand Down Expand Up @@ -50,7 +50,7 @@ export class RefreshTokenEntity extends Credential {
const rtEntity = new RefreshTokenEntity();

rtEntity.clientId = clientId;
rtEntity.credentialType = "RefreshToken";
rtEntity.credentialType = CredentialType.REFRESH_TOKEN;
rtEntity.environment = environment;
rtEntity.homeAccountId = homeAccountId;
rtEntity.secret = refreshToken;
Expand Down
76 changes: 76 additions & 0 deletions lib/msal-common/src/cache/interface/ITokenCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/

import { AccountEntity } from "../entities/AccountEntity";
import { Credential } from "../entities/Credential";
import { AccountCache } from "../utils/CacheTypes";

export interface ITokenCache {
/**
* saves account into cache
* @param account
*/
saveAccount(account: AccountEntity): void;

/**
* saves credential - accessToken, idToken or refreshToken into cache
* @param credential
*/
saveCredential(credential: Credential): void;

/**
* Given account key retrieve an account
* @param key
*/
getAccount(key: string): AccountEntity;

/**
* retrieve a credential - accessToken, idToken or refreshToken; given the cache key
* @param key
*/
getCredential(key: string): Credential;

/**
* retrieve accounts matching all provided filters; if no filter is set, get all accounts
* @param homeAccountId
* @param environment
* @param realm
*/
getAccounts(
homeAccountId?: string,
environment?: string,
realm?: string
): AccountCache;

/**
* retrieve credentails matching all provided filters; if no filter is set, get all credentials
* @param homeAccountId
* @param environment
* @param credentialType
* @param clientId
* @param realm
* @param target
*/
getCredentials(
homeAccountId?: string,
environment?: string,
credentialType?: string,
clientId?: string,
realm?: string,
target?: string
): Credential;

/**
* returns a boolean if the given account is removed
* @param account
*/
removeAccount(account: AccountEntity): boolean;

/**
* returns a boolean if the given credential is removed
* @param credential
*/
removeCredential(credential: Credential): boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AccessTokenEntity } from "../entities/AccessTokenEntity";
import { RefreshTokenEntity } from "../entities/RefreshTokenEntity";
import { AppMetadataEntity } from "../entities/AppMetadataEntity";
import { CacheHelper } from "../utils/CacheHelper";
import { AccountCacheMaps, IdTokenCacheMaps, AccessTokenCacheMaps, RefreshTokenCacheMaps, AppMetadataCacheMaps } from "../serialize/JsonKeys";
import { AccountCacheMaps, IdTokenCacheMaps, AccessTokenCacheMaps, RefreshTokenCacheMaps, AppMetadataCacheMaps } from "./JsonKeys";
import { AccountCache, IdTokenCache, AccessTokenCache, RefreshTokenCache, AppMetadataCache, InMemoryCache, JsonCache } from "../utils/CacheTypes";
import { StringDict } from "../../utils/MsalTypes";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { ICrypto } from "../crypto/ICrypto";
import { StringUtils } from "../utils/StringUtils";
import { UrlString } from "../url/UrlString";
import { ICrypto } from "../../crypto/ICrypto";
import { StringUtils } from "../../utils/StringUtils";
import { UrlString } from "../../url/UrlString";

/**
* Key to cache access tokens, id tokens, and refresh tokens. Helps manage token renewal.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import { AccessTokenCacheItem } from "./AccessTokenCacheItem";
import { AccessTokenKey } from "./AccessTokenKey";
import { AccessTokenValue } from "./AccessTokenValue";
import { ICacheStorage } from "./ICacheStorage";
import { Account } from "../account/Account";
import { Authority } from "../authority/Authority";
import { ServerCodeRequestParameters } from "../server/ServerCodeRequestParameters";
import { ClientAuthError } from "../error/ClientAuthError";
import { StringUtils } from "../utils/StringUtils";
import { TemporaryCacheKeys, Constants } from "../utils/Constants";
import { ICacheStorage } from "../interface/ICacheStorage";
import { Account } from "../../account/Account";
import { Authority } from "../../authority/Authority";
import { ServerCodeRequestParameters } from "../../server/ServerCodeRequestParameters";
import { ClientAuthError } from "../../error/ClientAuthError";
import { StringUtils } from "../../utils/StringUtils";
import { TemporaryCacheKeys, Constants } from "../../utils/Constants";

/**
* The CacheHelpers class contains a set of helper functions used by the module to manage cache items.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* Licensed under the MIT License.
*/

import { Credential } from "../entities/Credential"
import { IdTokenEntity } from "../entities/IdTokenEntity";

export class CacheHelper {
/**
* Helper to convert serialized data to object
Expand Down
8 changes: 4 additions & 4 deletions lib/msal-common/src/client/BaseClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
* Licensed under the MIT License.
*/
import { ClientConfiguration, buildClientConfiguration } from "../config/ClientConfiguration";
import { ICacheStorage } from "../cache/ICacheStorage";
import { CacheHelpers } from "../cache/CacheHelpers";
import { ICacheStorage } from "../cache/interface/ICacheStorage";
import { CacheHelpers } from "../cache/spacache/CacheHelpers";
import { INetworkModule } from "../network/INetworkModule";
import { ICrypto } from "../crypto/ICrypto";
import { Account } from "../account/Account";
Expand All @@ -15,8 +15,8 @@ import { AADServerParamKeys, Constants, HeaderNames } from "../utils/Constants";
import { ClientAuthError } from "../error/ClientAuthError";
import { NetworkResponse } from "../network/NetworkManager";
import { ServerAuthorizationTokenResponse } from "../server/ServerAuthorizationTokenResponse";
import { UnifiedCacheManager } from "../unifiedCache/UnifiedCacheManager";
import { Serializer } from "../unifiedCache/serialize/Serializer";
import { UnifiedCacheManager } from "../cache/UnifiedCacheManager";
import { Serializer } from "../cache/serialize/Serializer";

/**
* Base application class which will construct requests to send to and handle responses from the Microsoft STS using the authorization code flow.
Expand Down
2 changes: 1 addition & 1 deletion lib/msal-common/src/client/SPAClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { ServerAuthorizationCodeResponse } from "../server/ServerAuthorizationCo
import { ServerAuthorizationTokenResponse } from "../server/ServerAuthorizationTokenResponse";
import { ClientAuthError } from "../error/ClientAuthError";
import { ClientConfigurationError } from "../error/ClientConfigurationError";
import { AccessTokenCacheItem } from "../cache/AccessTokenCacheItem";
import { AccessTokenCacheItem } from "../cache/spacache/AccessTokenCacheItem";
import { AuthorityFactory } from "../authority/AuthorityFactory";
import { IdToken } from "../account/IdToken";
import { ScopeSet } from "../request/ScopeSet";
Expand Down
2 changes: 1 addition & 1 deletion lib/msal-common/src/config/ClientConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Licensed under the MIT License.
*/

import { ICacheStorage } from "../cache/ICacheStorage";
import { ICacheStorage } from "../cache/interface/ICacheStorage";
import { INetworkModule } from "../network/INetworkModule";
import { ICrypto, PkceCodes } from "../crypto/ICrypto";
import { AuthError } from "../error/AuthError";
Expand Down
8 changes: 4 additions & 4 deletions lib/msal-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ export { IdTokenClaims } from "./account/IdTokenClaims";
export { Authority } from "./authority/Authority";
export { AuthorityFactory } from "./authority/AuthorityFactory";
// Cache
export { ICacheStorage } from "./cache/ICacheStorage";
export { UnifiedCacheManager } from "./unifiedCache/UnifiedCacheManager";
export { JsonCache, InMemoryCache } from "./unifiedCache/utils/CacheTypes";
export { Serializer } from "./unifiedCache/serialize/Serializer";
export { ICacheStorage } from "./cache/interface/ICacheStorage";
export { UnifiedCacheManager } from "./cache/UnifiedCacheManager";
export { JsonCache, InMemoryCache } from "./cache/utils/CacheTypes";
export { Serializer } from "./cache/serialize/Serializer";
// Network Interface
export { INetworkModule, NetworkRequestOptions } from "./network/INetworkModule";
export { NetworkResponse } from "./network/NetworkManager";
Expand Down