Skip to content

Commit

Permalink
Silent works / logout works (tested in browser with a PR AzureAD#1762
Browse files Browse the repository at this point in the history
…built on this)
  • Loading branch information
sameerag committed Jun 9, 2020
1 parent 6088a2f commit 335d0a5
Show file tree
Hide file tree
Showing 13 changed files with 693 additions and 359 deletions.
2 changes: 1 addition & 1 deletion src/account/IAccount.ts
Expand Up @@ -7,5 +7,5 @@ export interface IAccount {
homeAccountId: string;
environment: string;
tenantId: string;
userName: string;
username: string;
}
80 changes: 44 additions & 36 deletions src/client/BaseClient.ts
Expand Up @@ -3,28 +3,29 @@
* Licensed under the MIT License.
*/

import { ClientConfiguration, buildClientConfiguration } from "../config/ClientConfiguration";
import {
ClientConfiguration,
buildClientConfiguration,
} from "../config/ClientConfiguration";
import { ICacheStorage } from "../cache/ICacheStorage";
import { CacheHelpers } from "../cache/CacheHelpers";
import { INetworkModule } from "../network/INetworkModule";
import { ICrypto } from "../crypto/ICrypto";
import { Account } from "../account/Account";
import { Authority } from "../authority/Authority";
import { Logger } from "../logger/Logger";
import { AADServerParamKeys, Constants, HeaderNames } from "../utils/Constants";
import { NetworkResponse } from "../network/NetworkManager";
import { ServerAuthorizationTokenResponse } from "../server/ServerAuthorizationTokenResponse";
import { B2cAuthority } from "../authority/B2cAuthority";
import { UnifiedCacheManager } from "../unifiedCache/UnifiedCacheManager";
import { AccountEntity } from "../unifiedCache/entities/AccountEntity";
import { IAccount } from "../account/IAccount";
import { AccountCache } from "../unifiedCache/utils/CacheTypes";
import { AccountEntity } from "../unifiedCache/entities/AccountEntity";
import { CacheHelper } from "../unifiedCache/utils/CacheHelper";

/**
* Base application class which will construct requests to send to and handle responses from the Microsoft STS using the authorization code flow.
*/
export abstract class BaseClient {

// Logger object
public logger: Logger;

Expand All @@ -40,14 +41,11 @@ export abstract class BaseClient {
// Network Interface
protected networkClient: INetworkModule;

// Helper API object for running cache functions
protected spaCacheManager: CacheHelpers;

// Helper API object for serialized cache operations
protected unifiedCacheManager: UnifiedCacheManager;

// Account object
protected account: Account;
protected account: AccountEntity;

// Default authority object
protected defaultAuthority: Authority;
Expand All @@ -65,16 +63,18 @@ export abstract class BaseClient {
// Initialize storage interface
this.cacheStorage = this.config.storageInterface;

// Initialize storage helper object
this.spaCacheManager = new CacheHelpers(this.cacheStorage);

// Initialize serialized cache manager
this.unifiedCacheManager = new UnifiedCacheManager(this.cacheStorage, this.config.systemOptions.storeInMemory);
this.unifiedCacheManager = new UnifiedCacheManager(
this.cacheStorage,
this.config.systemOptions.storeInMemory
);

// Set the network interface
this.networkClient = this.config.networkInterface;

B2cAuthority.setKnownAuthorities(this.config.authOptions.knownAuthorities);
B2cAuthority.setKnownAuthorities(
this.config.authOptions.knownAuthorities
);

this.defaultAuthority = this.config.authOptions.authority;
}
Expand All @@ -83,7 +83,6 @@ export abstract class BaseClient {
* Creates default headers for requests to token endpoint
*/
protected createDefaultTokenRequestHeaders(): Map<string, string> {

const headers = this.createDefaultLibraryHeaders();
headers.set(HeaderNames.CONTENT_TYPE, Constants.URL_FORM_CONTENT_TYPE);

Expand All @@ -96,10 +95,22 @@ export abstract class BaseClient {
protected createDefaultLibraryHeaders(): Map<string, string> {
const headers = new Map<string, string>();
// client info headers
headers.set(`${AADServerParamKeys.X_CLIENT_SKU}`, this.config.libraryInfo.sku);
headers.set(`${AADServerParamKeys.X_CLIENT_VER}`, this.config.libraryInfo.version);
headers.set(`${AADServerParamKeys.X_CLIENT_OS}`, this.config.libraryInfo.os);
headers.set(`${AADServerParamKeys.X_CLIENT_CPU}`, this.config.libraryInfo.cpu);
headers.set(
`${AADServerParamKeys.X_CLIENT_SKU}`,
this.config.libraryInfo.sku
);
headers.set(
`${AADServerParamKeys.X_CLIENT_VER}`,
this.config.libraryInfo.version
);
headers.set(
`${AADServerParamKeys.X_CLIENT_OS}`,
this.config.libraryInfo.os
);
headers.set(
`${AADServerParamKeys.X_CLIENT_CPU}`,
this.config.libraryInfo.cpu
);

return headers;
}
Expand All @@ -113,36 +124,33 @@ export abstract class BaseClient {
protected executePostToTokenEndpoint(
tokenEndpoint: string,
queryString: string,
headers: Map<string, string> ): Promise<NetworkResponse<ServerAuthorizationTokenResponse>> {

return this.networkClient.sendPostRequestAsync<ServerAuthorizationTokenResponse>(
tokenEndpoint,
{
body: queryString,
headers: headers,
});
headers: Map<string, string>
): Promise<NetworkResponse<ServerAuthorizationTokenResponse>> {
return this.networkClient.sendPostRequestAsync<
ServerAuthorizationTokenResponse
>(tokenEndpoint, {
body: queryString,
headers: headers,
});
}

/**
* Get all currently signed in accounts.
*/
public getAllAccounts(): IAccount[] {
const currentAccounts: AccountCache = this.unifiedCacheManager.getAllAccounts();
console.log("Current Accounts Obj: " , currentAccounts);
const accountValues: AccountEntity[] = Object.values(currentAccounts);
console.log("Accounts: " , accountValues);
const numAccounts = accountValues.length;
if (numAccounts < 1) {
return null;
} else {
return accountValues.map<IAccount>((value) => {
return {
homeAccountId: value.homeAccountId,
environment: value.environment,
tenantId: value.realm,
userName: value.username
};
const allAccounts = accountValues.map<IAccount>((value) => {
const accountObj: AccountEntity = JSON.parse(
JSON.stringify(value)
);
return CacheHelper.toIAccount(accountObj);
});
return allAccounts;
}
}
}

0 comments on commit 335d0a5

Please sign in to comment.