Skip to content

Commit 79271fc

Browse files
committed
Adding updates to the redirect handling code
1 parent 7e95b41 commit 79271fc

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,12 @@ export class PublicClientApplication {
106106
this.config.auth.authority,
107107
this.networkClient
108108
);
109+
110+
const { location: { hash } } = window;
111+
if (StringUtils.isEmpty(hash)) {
112+
// There is no hash - assume we are in clean state and clear any current request data.
113+
this.browserStorage.cleanRequest();
114+
}
109115
}
110116

111117
// #region Redirect Flow
@@ -156,19 +162,19 @@ export class PublicClientApplication {
156162
*/
157163
private async handleRedirectResponse(): Promise<AuthenticationResult> {
158164
// Get current location hash from window or cache.
159-
const {location: {hash}} = window;
165+
const { location: { hash } } = window;
160166
const cachedHash = this.browserStorage.getItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.URL_HASH), CacheSchemaType.TEMPORARY) as string;
161167
const isResponseHash = UrlString.hashContainsKnownProperties(hash);
162168
const loginRequestUrl = this.browserStorage.getItem(this.browserStorage.generateCacheKey(TemporaryCacheKeys.ORIGIN_URI), CacheSchemaType.TEMPORARY) as string;
163169
const currentUrl = BrowserUtils.getCurrentUri();
164-
if (loginRequestUrl === currentUrl) {
170+
if (loginRequestUrl === currentUrl || !this.config.auth.navigateToLoginRequestUrl) {
165171
// We don't need to navigate - check for hash and prepare to process
166172
if (isResponseHash) {
167173
BrowserUtils.clearHash();
168174
return this.handleHash(hash);
169175
} else {
170176
// Loaded page with no valid hash - pass in the value retrieved from cache, or null/empty string
171-
return this.handleHash(`${cachedHash}`);
177+
return this.handleHash(cachedHash);
172178
}
173179
}
174180

@@ -184,39 +190,27 @@ export class PublicClientApplication {
184190
// Navigate to target url
185191
BrowserUtils.navigateWindow(loginRequestUrl, true);
186192
}
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);
199193
}
200194

201195
return null;
202196
}
203197

204198
/**
205-
* Checks if hash exists and handles in window. Otherwise, cancel any current requests and continue.
199+
* Checks if hash exists and handles in window.
206200
* @param responseHash
207201
* @param interactionHandler
208202
*/
209203
private async handleHash(responseHash: string): Promise<AuthenticationResult> {
204+
// There is no hash - return null.
205+
if (StringUtils.isEmpty(responseHash)) {
206+
return null;
207+
}
208+
209+
// Hash contains known properties - handle and return in callback
210210
const currentAuthority = this.browserStorage.getCachedAuthority();
211211
const authClient = await this.createAuthCodeClient(currentAuthority);
212212
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;
213+
return interactionHandler.handleCodeResponse(responseHash, this.browserCrypto);
220214
}
221215

222216
/**

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

Lines changed: 1 addition & 0 deletions
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

0 commit comments

Comments
 (0)