Skip to content

Commit de65864

Browse files
author
Prithvi Kanherkar
authored
Merge pull request #1856 from AzureAD/cache-cleanup-v2
Cache cleanup v2
2 parents ec094d8 + 1e9a858 commit de65864

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

lib/msal-browser/src/app/PublicClientApplication.ts

+20-27
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,14 @@ export class PublicClientApplication {
102102
// Initialize default authority instance
103103
TrustedAuthority.setTrustedAuthoritiesFromConfig(this.config.auth.knownAuthorities, this.config.auth.cloudDiscoveryMetadata);
104104

105-
this.defaultAuthorityPromise = AuthorityFactory.createDiscoveredInstance(
106-
this.config.auth.authority,
107-
this.networkClient
108-
);
105+
this.defaultAuthorityPromise = AuthorityFactory.createDiscoveredInstance(this.config.auth.authority, this.networkClient);
106+
107+
const { location: { hash } } = window;
108+
const cachedHash = this.browserStorage.getItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.URL_HASH), CacheSchemaType.TEMPORARY) as string;
109+
if (StringUtils.isEmpty(hash) && StringUtils.isEmpty(cachedHash)) {
110+
// There is no hash - assume we are in clean state and clear any current request data.
111+
this.browserStorage.cleanRequest();
112+
}
109113
}
110114

111115
// #region Redirect Flow
@@ -156,19 +160,19 @@ export class PublicClientApplication {
156160
*/
157161
private async handleRedirectResponse(): Promise<AuthenticationResult> {
158162
// Get current location hash from window or cache.
159-
const {location: {hash}} = window;
163+
const { location: { hash } } = window;
160164
const cachedHash = this.browserStorage.getItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.URL_HASH), CacheSchemaType.TEMPORARY) as string;
161165
const isResponseHash = UrlString.hashContainsKnownProperties(hash);
162166
const loginRequestUrl = this.browserStorage.getItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.ORIGIN_URI), CacheSchemaType.TEMPORARY) as string;
163167
const currentUrl = BrowserUtils.getCurrentUri();
164-
if (loginRequestUrl === currentUrl) {
168+
if (loginRequestUrl === currentUrl || !this.config.auth.navigateToLoginRequestUrl) {
165169
// We don't need to navigate - check for hash and prepare to process
166170
if (isResponseHash) {
167171
BrowserUtils.clearHash();
168172
return this.handleHash(hash);
169173
} else {
170174
// Loaded page with no valid hash - pass in the value retrieved from cache, or null/empty string
171-
return this.handleHash(`${cachedHash}`);
175+
return this.handleHash(cachedHash);
172176
}
173177
}
174178

@@ -184,39 +188,27 @@ export class PublicClientApplication {
184188
// Navigate to target url
185189
BrowserUtils.navigateWindow(loginRequestUrl, true);
186190
}
187-
return null;
188-
}
189-
190-
if (!isResponseHash) {
191-
// Loaded page with no valid hash - pass in the value retrieved from cache, or null/empty string
192-
return this.handleHash(cachedHash);
193-
}
194-
195-
if (!this.config.auth.navigateToLoginRequestUrl) {
196-
// We don't need to navigate - check for hash and prepare to process
197-
BrowserUtils.clearHash();
198-
return this.handleHash(hash);
199191
}
200192

201193
return null;
202194
}
203195

204196
/**
205-
* Checks if hash exists and handles in window. Otherwise, cancel any current requests and continue.
197+
* Checks if hash exists and handles in window.
206198
* @param responseHash
207199
* @param interactionHandler
208200
*/
209201
private async handleHash(responseHash: string): Promise<AuthenticationResult> {
202+
// There is no hash - return null.
203+
if (StringUtils.isEmpty(responseHash)) {
204+
return null;
205+
}
206+
207+
// Hash contains known properties - handle and return in callback
210208
const currentAuthority = this.browserStorage.getCachedAuthority();
211209
const authClient = await this.createAuthCodeClient(currentAuthority);
212210
const interactionHandler = new RedirectHandler(authClient, this.browserStorage);
213-
if (!StringUtils.isEmpty(responseHash)) {
214-
// Hash contains known properties - handle and return in callback
215-
return interactionHandler.handleCodeResponse(responseHash, this.browserCrypto);
216-
}
217-
// There is no hash - assume we are in clean state and clear any current request data.
218-
this.browserStorage.cleanRequest();
219-
return null;
211+
return interactionHandler.handleCodeResponse(responseHash, this.browserCrypto);
220212
}
221213

222214
/**
@@ -448,6 +440,7 @@ export class PublicClientApplication {
448440
// Handle response from hash string.
449441
return await silentHandler.handleCodeResponse(hash);
450442
} catch (e) {
443+
this.browserStorage.cleanRequest();
451444
throw e;
452445
}
453446
}

lib/msal-common/src/url/UrlString.ts

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ export class UrlString {
167167
const urlString = new UrlString(url);
168168
const parameters = urlString.getDeserializedHash<ServerAuthorizationCodeResponse>();
169169
return !!(
170+
parameters.code ||
170171
parameters.error_description ||
171172
parameters.error ||
172173
parameters.state

samples/msal-browser-samples/VanillaJSTestApp2.0/app/ssoSilent/authConfig.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,12 @@ const graphConfig = {
2525
// Add here scopes for access token to be used at MS Graph API endpoints.
2626
const tokenRequest = {
2727
scopes: ["Mail.Read", "openid", "profile"],
28-
loginHint: "[email protected]",
2928
redirectUri: "http://localhost:30662/",
3029
forceRefresh: false // Set this to "true" to skip a cached token and go to the server to get a new token
3130
};
3231

3332
const silentRequest = {
3433
scopes: ["openid", "profile", "User.Read", "Mail.Read"],
3534
redirectUri: "http://localhost:30662/",
36-
loginHint: "[email protected]"
35+
loginHint: "[email protected]"
3736
};

0 commit comments

Comments
 (0)