Skip to content

Commit bd7bb16

Browse files
committed
Merge branch 'dev' into fix-expiration-issues
2 parents ff1398a + ec094d8 commit bd7bb16

File tree

30 files changed

+231
-64
lines changed

30 files changed

+231
-64
lines changed

lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
},
1616
"packages": [
1717
"lib/*",
18-
"samples/*"
18+
"samples/**/*"
1919
],
2020
"version": "independent",
2121
"concurrency": 2

lib/msal-browser/docs/acquire-token.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ msalInstance.acquireTokenSilent(request).then(tokenResponse => {
3939
return myMSALObj.acquireTokenPopup(request);
4040
}
4141
}).catch(error => {
42-
console.log(error);
42+
handleError(error);
4343
});
4444
```
4545

lib/msal-browser/docs/token-lifetimes.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ const tokenResponse = await msalInstance.acquireTokenSilent(silentRequest).catch
5353
if (error instanceof InteractionRequiredAuthError) {
5454
// fallback to interaction when silent call fails
5555
return await myMSALObj.acquireTokenPopup(request).catch(error => {
56-
console.log(error);
56+
handleError(error);
5757
});
5858
}
5959
});

lib/msal-browser/docs/v1-migration.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ myMSALObj.handleRedirectPromise().then((tokenResponse) => {
7070
const username = accountObj.username;
7171

7272
}).catch((error) => {
73-
console.log(error);
73+
handleError(error);
7474
});
7575

7676
function signIn() {
@@ -79,7 +79,7 @@ function signIn() {
7979

8080
async function getTokenRedirect(request) {
8181
return await myMSALObj.acquireTokenSilent(request).catch(error => {
82-
console.log("silent token acquisition fails. acquiring token using redirect");
82+
this.logger.info("silent token acquisition fails. acquiring token using redirect");
8383
// fallback to interaction when silent call fails
8484
return myMSALObj.acquireTokenRedirect(request)
8585
});
@@ -95,7 +95,7 @@ async function signIn(method) {
9595
try {
9696
const loginResponse = await myMSALObj.loginPopup(loginRequest);
9797
} catch (err) {
98-
console.log(error);
98+
handleError(error);
9999
}
100100

101101
const currentAccounts = myMSALObj.getAllAccounts();
@@ -111,10 +111,10 @@ async function signIn(method) {
111111

112112
async function getTokenPopup(request) {
113113
return await myMSALObj.acquireTokenSilent(request).catch(async (error) => {
114-
console.log("silent token acquisition fails. acquiring token using popup");
114+
this.logger.info("silent token acquisition fails. acquiring token using popup");
115115
// fallback to interaction when silent call fails
116116
return await myMSALObj.acquireTokenPopup(request).catch(error => {
117-
console.log(error);
117+
handleError(error);
118118
});
119119
});
120120
}

lib/msal-browser/src/cache/BrowserStorage.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,7 @@ export class BrowserStorage extends CacheManager {
125125
break;
126126
}
127127
default: {
128-
console.log("Invalid Cache Type");
129-
return;
128+
throw BrowserAuthError.createInvalidCacheTypeError();
130129
}
131130
}
132131
}
@@ -174,8 +173,7 @@ export class BrowserStorage extends CacheManager {
174173
return value;
175174
}
176175
default: {
177-
console.log("Invalid Cache Type");
178-
return {};
176+
throw BrowserAuthError.createInvalidCacheTypeError();
179177
}
180178
}
181179
}

lib/msal-browser/src/error/BrowserAuthError.ts

+11
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ export const BrowserAuthErrorMessage = {
7676
code: "token_request_cache_error",
7777
desc: "The token request could not be fetched from the cache correctly."
7878
},
79+
invalidCacheType: {
80+
code: "invalid_cache_type",
81+
desc: "Invalid cache type"
82+
}
7983
};
8084

8185
/**
@@ -228,4 +232,11 @@ export class BrowserAuthError extends AuthError {
228232
return new BrowserAuthError(BrowserAuthErrorMessage.tokenRequestCacheError.code,
229233
`${BrowserAuthErrorMessage.tokenRequestCacheError.desc} Error Detail: ${errDetail}`);
230234
}
235+
236+
/**
237+
* Creates an error thrown if cache type is invalid.
238+
*/
239+
static createInvalidCacheTypeError(): BrowserAuthError {
240+
return new BrowserAuthError(BrowserAuthErrorMessage.invalidCacheType.code, `${BrowserAuthErrorMessage.invalidCacheType.desc}`);
241+
}
231242
}

lib/msal-browser/test/cache/BrowserStorage.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,29 @@ describe("BrowserStorage() tests", () => {
133133
expect(window.localStorage.getItem(msalCacheKey)).to.be.eq(cacheVal);
134134
});
135135

136+
it("setItem() throws error if type passed in is not one of CacheSchemaType types", () => {
137+
expect(() => browserSessionStorage.setItem(msalCacheKey, cacheVal, "OTHER_TYPE")).to.throw(BrowserAuthError);
138+
expect(() => browserSessionStorage.setItem(msalCacheKey, cacheVal, "OTHER_TYPE")).to.throw(BrowserAuthErrorMessage.invalidCacheType.desc);
139+
expect(() => browserLocalStorage.setItem(msalCacheKey, cacheVal, "OTHER_TYPE")).to.throw(BrowserAuthError);
140+
expect(() => browserLocalStorage.setItem(msalCacheKey, cacheVal, "OTHER_TYPE")).to.throw(BrowserAuthErrorMessage.invalidCacheType.desc);
141+
});
142+
136143
it("getItem()", () => {
137144
window.sessionStorage.setItem(msalCacheKey, cacheVal);
138145
window.localStorage.setItem(msalCacheKey, cacheVal);
139146
expect(browserSessionStorage.getItem(msalCacheKey, CacheSchemaType.TEMPORARY)).to.be.eq(cacheVal);
140147
expect(browserLocalStorage.getItem(msalCacheKey, CacheSchemaType.TEMPORARY)).to.be.eq(cacheVal);
141148
});
142149

150+
it("getItem() throws error if type passed in is not one of CacheSchemaType types", () => {
151+
window.sessionStorage.setItem(msalCacheKey, cacheVal);
152+
window.localStorage.setItem(msalCacheKey, cacheVal);
153+
expect(() => browserSessionStorage.getItem(msalCacheKey, "OTHER_TYPE")).to.throw(BrowserAuthError);
154+
expect(() => browserSessionStorage.getItem(msalCacheKey, "OTHER_TYPE")).to.throw(BrowserAuthErrorMessage.invalidCacheType.desc);
155+
expect(() => browserLocalStorage.getItem(msalCacheKey, "OTHER_TYPE")).to.throw(BrowserAuthError);
156+
expect(() => browserLocalStorage.getItem(msalCacheKey, "OTHER_TYPE")).to.throw(BrowserAuthErrorMessage.invalidCacheType.desc);
157+
});
158+
143159
it("removeItem()", () => {
144160
browserSessionStorage.setItem(msalCacheKey, cacheVal, CacheSchemaType.TEMPORARY);
145161
browserLocalStorage.setItem(msalCacheKey, cacheVal, CacheSchemaType.TEMPORARY);

lib/msal-browser/test/error/BrowserAuthError.spec.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,9 @@ describe("BrowserAuthError Unit Tests", () => {
244244
expect(err.message).to.include(BrowserAuthErrorMessage.silentPromptValueError.desc);
245245
expect(err.name).to.equal("BrowserAuthError");
246246
expect(err.stack).to.include("BrowserAuthError.spec.ts");
247-
});
247+
});
248248

249-
it("createTokenRequestCacheError creates a ClientAuthError object", () => {
249+
it("createTokenRequestCacheError creates a ClientAuthError object", () => {
250250
const err: BrowserAuthError = BrowserAuthError.createTokenRequestCacheError("Couldn't parse request from cache");
251251

252252
expect(err instanceof BrowserAuthError).to.be.true;
@@ -258,4 +258,17 @@ describe("BrowserAuthError Unit Tests", () => {
258258
expect(err.name).to.equal("BrowserAuthError");
259259
expect(err.stack).to.include("BrowserAuthError.spec.ts");
260260
});
261+
262+
it("createInvalidCacheTypeError()", () => {
263+
const err: BrowserAuthError = BrowserAuthError.createInvalidCacheTypeError();
264+
265+
expect(err instanceof BrowserAuthError).to.be.true;
266+
expect(err instanceof AuthError).to.be.true;
267+
expect(err instanceof Error).to.be.true;
268+
expect(err.errorCode).to.equal(BrowserAuthErrorMessage.invalidCacheType.code);
269+
expect(err.errorMessage).to.include(BrowserAuthErrorMessage.invalidCacheType.desc);
270+
expect(err.message).to.include(BrowserAuthErrorMessage.invalidCacheType.desc);
271+
expect(err.name).to.equal("BrowserAuthError");
272+
expect(err.stack).to.include("BrowserAuthError.spec.ts");
273+
});
261274
});

lib/msal-common/src/cache/entities/AccountEntity.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ export class AccountEntity {
8484
case CacheAccountType.GENERIC_ACCOUNT_TYPE:
8585
return CacheType.GENERIC;
8686
default: {
87-
console.log("Unexpected account type");
88-
return null;
87+
throw ClientAuthError.createUnexpectedAccountTypeError();
8988
}
9089
}
9190
}

lib/msal-common/src/cache/entities/CredentialEntity.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { Separators, CredentialType, CacheType, Constants } from "../../utils/Constants";
7+
import { ClientAuthError } from "../../error/ClientAuthError";
78

89
/**
910
* Base type for credentials to be stored in the cache: eg: ACCESS_TOKEN, ID_TOKEN etc
@@ -87,8 +88,7 @@ export class CredentialEntity {
8788
case CredentialType.REFRESH_TOKEN:
8889
return CacheType.REFRESH_TOKEN;
8990
default: {
90-
console.log("Unexpected credential type");
91-
return null;
91+
throw ClientAuthError.createUnexpectedCredentialTypeError();
9292
}
9393
}
9494
}

lib/msal-common/src/error/ClientAuthError.ts

+33
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ export const ClientAuthErrorMessage = {
134134
noCryptoObj: {
135135
code: "no_crypto_object",
136136
desc: "No crypto object detected. This is required for the following operation: "
137+
},
138+
invalidCacheType: {
139+
code: "invalid_cache_type",
140+
desc: "Invalid cache type"
141+
},
142+
unexpectedAccountType: {
143+
code: "unexpected_account_type",
144+
desc: "Unexpected account type."
145+
},
146+
unexpectedCredentialType: {
147+
code: "unexpected_credential_type",
148+
desc: "Unexpected credential type."
137149
}
138150
};
139151

@@ -389,4 +401,25 @@ export class ClientAuthError extends AuthError {
389401
static createNoCryptoObjectError(operationName: string): ClientAuthError {
390402
return new ClientAuthError(ClientAuthErrorMessage.noCryptoObj.code, `${ClientAuthErrorMessage.noCryptoObj.desc}${operationName}`);
391403
}
404+
405+
/**
406+
* Throws error if cache type is invalid.
407+
*/
408+
static createInvalidCacheTypeError(): ClientAuthError {
409+
return new ClientAuthError(ClientAuthErrorMessage.invalidCacheType.code, `${ClientAuthErrorMessage.invalidCacheType.desc}`);
410+
}
411+
412+
/**
413+
* Throws error if unexpected account type.
414+
*/
415+
static createUnexpectedAccountTypeError(): ClientAuthError {
416+
return new ClientAuthError(ClientAuthErrorMessage.unexpectedAccountType.code, `${ClientAuthErrorMessage.unexpectedAccountType.desc}`);
417+
}
418+
419+
/**
420+
* Throws error if unexpected credential type.
421+
*/
422+
static createUnexpectedCredentialTypeError(): ClientAuthError {
423+
return new ClientAuthError(ClientAuthErrorMessage.unexpectedCredentialType.code, `${ClientAuthErrorMessage.unexpectedCredentialType.desc}`);
424+
}
392425
}

lib/msal-common/src/request/RequestValidator.ts

-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ export class RequestValidator {
8080
// Remove any query parameters already included in SSO params
8181
queryParams.forEach((value, key) => {
8282
if (eQParams[key]) {
83-
console.log("Removed param " + key + " from extraQueryParameters since it was already present in library query parameters.");
8483
delete eQParams[key];
8584
}
8685
});

lib/msal-common/test/cache/entities/AccessTokenEntity.spec.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { expect } from "chai";
22
import { AccessTokenEntity } from "../../../src/cache/entities/AccessTokenEntity";
33
import { mockCache } from "./cacheConstants";
4+
import { ClientAuthError, ClientAuthErrorMessage } from "../../../src";
5+
import { CacheType } from "../../../src/utils/Constants";
46

57
describe("AccessTokenEntity.ts Unit Tests", () => {
68

@@ -10,9 +12,20 @@ describe("AccessTokenEntity.ts Unit Tests", () => {
1012
});
1113

1214
it("Generate AccessTokenEntity key", () => {
13-
let at = mockCache.createMockATOne();
15+
const at = mockCache.createMockATOne();
1416
expect(at.generateCredentialKey()).to.eql(
1517
"uid.utid-login.microsoftonline.com-accesstoken-mock_client_id-microsoft-scope1 scope2 scope3"
1618
);
1719
});
20+
21+
it("Throws error if AccessTokenEntity is not assigned a type", () => {
22+
const at = new AccessTokenEntity();
23+
expect(() => at.generateType()).to.throw(ClientAuthError);
24+
expect(() => at.generateType()).to.throw(ClientAuthErrorMessage.unexpectedCredentialType.desc);
25+
});
26+
27+
it("Generate AccessTokenEntity type", () => {
28+
const at = mockCache.createMockATOne();
29+
expect(at.generateType()).to.eql(CacheType.ACCESS_TOKEN);
30+
});
1831
});

lib/msal-common/test/cache/entities/AccountEntity.spec.ts

+14-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { NetworkRequestOptions, INetworkModule } from "../../../src/network/INet
88
import { ICrypto, PkceCodes } from "../../../src/crypto/ICrypto";
99
import { RANDOM_TEST_GUID, TEST_DATA_CLIENT_INFO, TEST_CONFIG, TEST_TOKENS, TEST_URIS } from "../../utils/StringConstants";
1010
import sinon from "sinon";
11+
import { ClientAuthError, ClientAuthErrorMessage } from "../../../src";
1112
import { ClientTestUtils } from "../../client/ClientTestUtils";
1213

1314
describe("AccountEntity.ts Unit Tests", () => {
@@ -25,13 +26,25 @@ describe("AccountEntity.ts Unit Tests", () => {
2526
});
2627

2728
it("generate an AccountEntityKey", () => {
28-
let ac = new AccountEntity();
29+
const ac = new AccountEntity();
2930
Object.assign(ac, mockAccountEntity);
3031
expect(ac.generateAccountKey()).to.eql(
3132
"uid.utid-login.microsoftonline.com-microsoft"
3233
);
3334
});
3435

36+
it("throws error if account entity is not assigned a type", () => {
37+
const ac = new AccountEntity();
38+
expect(() => ac.generateType()).to.throw(ClientAuthError);
39+
expect(() => ac.generateType()).to.throw(ClientAuthErrorMessage.unexpectedAccountType.desc);
40+
});
41+
42+
it("generate type of the cache", () => {
43+
const ac = new AccountEntity();
44+
Object.assign(ac, mockAccountEntity);
45+
expect(ac.generateType()).to.eql(1003);
46+
});
47+
3548
it("create an Account", () => {
3649
let cryptoInterface: ICrypto = {
3750
createNewGuid(): string {
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { expect } from "chai";
22
import { IdTokenEntity } from "../../../src/cache/entities/IdTokenEntity";
33
import { mockIdTokenEntity } from "./cacheConstants";
4+
import { ClientAuthError, ClientAuthErrorMessage } from "../../../src";
5+
import { CacheType } from "../../../src/utils/Constants";
46

57
describe("IdTokenEntity.ts Unit Tests", () => {
68
it("Verify an IdTokenEntity", () => {
@@ -9,10 +11,22 @@ describe("IdTokenEntity.ts Unit Tests", () => {
911
});
1012

1113
it("Create an IdTokenEntity", () => {
12-
let idT = new IdTokenEntity();
14+
const idT = new IdTokenEntity();
1315
Object.assign(idT, mockIdTokenEntity);
1416
expect(idT.generateCredentialKey()).to.eql(
1517
"uid.utid-login.microsoftonline.com-idtoken-mock_client_id-microsoft-"
1618
);
1719
});
20+
21+
it("Throws error if IdTokenEntity is not assigned a type", () => {
22+
const idT = new IdTokenEntity();
23+
expect(() => idT.generateType()).to.throw(ClientAuthError);
24+
expect(() => idT.generateType()).to.throw(ClientAuthErrorMessage.unexpectedCredentialType.desc);
25+
});
26+
27+
it("Generate IdTokenEntity type", () => {
28+
const idT = new IdTokenEntity();
29+
Object.assign(idT, mockIdTokenEntity);
30+
expect(idT.generateType()).to.eql(CacheType.ID_TOKEN);
31+
});
1832
});
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { expect } from "chai";
22
import { RefreshTokenEntity } from "../../../src/cache/entities/RefreshTokenEntity";
33
import { mockRefreshTokenEntity, mockRefreshTokenEntityWithFamilyId } from "./cacheConstants";
4+
import { ClientAuthError, ClientAuthErrorMessage } from "../../../src";
5+
import { CacheType } from "../../../src/utils/Constants";
46

57
describe("RefreshTokenEntity.ts Unit Tests", () => {
68
it("Verify a RefreshTokenEntity", () => {
@@ -9,18 +11,30 @@ describe("RefreshTokenEntity.ts Unit Tests", () => {
911
});
1012

1113
it("Create a RefreshTokenEntity", () => {
12-
let rt = new RefreshTokenEntity();
14+
const rt = new RefreshTokenEntity();
1315
Object.assign(rt, mockRefreshTokenEntity);
1416
expect(rt.generateCredentialKey()).to.eql(
1517
"uid.utid-login.microsoftonline.com-refreshtoken-mock_client_id--"
1618
);
1719
});
1820

1921
it("Create a RefreshTokenEntity with familyId", () => {
20-
let rt = new RefreshTokenEntity();
22+
const rt = new RefreshTokenEntity();
2123
Object.assign(rt, mockRefreshTokenEntityWithFamilyId);
2224
expect(rt.generateCredentialKey()).to.eql(
2325
"uid.utid-login.microsoftonline.com-refreshtoken-1--"
2426
);
2527
});
28+
29+
it("Throws error if RefreshTokenEntity is not assigned a type", () => {
30+
const rt = new RefreshTokenEntity();
31+
expect(() => rt.generateType()).to.throw(ClientAuthError);
32+
expect(() => rt.generateType()).to.throw(ClientAuthErrorMessage.unexpectedCredentialType.desc);
33+
});
34+
35+
it("Generate RefreshTokenEntity type", () => {
36+
const rt = new RefreshTokenEntity();
37+
Object.assign(rt, mockRefreshTokenEntity);
38+
expect(rt.generateType()).to.eql(CacheType.REFRESH_TOKEN);
39+
});
2640
});

0 commit comments

Comments
 (0)