Skip to content

Commit

Permalink
Refactor to use an MSAL-specific client
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Turner committed Oct 14, 2020
1 parent e9342e8 commit d8643d9
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
42 changes: 30 additions & 12 deletions sdk/identity/identity/src/client/msalClient.ts
Expand Up @@ -3,9 +3,9 @@ import {
PublicClientApplication,
Configuration,
AuthorizationCodeRequest,
AuthenticationResult,
AuthenticationResult
} from "@azure/msal-node";
import {IdentityClient, TokenCredentialOptions} from "./identityClient";
import { IdentityClient, TokenCredentialOptions } from "./identityClient";
import { AccessToken } from "@azure/core-http";
import { credentialLogger } from "../util/logging";

Expand All @@ -18,7 +18,9 @@ try {

const logger = credentialLogger("InteractiveBrowserCredential");

async function createPersistence(cachePath?: string): Promise<
async function createPersistence(
cachePath?: string
): Promise<
| {
cachePlugin?: {
readFromStorage: () => Promise<string>;
Expand All @@ -38,33 +40,41 @@ async function createPersistence(cachePath?: string): Promise<

return {
cachePlugin: new msalExt.PersistenceCachePlugin(filePersistence)
}
};
}

// On Mac, uses keychain.
if (process.platform === "darwin") {
let keychainPersistence = await msalExt.KeychainPersistence.create(cachePath, "serviceName", "accountName");
let keychainPersistence = await msalExt.KeychainPersistence.create(
cachePath,
"serviceName",
"accountName"
);

return {
cachePlugin: new msalExt.PersistenceCachePlugin(keychainPersistence)
}
};
}

// On Linux, uses libsecret to store to secret service. Libsecret has to be installed.
if (process.platform === "linux") {
let libSecretPersistence = await msalExt.LibSecretPersistence.create(cachePath, "serviceName", "accountName");
let libSecretPersistence = await msalExt.LibSecretPersistence.create(
cachePath,
"serviceName",
"accountName"
);

return {
cachePlugin: new msalExt.PersistenceCachePlugin(libSecretPersistence)
}
};
}

// fall back to using plain text file. Not recommended for storing secrets.
let filePersistence = await msalExt.FilePersistence.create(cachePath);

return {
cachePlugin: new msalExt.PersistenceCachePlugin(filePersistence)
}
};
}

/**
Expand Down Expand Up @@ -109,7 +119,15 @@ export class MsalClient {
private authorityHost: string;
private cachePath?: string;

constructor(clientId: string, tenantId: string, authorityHost: string, persistenceEnabled: boolean, authenticationRecord?: AuthenticationRecord, cachePath?: string, options?: TokenCredentialOptions) {
constructor(
clientId: string,
tenantId: string,
authorityHost: string,
persistenceEnabled: boolean,
authenticationRecord?: AuthenticationRecord,
cachePath?: string,
options?: TokenCredentialOptions
) {
this.identityClient = new IdentityClient(options);
this.clientId = clientId;
this.tenantId = tenantId;
Expand Down Expand Up @@ -143,7 +161,7 @@ export class MsalClient {
system: { networkClient: this.identityClient }
};
this.pca = new PublicClientApplication(publicClientConfig);
this.pca.getAuthCodeUrl
this.pca.getAuthCodeUrl;
}

async acquireTokenFromCache(): Promise<AccessToken | null> {
Expand All @@ -170,7 +188,7 @@ export class MsalClient {
}
}

async getAuthCodeUrl(request: { scopes: string[], redirectUri: string }): Promise<string> {
async getAuthCodeUrl(request: { scopes: string[]; redirectUri: string }): Promise<string> {
await this.preparePublicClientApplication();

return this.pca!.getAuthCodeUrl(request);
Expand Down
Expand Up @@ -9,7 +9,7 @@ import { credentialLogger } from "../util/logging";
import { DefaultTenantId, DeveloperSignOnClientId } from "../constants";
import { Socket } from "net";
import { AuthenticationRequired, MsalClient } from "../client/msalClient";
import { AuthorizationCodeRequest } from "@azure/msal-node"
import { AuthorizationCodeRequest } from "@azure/msal-node";

import express from "express";
import open from "open";
Expand Down Expand Up @@ -61,7 +61,15 @@ export class InteractiveBrowserCredential implements TokenCredential {
authorityHost = "https://login.microsoftonline.com/" + tenantId;
}

this.msalClient = new MsalClient(clientId, tenantId, authorityHost, persistenceEnabled, authenticationRecord, ".", options);
this.msalClient = new MsalClient(
clientId,
tenantId,
authorityHost,
persistenceEnabled,
authenticationRecord,
".",
options
);
}

/**
Expand Down

0 comments on commit d8643d9

Please sign in to comment.