Skip to content

Commit 525affc

Browse files
Added Logger and RequestContextClasses
1 parent 8ea1e31 commit 525affc

19 files changed

+282
-2586
lines changed

lib/AccessTokenCacheItem.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
///<reference path='AccessTokenKey.ts'/>
2-
///<reference path='AccessTokenValue.ts'/>
3-
41
namespace MSAL {
52
export class AccessTokenCacheItem {
63
key: AccessTokenKey;
@@ -10,4 +7,4 @@ namespace MSAL {
107
this.value = value;
118
}
129
}
13-
}
10+
}

lib/AccessTokenKey.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ namespace MSAL {
22
export class AccessTokenKey {
33
authority: string;
44
clientId: string;
5-
user: User;
5+
homeObjectId: string;
66
Scopes: string;
77

8-
constructor(authority: string, clientId: string, user: User, scopes: string) {
8+
constructor(authority: string, clientId: string, scopes: string, homeObjectId: string) {
99
this.authority = authority;
1010
this.clientId = clientId;
1111
this.Scopes = scopes;
12-
this.user = user;
12+
this.homeObjectId = homeObjectId;
1313
}
1414
}
15-
}
15+
}

lib/AccessTokenValue.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ namespace MSAL {
77
this.ExpiresIn = expiresIn;
88
}
99
}
10-
}
10+
}

lib/AuthenticationRequestParameters.ts

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
namespace MSAL {
2+
3+
export enum ResponseTypes {
4+
id_token,
5+
token
6+
}
7+
28
export class AuthenticationRequestParameters {
39
authority: string;
410
clientId: string;
@@ -22,9 +28,8 @@ namespace MSAL {
2228
this.responseType = responseType;
2329
this.redirectUri = redirectUri;
2430
// randomly generated values
25-
if (responseType !== "token") {
31+
if (responseType !== "token")
2632
this.nonce = Utils.Guid();
27-
}
2833
this.correlationId = Utils.Guid();
2934
this.state = Utils.Guid();
3035
this.nonce = Utils.Guid();
@@ -40,18 +45,17 @@ namespace MSAL {
4045
let str: Array<string> = [];
4146
str.push('?response_type=' + this.responseType);
4247
if (this.responseType === ResponseTypes[ResponseTypes.id_token]) {
43-
if (scopes.indexOf(this.clientId) > -1) {
48+
if (scopes.indexOf(this.clientId) > -1)
4449
this.translateclientIdUsedInScope(scopes);
45-
}
4650
}
51+
4752
str.push('scope=' + encodeURIComponent(this.parseScope(scopes)));
4853
str.push('client_id=' + encodeURIComponent(this.clientId));
4954
str.push('redirect_uri=' + encodeURIComponent(this.redirectUri));
5055
str.push('state=' + encodeURIComponent(this.state));
5156
str.push('nonce=' + encodeURIComponent(this.nonce));
52-
if (this.extraQueryParameters) {
57+
if (this.extraQueryParameters)
5358
str.push(this.extraQueryParameters);
54-
}
5559
str.push('client-request-id=' + encodeURIComponent(this.correlationId));
5660
requestUrl = this.authority + '/oauth2/v2.0/authorize' + str.join('&') + "&x-client-SKU=" + this.xClientSku + "&x-client-Ver=" + this.xClientVer;
5761
return requestUrl;
@@ -76,4 +80,4 @@ namespace MSAL {
7680
return scopeList;
7781
}
7882
}
79-
}
83+
}

lib/Constants.ts

+14-14
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ namespace MSAL {
55
static get accessToken(): string { return "access_token"; }
66
static get expiresIn(): string { return "expires_in"; }
77
static get sessionState(): string { return "session_state"; }
8-
static get tokenKeys(): string { return "adal.token.keys"; }
9-
static get accessTokenKey(): string { return "adal.access.token.key"; }
10-
static get expirationKey(): string { return "adal.expiration.key"; }
11-
static get stateLogin(): string { return "adal.state.login"; }
12-
static get stateAcquireToken(): string { return "adal.state.acquireToken"; }
13-
static get stateRenew(): string { return "adal.state.renew"; }
14-
static get nonceIdToken(): string { return "adal.nonce.idtoken"; }
15-
static get userName(): string { return "adal.username"; }
16-
static get idTokenKey(): string { return "adal.idtoken"; }
17-
static get error(): string { return "adal.error"; }
18-
static get loginRequest(): string { return "adal.login.request"; }
19-
static get loginError(): string { return "adal.login.error"; }
20-
static get renewStatus(): string { return "adal.token.renew.status"; }
8+
static get tokenKeys(): string { return "msal.token.keys"; }
9+
static get accessTokenKey(): string { return "msal.access.token.key"; }
10+
static get expirationKey(): string { return "msal.expiration.key"; }
11+
static get stateLogin(): string { return "msal.state.login"; }
12+
static get stateAcquireToken(): string { return "msal.state.acquireToken"; }
13+
static get stateRenew(): string { return "msal.state.renew"; }
14+
static get nonceIdToken(): string { return "msal.nonce.idtoken"; }
15+
static get userName(): string { return "msal.username"; }
16+
static get idTokenKey(): string { return "msal.idtoken"; }
17+
static get error(): string { return "msal.error"; }
18+
static get loginRequest(): string { return "msal.login.request"; }
19+
static get loginError(): string { return "msal.login.error"; }
20+
static get renewStatus(): string { return "msal.token.renew.status"; }
2121
static get resourceDelimeter(): string { return "|"; }
2222
private static _loadFrameTimeout: number = 6000;
2323
static get loadFrameTimeout(): number {
@@ -43,4 +43,4 @@ namespace MSAL {
4343
static get renewToken(): string { return "renewToken"; }
4444
static get unknown(): string { return "UNKNOWN"; }
4545
}
46-
}
46+
}

lib/Logger.ts

+84-31
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,100 @@
11
namespace MSAL {
2-
export enum LoggingLevel {
3-
ERROR,
4-
WARN,
5-
INFO,
6-
VERBOSE
2+
3+
export interface ILoggerCallback {
4+
(level: LogLevel, message: string,containsPii:boolean): boolean;
5+
}
6+
7+
export enum LogLevel {
8+
Error,
9+
Warning,
10+
Info,
11+
Verbose
712
}
813

9-
export class Logger {
10-
static log(level: number, message: string, error: string): void {
11-
if (level <= Logging._logLevel) {
12-
var timestamp = new Date().toUTCString();
13-
var formattedMessage = '';
14-
if (Logging._correlationId)
15-
formattedMessage = timestamp + ':' + Logging._correlationId + '-' + this.libVersion() + '-' + LoggingLevel[level] + ' ' + message;
16-
else
17-
formattedMessage = timestamp + ':' + this.libVersion() + '-' + LoggingLevel[level] + ' ' + message;
18-
if (error) {
19-
formattedMessage += '\nstack:\n' + error;
20-
}
21-
22-
if (Logging._loginCallback)
23-
Logging._loginCallback(formattedMessage);
14+
export class Logger {// Singleton Class
15+
private static _instance: Logger;
16+
private _correlationId: string;
17+
get correlationId(): string { return this._correlationId; }
18+
private _level: LogLevel = LogLevel.Info;
19+
get level(): LogLevel { return this._level; }
20+
set level(logLevel: LogLevel) {
21+
if (LogLevel[logLevel])
22+
this._level = logLevel;
23+
else throw new Error("Provide a valid value for level. Possibles range for logLevel is 0-3");
24+
};
25+
26+
private _piiLoggingEnabled: boolean = false;
27+
get piiLoggingEnabled(): boolean { return this._piiLoggingEnabled; }
28+
set piiLoggingEnabled(piiLoggingEnabled: boolean) {
29+
this._piiLoggingEnabled = piiLoggingEnabled;
30+
};
31+
32+
private _localCallback: ILoggerCallback;
33+
get localCallback(): ILoggerCallback { return this._localCallback; }
34+
set localCallback(localCallback: ILoggerCallback) {
35+
this._localCallback = localCallback;
36+
};
37+
38+
constructor(correlationId: string) {
39+
if (Logger._instance) {
40+
return Logger._instance;
2441
}
42+
this._correlationId = correlationId;
43+
Logger._instance = this;
44+
}
45+
46+
private LogMessage(logMessage: string, logLevel: LogLevel, containsPii: boolean) {
47+
if ((logLevel > this.level) || (!this.piiLoggingEnabled && containsPii)) {
48+
return;
49+
}
50+
var timestamp = new Date().toUTCString();
51+
var log: string = '';
52+
if (!Utils.isEmpty(this.correlationId))
53+
log = timestamp + ':' + this._correlationId + '-' + Utils.GetLibraryVersion() + '-' + LogLevel[logLevel] + ' ' + logMessage;
54+
else
55+
log = timestamp + ':' + Utils.GetLibraryVersion() + '-' + LogLevel[logLevel] + ' ' + logMessage;
56+
57+
this.executeCallback(logLevel, log, containsPii);
58+
59+
}
60+
61+
executeCallback(level: LogLevel, message: string, containsPii: boolean) {
62+
if (this.localCallback)
63+
this.localCallback(level, message, containsPii);
64+
}
65+
66+
error(message: string): void {
67+
this.LogMessage(message, LogLevel.Error, false);
2568
}
2669

27-
static error(message: string, error: string): void {
28-
this.log(LoggingLevel.ERROR, message, error);
70+
errorPii(message: string): void {
71+
this.LogMessage(message, LogLevel.Error, true);
2972
}
3073

31-
static warn(message: string): void {
32-
this.log(LoggingLevel.WARN, message, null);
74+
warning(message: string): void {
75+
this.LogMessage(message, LogLevel.Warning, false);
3376
}
3477

35-
static info(message: string): void {
36-
this.log(LoggingLevel.INFO, message, null);
78+
warningPii(message: string): void {
79+
this.LogMessage(message, LogLevel.Warning, true);
3780
}
3881

39-
static verbose(message: string): void {
40-
this.log(LoggingLevel.VERBOSE, message, null);
82+
info(message: string): void {
83+
this.LogMessage(message, LogLevel.Info, false);
4184
}
4285

43-
static libVersion(): string {
44-
return '1.0.0';
86+
infoPii(message: string): void {
87+
this.LogMessage(message, LogLevel.Info, true);
4588
}
89+
90+
verbose(message: string): void {
91+
this.LogMessage(message, LogLevel.Verbose, false);
92+
}
93+
94+
verbosePii(message: string): void {
95+
this.LogMessage(message, LogLevel.Verbose, true);
96+
}
97+
4698
}
47-
}
99+
100+
}

lib/Logging.ts

-12
This file was deleted.

lib/RequestContext.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
namespace MSAL {// Singleton Class
2+
export class RequestContext {
3+
private static _instance: RequestContext;
4+
private _correlationId: string;
5+
get correlationId(): string { return this._correlationId;}
6+
private _logger: Logger;
7+
get logger(): Logger { return this._logger; }
8+
9+
constructor(correlationId: string) {
10+
if (RequestContext._instance) {
11+
return RequestContext._instance;
12+
}
13+
this._logger = new Logger(correlationId);
14+
this._correlationId = this._logger.correlationId;
15+
RequestContext._instance = this;
16+
}
17+
}
18+
}

lib/RequestInfo.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ namespace MSAL {
55
stateMatch: boolean;
66
stateResponse: string;
77
requestType: string;
8-
98
constructor() {
109
this.valid = false;
1110
this.parameters = {};
@@ -14,4 +13,4 @@ namespace MSAL {
1413
this.requestType = 'unknown';
1514
}
1615
}
17-
}
16+
}

lib/ResponseTypes.ts

-6
This file was deleted.

lib/Storage.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
namespace MSAL {
2-
export class Storage {
2+
export class Storage {// Singleton
3+
private static _instance: Storage;
34
private _localStorageSupported: boolean;
45
private _sessionStorageSupported: boolean;
56
private _cacheLocation: string;
67

78
constructor(cacheLocation: string) {
9+
if (Storage._instance) {
10+
return Storage._instance;
11+
}
812
this._cacheLocation = cacheLocation;
913
this._localStorageSupported = typeof window[this._cacheLocation] != "undefined" && window[this._cacheLocation] != null;
1014
this._sessionStorageSupported = typeof window[cacheLocation] != "undefined" && window[cacheLocation] != null;
15+
Storage._instance = this;
1116
if (!this._localStorageSupported && !this._sessionStorageSupported)
1217
throw new Error('localStorage and sessionStorage not supported');
1318
}
@@ -65,4 +70,4 @@ namespace MSAL {
6570
return results;
6671
}
6772
}
68-
}
73+
}

lib/User.ts

-6
This file was deleted.

0 commit comments

Comments
 (0)