Skip to content
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

Instantiate Logger instance for PublicClientApplication #1882

Merged
merged 3 commits into from
Jul 2, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 9 additions & 2 deletions lib/msal-browser/src/app/PublicClientApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ import {
ClientConfiguration,
SilentFlowClient,
EndSessionRequest,
BaseAuthRequest
BaseAuthRequest,
Logger
} from "@azure/msal-common";
import { buildConfiguration, Configuration } from "../config/Configuration";
import { BrowserStorage } from "../cache/BrowserStorage";
Expand Down Expand Up @@ -65,6 +66,9 @@ export class PublicClientApplication implements IPublicClientApplication {
// Default authority
private defaultAuthorityPromise: Promise<Authority>;

// Logger
private logger: Logger;

/**
* @constructor
* Constructor for the PublicClientApplication used to instantiate the PublicClientApplication object
Expand Down Expand Up @@ -99,6 +103,9 @@ export class PublicClientApplication implements IPublicClientApplication {
// Initialize the browser storage class.
this.browserStorage = new BrowserStorage(this.config.auth.clientId, this.config.cache);

// Initialize logger
this.logger = new Logger(this.config.system.loggerOptions);

// Initialize default authority instance
TrustedAuthority.setTrustedAuthoritiesFromConfig(this.config.auth.knownAuthorities, this.config.auth.cloudDiscoveryMetadata);

Expand Down Expand Up @@ -153,7 +160,7 @@ export class PublicClientApplication implements IPublicClientApplication {
this.browserStorage.setItem(hashKey, hash, CacheSchemaType.TEMPORARY);
if (StringUtils.isEmpty(loginRequestUrl) || loginRequestUrl === "null") {
// Redirect to home page if login request url is null (real null or the string null)
console.warn("Unable to get valid login request url from cache, redirecting to home page");
this.logger.warning("Unable to get valid login request url from cache, redirecting to home page");
BrowserUtils.navigateWindow("/", true);
} else {
// Navigate to target url
Expand Down
22 changes: 2 additions & 20 deletions lib/msal-browser/src/config/Configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License.
*/
import { SystemOptions, LoggerOptions, INetworkModule, LogLevel, DEFAULT_SYSTEM_OPTIONS, Constants } from "@azure/msal-common";
import { SystemOptions, LoggerOptions, INetworkModule, DEFAULT_SYSTEM_OPTIONS, Constants } from "@azure/msal-common";
import { BrowserUtils } from "../utils/BrowserUtils";
import { BrowserConstants } from "../utils/BrowserConstants";

Expand Down Expand Up @@ -82,25 +82,7 @@ const DEFAULT_CACHE_OPTIONS: CacheOptions = {

// Default logger options for browser
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: (): void => {},
piiLoggingEnabled: false
};

Expand Down
1 change: 1 addition & 0 deletions lib/msal-browser/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export {
AuthErrorMessage,
INetworkModule,
// Logger Object
ILoggerCallback,
Logger,
LogLevel
} from "@azure/msal-common";
29 changes: 27 additions & 2 deletions lib/msal-browser/test/config/Configuration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,38 @@ describe("Configuration.ts Class Unit Tests", () => {
expect(emptyConfig.system.telemetry).to.be.null;
});

it("Tests default logger", () => {
it("Tests logger", () => {
const consoleErrorSpy = sinon.spy(console, "error");
const consoleInfoSpy = sinon.spy(console, "info");
const consoleDebugSpy = sinon.spy(console, "debug");
const consoleWarnSpy = sinon.spy(console, "warn");
const message = "log message";
let emptyConfig: Configuration = buildConfiguration({auth: null});
let emptyConfig: Configuration = buildConfiguration({
auth: null,
system: {
loggerOptions: {
loggerCallback: (level, message, containsPii) => {
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;
}
}
}
}
});
emptyConfig.system.loggerOptions.loggerCallback(LogLevel.Error, message, true)
expect(consoleErrorSpy.called).to.be.false;
emptyConfig.system.loggerOptions.loggerCallback(LogLevel.Error, message, false)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,31 @@ const msalConfig = {
cache: {
cacheLocation: "sessionStorage", // This configures where your cache will be stored
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
},
system: {
loggerOptions: {
loggerCallback: (level, message, containsPii) => {
if (containsPii) {
return;
}
switch (level) {
case msal.LogLevel.Error:
console.error(message);
return;
case msal.LogLevel.Info:
console.info(message);
return;
case msal.LogLevel.Verbose:
console.debug(message);
return;
case msal.LogLevel.Warning:
console.warn(message);
return;
}
}
}
}
};
};

// Add here scopes for id token to be used at MS Identity Platform endpoints.
const loginRequest = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,31 @@ const msalConfig = {
cache: {
cacheLocation: "sessionStorage", // This configures where your cache will be stored
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
},
system: {
loggerOptions: {
loggerCallback: (level, message, containsPii) => {
if (containsPii) {
return;
}
switch (level) {
case msal.LogLevel.Error:
console.error(message);
return;
case msal.LogLevel.Info:
console.info(message);
return;
case msal.LogLevel.Verbose:
console.debug(message);
return;
case msal.LogLevel.Warning:
console.warn(message);
return;
}
}
}
}
};
};

// Add here scopes for id token to be used at MS Identity Platform endpoints.
const loginRequest = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,29 @@ const msalConfig = {
cache: {
cacheLocation: "sessionStorage", // This configures where your cache will be stored
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
},
system: {
loggerOptions: {
loggerCallback: (level, message, containsPii) => {
if (containsPii) {
return;
}
switch (level) {
case msal.LogLevel.Error:
console.error(message);
return;
case msal.LogLevel.Info:
console.info(message);
return;
case msal.LogLevel.Verbose:
console.debug(message);
return;
case msal.LogLevel.Warning:
console.warn(message);
return;
}
}
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,31 @@ const msalConfig = {
cache: {
cacheLocation: "sessionStorage", // This configures where your cache will be stored
storeAuthStateInCookie: false, // Set this to "true" if you are having issues on IE11 or Edge
},
system: {
loggerOptions: {
loggerCallback: (level, message, containsPii) => {
if (containsPii) {
return;
}
switch (level) {
case msal.LogLevel.Error:
console.error(message);
return;
case msal.LogLevel.Info:
console.info(message);
return;
case msal.LogLevel.Verbose:
console.debug(message);
return;
case msal.LogLevel.Warning:
console.warn(message);
return;
}
}
}
}
};
};

// Add here scopes for id token to be used at MS Identity Platform endpoints.
const loginRequest = {
Expand Down