Skip to content

Added Logger and RequestContextClasses #5

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 2 commits into from
Apr 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,4 @@ paket-files/
lib/*.js
lib/*.js.map
#tests/unit/*.js # TODO add these once the unit tests are in TS
#tests/unit/*.js.map
out
#tests/unit/*.js.map
6 changes: 2 additions & 4 deletions lib/AccessTokenCacheItem.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
///<reference path='AccessTokenKey.ts'/>
///<reference path='AccessTokenValue.ts'/>

namespace MSAL {
export class AccessTokenCacheItem {
key: AccessTokenKey;
Expand All @@ -10,4 +7,5 @@ namespace MSAL {
this.value = value;
}
}
}
}

8 changes: 4 additions & 4 deletions lib/AccessTokenKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ namespace MSAL {
export class AccessTokenKey {
authority: string;
clientId: string;
user: User;
userIdentifier: string;
Scopes: string;

constructor(authority: string, clientId: string, user: User, scopes: string) {
constructor(authority: string, clientId: string, scopes: string, userIdentifier: string) {
this.authority = authority;
this.clientId = clientId;
this.Scopes = scopes;
this.user = user;
this.userIdentifier = userIdentifier;
}
}
}
}
2 changes: 1 addition & 1 deletion lib/AccessTokenValue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ namespace MSAL {
this.ExpiresIn = expiresIn;
}
}
}
}
19 changes: 13 additions & 6 deletions lib/AuthenticationRequestParameters.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
namespace MSAL {

export enum ResponseTypes {
id_token,
token
}

export class AuthenticationRequestParameters {
authority: string;
clientId: string;
Expand All @@ -23,19 +29,20 @@ namespace MSAL {
this.redirectUri = redirectUri;
// randomly generated values
if (responseType !== "token") {
this.nonce = Utils.Guid();
this.nonce = Utils.CreateNewGuid();
}
this.correlationId = Utils.Guid();
this.state = Utils.Guid();
this.nonce = Utils.Guid();
this.correlationId = Utils.CreateNewGuid();
this.state = Utils.CreateNewGuid();
this.nonce = Utils.CreateNewGuid();
// telemetry information
this.xClientSku = "Js";
this.xClientVer = Utils.GetLibraryVersion();
}

CreateNavigateUrl(scopes: Array<string>): string {
if (!scopes)
if (!scopes) {
scopes = [this.clientId];
}
let requestUrl = "";
let str: Array<string> = [];
str.push('?response_type=' + this.responseType);
Expand Down Expand Up @@ -76,4 +83,4 @@ namespace MSAL {
return scopeList;
}
}
}
}
28 changes: 14 additions & 14 deletions lib/Constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ namespace MSAL {
static get accessToken(): string { return "access_token"; }
static get expiresIn(): string { return "expires_in"; }
static get sessionState(): string { return "session_state"; }
static get tokenKeys(): string { return "adal.token.keys"; }
static get accessTokenKey(): string { return "adal.access.token.key"; }
static get expirationKey(): string { return "adal.expiration.key"; }
static get stateLogin(): string { return "adal.state.login"; }
static get stateAcquireToken(): string { return "adal.state.acquireToken"; }
static get stateRenew(): string { return "adal.state.renew"; }
static get nonceIdToken(): string { return "adal.nonce.idtoken"; }
static get userName(): string { return "adal.username"; }
static get idTokenKey(): string { return "adal.idtoken"; }
static get error(): string { return "adal.error"; }
static get loginRequest(): string { return "adal.login.request"; }
static get loginError(): string { return "adal.login.error"; }
static get renewStatus(): string { return "adal.token.renew.status"; }
static get tokenKeys(): string { return "msal.token.keys"; }
static get accessTokenKey(): string { return "msal.access.token.key"; }
static get expirationKey(): string { return "msal.expiration.key"; }
static get stateLogin(): string { return "msal.state.login"; }
static get stateAcquireToken(): string { return "msal.state.acquireToken"; }
static get stateRenew(): string { return "msal.state.renew"; }
static get nonceIdToken(): string { return "msal.nonce.idtoken"; }
static get userName(): string { return "msal.username"; }
static get idTokenKey(): string { return "msal.idtoken"; }
static get error(): string { return "msal.error"; }
static get loginRequest(): string { return "msal.login.request"; }
static get loginError(): string { return "msal.login.error"; }
static get renewStatus(): string { return "msal.token.renew.status"; }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ this change.

static get resourceDelimeter(): string { return "|"; }
private static _loadFrameTimeout: number = 6000;
static get loadFrameTimeout(): number {
Expand All @@ -43,4 +43,4 @@ namespace MSAL {
static get renewToken(): string { return "renewToken"; }
static get unknown(): string { return "UNKNOWN"; }
}
}
}
121 changes: 90 additions & 31 deletions lib/Logger.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,106 @@
namespace MSAL {
export enum LoggingLevel {
ERROR,
WARN,
INFO,
VERBOSE

export interface ILoggerCallback {
(level: LogLevel, message: string,containsPii:boolean): void;
}

export enum LogLevel {
Error,
Warning,
Info,
Verbose
}

export class Logger {
static log(level: number, message: string, error: string): void {
if (level <= Logging._logLevel) {
var timestamp = new Date().toUTCString();
var formattedMessage = '';
if (Logging._correlationId)
formattedMessage = timestamp + ':' + Logging._correlationId + '-' + this.libVersion() + '-' + LoggingLevel[level] + ' ' + message;
else
formattedMessage = timestamp + ':' + this.libVersion() + '-' + LoggingLevel[level] + ' ' + message;
if (error) {
formattedMessage += '\nstack:\n' + error;
}

if (Logging._loginCallback)
Logging._loginCallback(formattedMessage);
export class Logger {// Singleton Class
private static _instance: Logger;
private _correlationId: string;
get correlationId(): string { return this._correlationId; }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should have set property on the correlationID.

set correlationId(correlationId: string) {
this._correlationId = correlationId;
};
private _level: LogLevel = LogLevel.Info;
get level(): LogLevel { return this._level; }
set level(logLevel: LogLevel) {
if (LogLevel[logLevel]) {
this._level = logLevel;
}
else throw new Error("Provide a valid value for level. Possibles range for logLevel is 0-3");
};

private _piiLoggingEnabled: boolean = false;
get piiLoggingEnabled(): boolean { return this._piiLoggingEnabled; }
set piiLoggingEnabled(piiLoggingEnabled: boolean) {
this._piiLoggingEnabled = piiLoggingEnabled;
};

private _localCallback: ILoggerCallback;
get localCallback(): ILoggerCallback { return this._localCallback; }
set localCallback(localCallback: ILoggerCallback) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set should fail is callback is already set.

if (this.localCallback) {
throw new Error("MSAL logging callback can only be set once per process and should never change once set.");
}
this._localCallback = localCallback;
};

constructor(correlationId: string) {
if (Logger._instance) {
return Logger._instance;
}
this._correlationId = correlationId;
Logger._instance = this;
}

private LogMessage(logMessage: string, logLevel: LogLevel, containsPii: boolean) {
if ((logLevel > this.level) || (!this.piiLoggingEnabled && containsPii)) {
return;
}
var timestamp = new Date().toUTCString();
var log: string = '';
if (!Utils.isEmpty(this.correlationId)) {
log = timestamp + ':' + this._correlationId + '-' + Utils.GetLibraryVersion() + '-' + LogLevel[logLevel] + ' ' + logMessage;
}
else {
log = timestamp + ':' + Utils.GetLibraryVersion() + '-' + LogLevel[logLevel] + ' ' + logMessage;
}
this.executeCallback(logLevel, log, containsPii);
}

executeCallback(level: LogLevel, message: string, containsPii: boolean) {
if (this.localCallback) {
this.localCallback(level, message, containsPii);
}
}

error(message: string): void {
this.LogMessage(message, LogLevel.Error, false);
}

errorPii(message: string): void {
this.LogMessage(message, LogLevel.Error, true);
}

warning(message: string): void {
this.LogMessage(message, LogLevel.Warning, false);
}

static error(message: string, error: string): void {
this.log(LoggingLevel.ERROR, message, error);
warningPii(message: string): void {
this.LogMessage(message, LogLevel.Warning, true);
}

static warn(message: string): void {
this.log(LoggingLevel.WARN, message, null);
info(message: string): void {
this.LogMessage(message, LogLevel.Info, false);
}

static info(message: string): void {
this.log(LoggingLevel.INFO, message, null);
infoPii(message: string): void {
this.LogMessage(message, LogLevel.Info, true);
}

static verbose(message: string): void {
this.log(LoggingLevel.VERBOSE, message, null);
verbose(message: string): void {
this.LogMessage(message, LogLevel.Verbose, false);
}

static libVersion(): string {
return '1.0.0';
verbosePii(message: string): void {
this.LogMessage(message, LogLevel.Verbose, true);
}
}
}
}
12 changes: 0 additions & 12 deletions lib/Logging.ts

This file was deleted.

19 changes: 19 additions & 0 deletions lib/RequestContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
namespace MSAL {// Singleton Class
export class RequestContext {
private static _instance: RequestContext;
private _correlationId: string;
get correlationId(): string { return this._correlationId;}
private _logger: Logger;
get logger(): Logger { return this._logger; }

constructor(correlationId: string) {
if (RequestContext._instance) {
return RequestContext._instance;
}

this._logger = new Logger(correlationId);
this._correlationId = this._logger.correlationId;
RequestContext._instance = this;
}
}
}
3 changes: 1 addition & 2 deletions lib/RequestInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ namespace MSAL {
stateMatch: boolean;
stateResponse: string;
requestType: string;

constructor() {
this.valid = false;
this.parameters = {};
Expand All @@ -14,4 +13,4 @@ namespace MSAL {
this.requestType = 'unknown';
}
}
}
}
6 changes: 0 additions & 6 deletions lib/ResponseTypes.ts

This file was deleted.

9 changes: 7 additions & 2 deletions lib/Storage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
namespace MSAL {
export class Storage {
export class Storage {// Singleton
private static _instance: Storage;
private _localStorageSupported: boolean;
private _sessionStorageSupported: boolean;
private _cacheLocation: string;

constructor(cacheLocation: string) {
if (Storage._instance) {
return Storage._instance;
}
this._cacheLocation = cacheLocation;
this._localStorageSupported = typeof window[this._cacheLocation] != "undefined" && window[this._cacheLocation] != null;
this._sessionStorageSupported = typeof window[cacheLocation] != "undefined" && window[cacheLocation] != null;
Storage._instance = this;
if (!this._localStorageSupported && !this._sessionStorageSupported)
throw new Error('localStorage and sessionStorage not supported');
}
Expand Down Expand Up @@ -65,4 +70,4 @@ namespace MSAL {
return results;
}
}
}
}
4 changes: 2 additions & 2 deletions lib/User.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace MSAL {
export class User {
export interface User {
username: string;
profile: any;
}
}
}
Loading