Skip to content

Commit 65fb4a6

Browse files
authored
Merge pull request #1423 from AzureAD/msal-node-logging
[msal-node] add DEBUG logging
2 parents a128f4e + 45ae3a9 commit 65fb4a6

21 files changed

+1441
-2568
lines changed

lib/msal-common/package-lock.json

+7-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/msal-common/package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"@rollup/plugin-json": "^4.0.0",
6464
"@types/chai": "^4.2.5",
6565
"@types/chai-as-promised": "^7.1.2",
66+
"@types/debug": "^4.1.5",
6667
"@types/mocha": "^5.2.7",
6768
"@types/sinon": "^7.5.0",
6869
"@typescript-eslint/eslint-plugin": "^2.4.0",
@@ -85,5 +86,7 @@
8586
"tslint": "^5.20.0",
8687
"typescript": "^3.7.5"
8788
},
88-
"dependencies": {}
89+
"dependencies": {
90+
"debug": "^4.1.1"
91+
}
8992
}

lib/msal-common/src/client/AuthorizationCodeClient.ts

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export class AuthorizationCodeClient extends BaseClient {
4747
*/
4848
async acquireToken(request: AuthorizationCodeRequest): Promise<string> {
4949

50+
this.logger.info("in acquireToken call");
5051
const authority: Authority = await this.createAuthority(request && request.authority);
5152
const response = await this.executeTokenRequest(authority, request);
5253
return JSON.stringify(response.body);

lib/msal-common/src/config/Configuration.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,7 @@ const DEFAULT_SYSTEM_OPTIONS: SystemOptions = {
8686
// Default logger implementation
8787
const DEFAULT_LOGGER_IMPLEMENTATION: LoggerOptions = {
8888
loggerCallback: () => {
89-
const notImplErr = "Logger - loggerCallbackInterface() has not been implemented.";
90-
throw AuthError.createUnexpectedError(notImplErr);
89+
// allow users to not set loggerCallback
9190
},
9291
piiLoggingEnabled: false,
9392
logLevel: LogLevel.Info

lib/msal-common/src/logger/Logger.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* Licensed under the MIT License.
44
*/
55
import pkg from "../../package.json";
6+
import debug from "debug";
67
import { StringUtils } from "../utils/StringUtils";
78
import { LoggerOptions } from "../config/Configuration";
89

@@ -12,7 +13,8 @@ import { LoggerOptions } from "../config/Configuration";
1213
export type LoggerMessageOptions = {
1314
logLevel: LogLevel,
1415
correlationId?: string,
15-
containsPii?: boolean
16+
containsPii?: boolean,
17+
context?: string
1618
};
1719

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

lib/msal-common/test/config/Configuration.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,8 @@ describe("Configuration.ts Class Unit Tests", () => {
5454
await expect(emptyConfig.networkInterface.sendPostRequestAsync("", null)).to.be.rejectedWith(AuthError);
5555
// Logger options checks
5656
expect(emptyConfig.loggerOptions).to.be.not.null;
57-
expect(() => emptyConfig.loggerOptions.loggerCallback(null, "", false)).to.throw("Unexpected error in authentication.: Logger - loggerCallbackInterface() has not been implemented.");
58-
expect(() => emptyConfig.loggerOptions.loggerCallback(null, "", false)).to.throw(AuthError);
5957
expect(emptyConfig.loggerOptions.piiLoggingEnabled).to.be.false;
58+
expect(emptyConfig.loggerOptions.logLevel).to.be.equal(LogLevel.Info);
6059
});
6160

6261
const clearFunc = (): void => {

lib/msal-common/test/config/PublicClientSPAConfiguration.spec.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,8 @@ describe("SPAConfiguration.ts Class Unit Tests", () => {
5757
await expect(emptyConfig.networkInterface.sendPostRequestAsync("", null)).to.be.rejectedWith(AuthError);
5858
// Logger options checks
5959
expect(emptyConfig.loggerOptions).to.be.not.null;
60-
expect(() => emptyConfig.loggerOptions.loggerCallback(null, "", false)).to.throw("Unexpected error in authentication.: Logger - loggerCallbackInterface() has not been implemented.");
61-
expect(() => emptyConfig.loggerOptions.loggerCallback(null, "", false)).to.throw(AuthError);
6260
expect(emptyConfig.loggerOptions.piiLoggingEnabled).to.be.false;
61+
expect(emptyConfig.loggerOptions.logLevel).to.be.equal(LogLevel.Info);
6362
});
6463

6564
const clearFunc = (): void => {

lib/msal-node/package-lock.json

+2-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/msal-node/src/client/ClientApplication.ts

+20-11
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@ import {
99
AuthorizationCodeRequest,
1010
Configuration,
1111
} from '@azure/msal-common';
12-
import { ClientConfiguration, buildConfiguration } from '../config/ClientConfiguration';
12+
import {
13+
ClientConfiguration,
14+
buildConfiguration,
15+
} from '../config/ClientConfiguration';
1316
import { CryptoProvider } from '../crypto/CryptoProvider';
1417
import { Storage } from '../cache/Storage';
1518

1619
export abstract class ClientApplication {
17-
1820
// Input configuration by developer/user
1921
protected config: ClientConfiguration;
2022

@@ -40,7 +42,6 @@ export abstract class ClientApplication {
4042
* @param {@link (Configuration:type)} configuration object for the MSAL PublicClientApplication instance
4143
*/
4244
protected constructor(configuration: ClientConfiguration) {
43-
4445
this.config = buildConfiguration(configuration);
4546
}
4647

@@ -54,9 +55,12 @@ export abstract class ClientApplication {
5455
* acquireToken(AuthorizationCodeRequest)
5556
* @param request
5657
*/
57-
async getAuthCodeUrl(request: AuthorizationCodeUrlRequest): Promise<string> {
58-
59-
const authorizationCodeClient = new AuthorizationCodeClient(this.buildOauthClientConfiguration());
58+
async getAuthCodeUrl(
59+
request: AuthorizationCodeUrlRequest
60+
): Promise<string> {
61+
const authorizationCodeClient = new AuthorizationCodeClient(
62+
this.buildOauthClientConfiguration()
63+
);
6064
return authorizationCodeClient.getAuthCodeUrl(request);
6165
}
6266

@@ -70,9 +74,12 @@ export abstract class ClientApplication {
7074
*
7175
* @param request
7276
*/
73-
async acquireTokenByCode(request: AuthorizationCodeRequest): Promise<string> {
74-
75-
const authorizationCodeClient = new AuthorizationCodeClient(this.buildOauthClientConfiguration());
77+
async acquireTokenByCode(
78+
request: AuthorizationCodeRequest
79+
): Promise<string> {
80+
const authorizationCodeClient = new AuthorizationCodeClient(
81+
this.buildOauthClientConfiguration()
82+
);
7683
return authorizationCodeClient.acquireToken(request);
7784
}
7885

@@ -81,8 +88,10 @@ export abstract class ClientApplication {
8188
return {
8289
authOptions: this.config.auth,
8390
loggerOptions: {
84-
loggerCallback: this.config.system!.loggerOptions!.loggerCallback,
85-
piiLoggingEnabled: this.config.system!.loggerOptions!.piiLoggingEnabled,
91+
loggerCallback: this.config.system!.loggerOptions!
92+
.loggerCallback,
93+
piiLoggingEnabled: this.config.system!.loggerOptions!
94+
.piiLoggingEnabled,
8695
},
8796
cryptoInterface: new CryptoProvider(),
8897
networkInterface: this.config.system!.networkClient,

lib/msal-node/src/client/PublicClientApplication.ts

+8-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,15 @@
33
* Licensed under the MIT License.
44
*/
55

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

1010
/**
1111
* Class to be used to acquire tokens for public client applications (desktop, mobile). Public client applications
1212
* are not trusted to safely store application secrets, and therefore can only request tokens in the name of an user.
1313
*/
1414
export class PublicClientApplication extends ClientApplication {
15-
1615
/**
1716
* @constructor
1817
* Constructor for the PublicClientApplication
@@ -46,9 +45,12 @@ export class PublicClientApplication extends ClientApplication {
4645
* until the end-user completes input of credentials.
4746
* @param request
4847
*/
49-
public async acquireTokenByDeviceCode(request: DeviceCodeRequest): Promise<string>{
50-
51-
let deviceCodeClient: DeviceCodeClient = new DeviceCodeClient(this.buildOauthClientConfiguration());
48+
public async acquireTokenByDeviceCode(
49+
request: DeviceCodeRequest
50+
): Promise<string> {
51+
let deviceCodeClient: DeviceCodeClient = new DeviceCodeClient(
52+
this.buildOauthClientConfiguration()
53+
);
5254
return deviceCodeClient.acquireToken(request);
5355
}
5456
}

lib/msal-node/src/config/ClientConfiguration.ts

+4-25
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ export type ClientConfiguration = {
5151
};
5252

5353
const DEFAULT_AUTH_OPTIONS: NodeAuthOptions = {
54-
clientId: "",
55-
authority: "",
54+
clientId: '',
55+
authority: '',
5656
};
5757

5858
const DEFAULT_CACHE_OPTIONS: CacheOptions = {
@@ -61,30 +61,9 @@ const DEFAULT_CACHE_OPTIONS: CacheOptions = {
6161
};
6262

6363
const DEFAULT_LOGGER_OPTIONS: LoggerOptions = {
64-
loggerCallback: (
65-
level: LogLevel,
66-
message: string,
67-
containsPii: boolean
68-
): void => {
69-
if (containsPii) {
70-
return;
71-
}
72-
switch (level) {
73-
case LogLevel.Error:
74-
console.error(message);
75-
return;
76-
case LogLevel.Info:
77-
console.info(message);
78-
return;
79-
case LogLevel.Verbose:
80-
console.debug(message);
81-
return;
82-
case LogLevel.Warning:
83-
console.warn(message);
84-
return;
85-
}
86-
},
64+
loggerCallback: () => {},
8765
piiLoggingEnabled: false,
66+
logLevel: LogLevel.Info,
8867
};
8968

9069
const DEFAULT_SYSTEM_OPTIONS: NodeSystemOptions = {

lib/msal-node/src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
export { PublicClientApplication } from './client/PublicClientApplication';
22
export { ConfidentialClientApplication } from './client/ConfidentialClientApplication';
3-
export { ClientConfiguration, buildConfiguration } from './config/ClientConfiguration';
3+
export {
4+
ClientConfiguration,
5+
buildConfiguration,
6+
} from './config/ClientConfiguration';
47
export { Storage } from './cache/Storage';
58

69
// crypto

lib/msal-node/src/network/HttpClient.ts

+12-9
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
* Licensed under the MIT License.
44
*/
55

6-
import { INetworkModule, NetworkRequestOptions, NetworkResponse } from '@azure/msal-common';
6+
import {
7+
INetworkModule,
8+
NetworkRequestOptions,
9+
NetworkResponse,
10+
} from '@azure/msal-common';
711
import { HttpMethod } from './../utils/Constants';
8-
import axios, {AxiosRequestConfig} from 'axios';
12+
import axios, { AxiosRequestConfig } from 'axios';
913

1014
/**
1115
* This class implements the API for network requests.
1216
*/
1317
export class HttpClient implements INetworkModule {
14-
1518
constructor() {
1619
axios.defaults.validateStatus = () => true;
1720
}
@@ -25,17 +28,17 @@ export class HttpClient implements INetworkModule {
2528
url: string,
2629
options?: NetworkRequestOptions
2730
): Promise<NetworkResponse<T>> {
28-
const request: AxiosRequestConfig = {
29-
method: HttpMethod.GET,
30-
url: url,
31-
headers: options && options.headers,
31+
const request: AxiosRequestConfig = {
32+
method: HttpMethod.GET,
33+
url: url,
34+
headers: options && options.headers,
3235
};
3336

3437
const response = await axios(request);
3538
return {
3639
headers: response.headers,
3740
body: response.data as T,
38-
status: response.status
41+
status: response.status,
3942
};
4043
}
4144

@@ -59,7 +62,7 @@ export class HttpClient implements INetworkModule {
5962
return {
6063
headers: response.headers,
6164
body: response.data as T,
62-
status: response.status
65+
status: response.status,
6366
};
6467
}
6568
}

lib/msal-node/src/utils/Constants.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ export const CACHE = {
4343
* Constants for headers
4444
*/
4545
export const Header = {
46-
MSAL_SKU_KEY: "x-client-SKU",
47-
MSAL_SKU_VALUE: "MSAL.node",
48-
MSAL_VERSION: "x-client-VER",
49-
CPU: "x-client-CPU",
50-
OS: "x-client-OS"
46+
MSAL_SKU_KEY: 'x-client-SKU',
47+
MSAL_SKU_VALUE: 'MSAL.node',
48+
MSAL_VERSION: 'x-client-VER',
49+
CPU: 'x-client-CPU',
50+
OS: 'x-client-OS',
5151
// TODO will also add appName and appVersion here
52-
}
52+
};

0 commit comments

Comments
 (0)