Skip to content

Commit 11336f3

Browse files
committed
Initial PR to enable 'debug' logs in 'msal-common' and 'msal-node'
1 parent 0a2b5e4 commit 11336f3

File tree

14 files changed

+1274
-2458
lines changed

14 files changed

+1274
-2458
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.6.4"
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
@@ -49,6 +49,7 @@ export class AuthorizationCodeClient extends BaseClient {
4949
*/
5050
async acquireToken(request: AuthorizationCodeRequest): Promise<string> {
5151

52+
this.logger.info("in acquireToken call");
5253
const authority: Authority = await this.createAuthority(request && request.authority);
5354
const acquiredTokenResponse = this.executeTokenRequest(authority, request);
5455
return acquiredTokenResponse;

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,7 @@ const DEFAULT_SYSTEM_OPTIONS: SystemOptions = {
8686

8787
// Default logger implementation
8888
const DEFAULT_LOGGER_IMPLEMENTATION: LoggerOptions = {
89-
loggerCallback: () => {
90-
const notImplErr = "Logger - loggerCallbackInterface() has not been implemented.";
91-
throw AuthError.createUnexpectedError(notImplErr);
92-
},
89+
loggerCallback: () => {},
9390
piiLoggingEnabled: false,
9491
logLevel: LogLevel.Info
9592
};

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/src/server/ServerParamsGenerator.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -225,10 +225,10 @@ export class ServerParamsGenerator {
225225
* @param authority
226226
*/
227227
static createQueryString(paramsMap: Map<string, string>): string {
228-
let queryParameterArray: Array<string> = new Array<string>();
228+
const queryParameterArray: Array<string> = new Array<string>();
229229

230230
paramsMap.forEach((value, key) => {
231-
let keyValuePair = key + "=" + value;
231+
const keyValuePair = key + "=" + value;
232232
queryParameterArray.push(keyValuePair);
233233
});
234234

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

+28-20
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ import {
1010
Configuration,
1111
INetworkModule,
1212
} from '@azure/msal-common';
13-
import { ClientConfiguration, buildConfiguration } from '../config/ClientConfiguration';
13+
import {
14+
ClientConfiguration,
15+
buildConfiguration,
16+
} from '../config/ClientConfiguration';
1417
import { CryptoOps } from '../crypto/CryptoOps';
1518
import { Storage } from '../cache/Storage';
1619
import { NetworkUtils } from './../utils/NetworkUtils';
1720

1821
export abstract class ClientApplication {
19-
2022
// Input configuration by developer/user
2123
protected config: ClientConfiguration;
2224

@@ -67,31 +69,34 @@ export abstract class ClientApplication {
6769
);
6870
}
6971

70-
/**
72+
/**
7173
* Creates a url for logging in a user. This will by default add scopes: openid, profile and offline_access. Also performs validation of the request parameters.
7274
* Including any SSO parameters (account, sid, login_hint) will short circuit the authentication and allow you to retrieve a code without interaction.
7375
* @param request
7476
*/
7577
async getAuthCodeUrl(
7678
request: AuthorizationCodeUrlRequest
7779
): Promise<string> {
78-
7980
const authorizationCodeParameters: Configuration = {
8081
authOptions: this.config.auth,
8182
systemOptions: {
82-
tokenRenewalOffsetSeconds: this.config.system.tokenRenewalOffsetSeconds,
83+
tokenRenewalOffsetSeconds: this.config.system
84+
.tokenRenewalOffsetSeconds,
8385
telemetry: this.config.system.telemetry,
8486
},
8587
loggerOptions: {
8688
loggerCallback: this.config.system.loggerOptions.loggerCallback,
87-
piiLoggingEnabled: this.config.system.loggerOptions.piiLoggingEnabled,
89+
piiLoggingEnabled: this.config.system.loggerOptions
90+
.piiLoggingEnabled,
8891
},
8992
cryptoInterface: this.crypto,
9093
networkInterface: this.networkClient,
9194
storageInterface: this.storage,
9295
};
9396

94-
const authorizationCodeClient = new AuthorizationCodeClient(authorizationCodeParameters);
97+
const authorizationCodeClient = new AuthorizationCodeClient(
98+
authorizationCodeParameters
99+
);
95100

96101
return authorizationCodeClient.getAuthCodeUrl(request);
97102
}
@@ -103,40 +108,43 @@ export abstract class ClientApplication {
103108
async acquireTokenByCode(
104109
request: AuthorizationCodeRequest
105110
): Promise<string> {
106-
107111
const authorizationClientConfiguration: Configuration = {
108112
authOptions: this.config.auth,
109113
systemOptions: {
110-
tokenRenewalOffsetSeconds: this.config.system.tokenRenewalOffsetSeconds,
114+
tokenRenewalOffsetSeconds: this.config.system
115+
.tokenRenewalOffsetSeconds,
111116
telemetry: this.config.system.telemetry,
112117
},
113118
loggerOptions: {
114119
loggerCallback: this.config.system.loggerOptions.loggerCallback,
115-
piiLoggingEnabled: this.config.system.loggerOptions.piiLoggingEnabled,
120+
piiLoggingEnabled: this.config.system.loggerOptions
121+
.piiLoggingEnabled,
116122
},
117123
cryptoInterface: this.crypto,
118124
networkInterface: this.networkClient,
119125
storageInterface: this.storage,
120126
};
121127

122-
const authorizationCodeClient = new AuthorizationCodeClient(authorizationClientConfiguration);
128+
const authorizationCodeClient = new AuthorizationCodeClient(
129+
authorizationClientConfiguration
130+
);
123131

124132
return authorizationCodeClient.acquireToken(request);
125133
}
126134

127-
protected getNodeDefaultHeaders(): Map<string, string>{
128-
const msalSkuHeaderKey: string = "x-client-SKU";
129-
const msalVersionHeaderKey: string = "x-client-VER";
130-
const cpuHeaderKey: string = "x-client-CPU";
131-
const osHeaderKey: string = "x-client-OS";
135+
protected getNodeDefaultHeaders(): Map<string, string> {
136+
const msalSkuHeaderKey: string = 'x-client-SKU';
137+
const msalVersionHeaderKey: string = 'x-client-VER';
138+
const cpuHeaderKey: string = 'x-client-CPU';
139+
const osHeaderKey: string = 'x-client-OS';
132140
// const correlationId: string = "client-request_id";
133141
// TODO will also add appName and appVersion
134142

135143
return new Map<string, string>([
136-
[msalSkuHeaderKey, "MSAL.node"],
137-
[msalVersionHeaderKey, "0.1.0"],
138-
[cpuHeaderKey, ""],
139-
[osHeaderKey, process.platform]
144+
[msalSkuHeaderKey, 'MSAL.node'],
145+
[msalVersionHeaderKey, '0.1.0'],
146+
[cpuHeaderKey, ''],
147+
[osHeaderKey, process.platform],
140148
]);
141149
}
142150
}

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

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

6-
import { DeviceCodeClient, DeviceCodeRequest, AuthenticationResult, Configuration } from "@azure/msal-common";
7-
import { ClientConfiguration} from '../config/ClientConfiguration';
6+
import {
7+
DeviceCodeClient,
8+
DeviceCodeRequest,
9+
AuthenticationResult,
10+
Configuration,
11+
} from '@azure/msal-common';
12+
import { ClientConfiguration } from '../config/ClientConfiguration';
813
import { ClientApplication } from './ClientApplication';
914

1015
/**
1116
* Class to be used to acquire tokens for public client applications (desktop, mobile). Public client applications
1217
* are not trusted to safely store application secrets, and therefore can only request tokens in the name of an user.
1318
*/
1419
export class PublicClientApplication extends ClientApplication {
15-
1620
/**
1721
* @constructor
1822
* Constructor for the PublicClientApplication
@@ -46,24 +50,29 @@ export class PublicClientApplication extends ClientApplication {
4650
* until the end-user completes input of credentials.
4751
* @param requestParameters
4852
*/
49-
public async acquireTokenByDeviceCode(request: DeviceCodeRequest): Promise<AuthenticationResult>{
50-
53+
public async acquireTokenByDeviceCode(
54+
request: DeviceCodeRequest
55+
): Promise<AuthenticationResult> {
5156
const deviceCodeClientConfiguration: Configuration = {
5257
authOptions: this.config.auth,
5358
systemOptions: {
54-
tokenRenewalOffsetSeconds: this.config.system.tokenRenewalOffsetSeconds,
59+
tokenRenewalOffsetSeconds: this.config.system
60+
.tokenRenewalOffsetSeconds,
5561
telemetry: this.config.system.telemetry,
5662
},
5763
loggerOptions: {
5864
loggerCallback: this.config.system.loggerOptions.loggerCallback,
59-
piiLoggingEnabled: this.config.system.loggerOptions.piiLoggingEnabled,
65+
piiLoggingEnabled: this.config.system.loggerOptions
66+
.piiLoggingEnabled,
6067
},
6168
cryptoInterface: this.crypto,
6269
networkInterface: this.networkClient,
6370
storageInterface: this.storage,
6471
};
6572

66-
let deviceCodeClient: DeviceCodeClient = new DeviceCodeClient(deviceCodeClientConfiguration);
73+
let deviceCodeClient: DeviceCodeClient = new DeviceCodeClient(
74+
deviceCodeClientConfiguration
75+
);
6776
return deviceCodeClient.acquireToken(request);
6877
}
6978
}

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

+2-23
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,9 @@ const DEFAULT_CACHE_OPTIONS: CacheOptions = {
6767

6868
// Default logger options for browser
6969
const DEFAULT_LOGGER_OPTIONS: LoggerOptions = {
70-
loggerCallback: (
71-
level: LogLevel,
72-
message: string,
73-
containsPii: boolean
74-
): void => {
75-
if (containsPii) {
76-
return;
77-
}
78-
switch (level) {
79-
case LogLevel.Error:
80-
console.error(message);
81-
return;
82-
case LogLevel.Info:
83-
console.info(message);
84-
return;
85-
case LogLevel.Verbose:
86-
console.debug(message);
87-
return;
88-
case LogLevel.Warning:
89-
console.warn(message);
90-
return;
91-
}
92-
},
70+
loggerCallback: () => {},
9371
piiLoggingEnabled: false,
72+
logLevel: LogLevel.Info,
9473
};
9574

9675
// Default system options for browser

lib/msal-node/src/index.ts

+5-2
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
@@ -9,7 +12,7 @@ export { CryptoOps } from './crypto/CryptoOps';
912
// Common Object Formats
1013
export {
1114
// Request
12-
AuthorizationCodeUrlParameters,
15+
AuthorizationCodeUrlRequest,
1316
TokenExchangeParameters,
1417
// Response
1518
AuthResponse,

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import { INetworkModule, NetworkRequestOptions } from '@azure/msal-common';
77
import { HttpMethod } from './../utils/Constants';
8-
import axios, {AxiosRequestConfig} from 'axios';
8+
import axios, { AxiosRequestConfig } from 'axios';
99

1010
/**
1111
* This class implements the Fetch API for GET and POST requests. See more here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

samples/msal-node-auth-code/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ function acquireToken(req, res){
5454

5555
pca.acquireTokenByCode(tokenRequest).then((response) => {
5656
console.log(JSON.stringify(response));
57+
res.send(200);
5758
}).catch((error) => {
5859
console.log(JSON.stringify(error.response));
5960
})

0 commit comments

Comments
 (0)