Skip to content

[msal-node] add DEBUG logging #1423

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

Merged
merged 19 commits into from
Apr 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/msal-common/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion lib/msal-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@rollup/plugin-json": "^4.0.0",
"@types/chai": "^4.2.5",
"@types/chai-as-promised": "^7.1.2",
"@types/debug": "^4.1.5",
"@types/mocha": "^5.2.7",
"@types/sinon": "^7.5.0",
"@typescript-eslint/eslint-plugin": "^2.4.0",
Expand All @@ -85,5 +86,7 @@
"tslint": "^5.20.0",
"typescript": "^3.7.5"
},
"dependencies": {}
"dependencies": {
"debug": "^4.1.1"
}
}
1 change: 1 addition & 0 deletions lib/msal-common/src/client/AuthorizationCodeClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export class AuthorizationCodeClient extends BaseClient {
*/
async acquireToken(request: AuthorizationCodeRequest): Promise<string> {

this.logger.info("in acquireToken call");
const authority: Authority = await this.createAuthority(request && request.authority);
const response = await this.executeTokenRequest(authority, request);
return JSON.stringify(response.body);
Expand Down
3 changes: 1 addition & 2 deletions lib/msal-common/src/config/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ const DEFAULT_SYSTEM_OPTIONS: SystemOptions = {
// Default logger implementation
const DEFAULT_LOGGER_IMPLEMENTATION: LoggerOptions = {
loggerCallback: () => {
const notImplErr = "Logger - loggerCallbackInterface() has not been implemented.";
throw AuthError.createUnexpectedError(notImplErr);
// allow users to not set loggerCallback
},
piiLoggingEnabled: false,
logLevel: LogLevel.Info
Expand Down
5 changes: 4 additions & 1 deletion lib/msal-common/src/logger/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Licensed under the MIT License.
*/
import pkg from "../../package.json";
import debug from "debug";
import { StringUtils } from "../utils/StringUtils";
import { LoggerOptions } from "../config/Configuration";

Expand All @@ -12,7 +13,8 @@ import { LoggerOptions } from "../config/Configuration";
export type LoggerMessageOptions = {
logLevel: LogLevel,
correlationId?: string,
containsPii?: boolean
containsPii?: boolean,
context?: string
};

/**
Expand Down Expand Up @@ -67,6 +69,7 @@ export class Logger {
const timestamp = new Date().toUTCString();
const logHeader: string = StringUtils.isEmpty(this.correlationId) ? `[${timestamp}] : ` : `[${timestamp}] : [${this.correlationId}]`;
const log = `${logHeader} : ${pkg.version} : ${LogLevel[options.logLevel]} - ${logMessage}`;
debug(`msal:${LogLevel[options.logLevel]}${options.containsPii ? "-Pii": ""}${options.context ? `:${options.context}` : ""}`)(logMessage);
this.executeCallback(options.logLevel, log, options.containsPii);
}

Expand Down
3 changes: 1 addition & 2 deletions lib/msal-common/test/config/Configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ describe("Configuration.ts Class Unit Tests", () => {
await expect(emptyConfig.networkInterface.sendPostRequestAsync("", null)).to.be.rejectedWith(AuthError);
// Logger options checks
expect(emptyConfig.loggerOptions).to.be.not.null;
expect(() => emptyConfig.loggerOptions.loggerCallback(null, "", false)).to.throw("Unexpected error in authentication.: Logger - loggerCallbackInterface() has not been implemented.");
expect(() => emptyConfig.loggerOptions.loggerCallback(null, "", false)).to.throw(AuthError);
expect(emptyConfig.loggerOptions.piiLoggingEnabled).to.be.false;
expect(emptyConfig.loggerOptions.logLevel).to.be.equal(LogLevel.Info);
});

const clearFunc = (): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ describe("SPAConfiguration.ts Class Unit Tests", () => {
await expect(emptyConfig.networkInterface.sendPostRequestAsync("", null)).to.be.rejectedWith(AuthError);
// Logger options checks
expect(emptyConfig.loggerOptions).to.be.not.null;
expect(() => emptyConfig.loggerOptions.loggerCallback(null, "", false)).to.throw("Unexpected error in authentication.: Logger - loggerCallbackInterface() has not been implemented.");
expect(() => emptyConfig.loggerOptions.loggerCallback(null, "", false)).to.throw(AuthError);
expect(emptyConfig.loggerOptions.piiLoggingEnabled).to.be.false;
expect(emptyConfig.loggerOptions.logLevel).to.be.equal(LogLevel.Info);
});

const clearFunc = (): void => {
Expand Down
8 changes: 2 additions & 6 deletions lib/msal-node/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 20 additions & 11 deletions lib/msal-node/src/client/ClientApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import {
AuthorizationCodeRequest,
Configuration,
} from '@azure/msal-common';
import { ClientConfiguration, buildConfiguration } from '../config/ClientConfiguration';
import {
ClientConfiguration,
buildConfiguration,
} from '../config/ClientConfiguration';
import { CryptoProvider } from '../crypto/CryptoProvider';
import { Storage } from '../cache/Storage';

export abstract class ClientApplication {

// Input configuration by developer/user
protected config: ClientConfiguration;

Expand All @@ -40,7 +42,6 @@ export abstract class ClientApplication {
* @param {@link (Configuration:type)} configuration object for the MSAL PublicClientApplication instance
*/
protected constructor(configuration: ClientConfiguration) {

this.config = buildConfiguration(configuration);
}

Expand All @@ -54,9 +55,12 @@ export abstract class ClientApplication {
* acquireToken(AuthorizationCodeRequest)
* @param request
*/
async getAuthCodeUrl(request: AuthorizationCodeUrlRequest): Promise<string> {

const authorizationCodeClient = new AuthorizationCodeClient(this.buildOauthClientConfiguration());
async getAuthCodeUrl(
request: AuthorizationCodeUrlRequest
): Promise<string> {
const authorizationCodeClient = new AuthorizationCodeClient(
this.buildOauthClientConfiguration()
);
return authorizationCodeClient.getAuthCodeUrl(request);
}

Expand All @@ -70,9 +74,12 @@ export abstract class ClientApplication {
*
* @param request
*/
async acquireTokenByCode(request: AuthorizationCodeRequest): Promise<string> {

const authorizationCodeClient = new AuthorizationCodeClient(this.buildOauthClientConfiguration());
async acquireTokenByCode(
request: AuthorizationCodeRequest
): Promise<string> {
const authorizationCodeClient = new AuthorizationCodeClient(
this.buildOauthClientConfiguration()
);
return authorizationCodeClient.acquireToken(request);
}

Expand All @@ -81,8 +88,10 @@ export abstract class ClientApplication {
return {
authOptions: this.config.auth,
loggerOptions: {
loggerCallback: this.config.system!.loggerOptions!.loggerCallback,
piiLoggingEnabled: this.config.system!.loggerOptions!.piiLoggingEnabled,
loggerCallback: this.config.system!.loggerOptions!
.loggerCallback,
piiLoggingEnabled: this.config.system!.loggerOptions!
.piiLoggingEnabled,
},
cryptoInterface: new CryptoProvider(),
networkInterface: this.config.system!.networkClient,
Expand Down
14 changes: 8 additions & 6 deletions lib/msal-node/src/client/PublicClientApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
* Licensed under the MIT License.
*/

import { DeviceCodeClient, DeviceCodeRequest } from "@azure/msal-common";
import { ClientConfiguration} from '../config/ClientConfiguration';
import { DeviceCodeClient, DeviceCodeRequest } from '@azure/msal-common';
import { ClientConfiguration } from '../config/ClientConfiguration';
import { ClientApplication } from './ClientApplication';

/**
* Class to be used to acquire tokens for public client applications (desktop, mobile). Public client applications
* are not trusted to safely store application secrets, and therefore can only request tokens in the name of an user.
*/
export class PublicClientApplication extends ClientApplication {

/**
* @constructor
* Constructor for the PublicClientApplication
Expand Down Expand Up @@ -46,9 +45,12 @@ export class PublicClientApplication extends ClientApplication {
* until the end-user completes input of credentials.
* @param request
*/
public async acquireTokenByDeviceCode(request: DeviceCodeRequest): Promise<string>{

let deviceCodeClient: DeviceCodeClient = new DeviceCodeClient(this.buildOauthClientConfiguration());
public async acquireTokenByDeviceCode(
request: DeviceCodeRequest
): Promise<string> {
let deviceCodeClient: DeviceCodeClient = new DeviceCodeClient(
this.buildOauthClientConfiguration()
);
return deviceCodeClient.acquireToken(request);
}
}
29 changes: 4 additions & 25 deletions lib/msal-node/src/config/ClientConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ export type ClientConfiguration = {
};

const DEFAULT_AUTH_OPTIONS: NodeAuthOptions = {
clientId: "",
authority: "",
clientId: '',
authority: '',
};

const DEFAULT_CACHE_OPTIONS: CacheOptions = {
Expand All @@ -61,30 +61,9 @@ const DEFAULT_CACHE_OPTIONS: CacheOptions = {
};

const DEFAULT_LOGGER_OPTIONS: LoggerOptions = {
loggerCallback: (
level: LogLevel,
message: string,
containsPii: boolean
): void => {
if (containsPii) {
return;
}
switch (level) {
case LogLevel.Error:
console.error(message);
return;
case LogLevel.Info:
console.info(message);
return;
case LogLevel.Verbose:
console.debug(message);
return;
case LogLevel.Warning:
console.warn(message);
return;
}
},
loggerCallback: () => {},
piiLoggingEnabled: false,
logLevel: LogLevel.Info,
};

const DEFAULT_SYSTEM_OPTIONS: NodeSystemOptions = {
Expand Down
5 changes: 4 additions & 1 deletion lib/msal-node/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
export { PublicClientApplication } from './client/PublicClientApplication';
export { ConfidentialClientApplication } from './client/ConfidentialClientApplication';
export { ClientConfiguration, buildConfiguration } from './config/ClientConfiguration';
export {
ClientConfiguration,
buildConfiguration,
} from './config/ClientConfiguration';
export { Storage } from './cache/Storage';

// crypto
Expand Down
21 changes: 12 additions & 9 deletions lib/msal-node/src/network/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
* Licensed under the MIT License.
*/

import { INetworkModule, NetworkRequestOptions, NetworkResponse } from '@azure/msal-common';
import {
INetworkModule,
NetworkRequestOptions,
NetworkResponse,
} from '@azure/msal-common';
import { HttpMethod } from './../utils/Constants';
import axios, {AxiosRequestConfig} from 'axios';
import axios, { AxiosRequestConfig } from 'axios';

/**
* This class implements the API for network requests.
*/
export class HttpClient implements INetworkModule {

constructor() {
axios.defaults.validateStatus = () => true;
}
Expand All @@ -25,17 +28,17 @@ export class HttpClient implements INetworkModule {
url: string,
options?: NetworkRequestOptions
): Promise<NetworkResponse<T>> {
const request: AxiosRequestConfig = {
method: HttpMethod.GET,
url: url,
headers: options && options.headers,
const request: AxiosRequestConfig = {
method: HttpMethod.GET,
url: url,
headers: options && options.headers,
};

const response = await axios(request);
return {
headers: response.headers,
body: response.data as T,
status: response.status
status: response.status,
};
}

Expand All @@ -59,7 +62,7 @@ export class HttpClient implements INetworkModule {
return {
headers: response.headers,
body: response.data as T,
status: response.status
status: response.status,
};
}
}
12 changes: 6 additions & 6 deletions lib/msal-node/src/utils/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ export const CACHE = {
* Constants for headers
*/
export const Header = {
MSAL_SKU_KEY: "x-client-SKU",
MSAL_SKU_VALUE: "MSAL.node",
MSAL_VERSION: "x-client-VER",
CPU: "x-client-CPU",
OS: "x-client-OS"
MSAL_SKU_KEY: 'x-client-SKU',
MSAL_SKU_VALUE: 'MSAL.node',
MSAL_VERSION: 'x-client-VER',
CPU: 'x-client-CPU',
OS: 'x-client-OS',
// TODO will also add appName and appVersion here
}
};
Loading