Skip to content

Commit

Permalink
Add onRedirectNavigate callback to stop navigatation and get redirect…
Browse files Browse the repository at this point in the history
… url
  • Loading branch information
jasonnutter committed May 22, 2020
1 parent e65a909 commit 5f9340a
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/msal-core/src/AuthenticationParameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export type AuthenticationParameters = {
forceRefresh?: boolean;
redirectUri?: string;
redirectStartPage?: string;
authorityMetadata?: string
authorityMetadata?: string;
onRedirectNavigate?: ((url: string) => void | boolean)
};

export function validateClaimsRequest(request: AuthenticationParameters) {
Expand Down
20 changes: 18 additions & 2 deletions lib/msal-core/src/UserAgentApplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -588,8 +588,24 @@ export class UserAgentApplication {
}
}
} else {
// prompt user for interaction
this.navigateWindow(urlNavigate, popUpWindow);
// If onRedirectNavigate is implemented, invoke it and provide urlNavigate
if (request.onRedirectNavigate) {
this.logger.verbose("Invoking onRedirectNavigate callback");

const navigate = request.onRedirectNavigate(urlNavigate);

// Returning false from onRedirectNavigate will stop navigation
if (navigate !== false) {
this.logger.verbose("onRedirectNavigate did not return false, navigating");
this.navigateWindow(urlNavigate);
} else {
this.logger.verbose("onRedirectNavigate returned false, stopping navigation");
}
} else {
// Otherwise, perform navigation
this.logger.verbose("Navigating window to urlNavigate");
this.navigateWindow(urlNavigate);
}
}
} catch (err) {
this.logger.error(err);
Expand Down
108 changes: 108 additions & 0 deletions lib/msal-core/test/UserAgentApplication.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,114 @@ describe("UserAgentApplication.ts Class", function () {
msal.loginRedirect({});
});

it("loginRedirect does not navigate if onRedirectNavigate is implemented and returns false", done => {
const config: Configuration = {
auth: {
clientId: TEST_CONFIG.MSAL_CLIENT_ID
}
};

window.location = {
...oldWindowLocation,
hash: "",
assign: function (url) {
throw new Error("window.location.assign should not be called when onRedirectNavigate returns false");
}
};

msal = new UserAgentApplication(config);
msal.handleRedirectCallback(authCallback);
msal.loginRedirect({
onRedirectNavigate: url => {
expect(url).to.be.not.null;

done();
return false;
}
})
});

it("acquireTokenRedirect does not navigate if onRedirectNavigate is implemented and returns false", done => {
const config: Configuration = {
auth: {
clientId: TEST_CONFIG.MSAL_CLIENT_ID
}
};

window.location = {
...oldWindowLocation,
hash: "",
assign: function (url) {
throw new Error("window.location.assign should not be called when onRedirectNavigate returns false");
}
};

msal = new UserAgentApplication(config);
msal.handleRedirectCallback(authCallback);
msal.acquireTokenRedirect({
scopes: [ "user.read" ],
account,
onRedirectNavigate: url => {
expect(url).to.be.not.null;

done();
return false;
}
})
});

it("navigates if onRedirectNavigate returns null", done => {
const config: Configuration = {
auth: {
clientId: TEST_CONFIG.MSAL_CLIENT_ID
}
};

window.location = {
...oldWindowLocation,
hash: "",
assign: function (url) {
expect(url).to.not.be.null;
done();
}
};

msal = new UserAgentApplication(config);
msal.handleRedirectCallback(authCallback);
msal.loginRedirect({
onRedirectNavigate: url => {
expect(url).to.be.not.null;
}
})
});

it("navigates if onRedirectNavigate returns true", done => {
const config: Configuration = {
auth: {
clientId: TEST_CONFIG.MSAL_CLIENT_ID
}
};

window.location = {
...oldWindowLocation,
hash: "",
assign: function (url) {
expect(url).to.not.be.null;
done();
}
};

msal = new UserAgentApplication(config);
msal.handleRedirectCallback(authCallback);
msal.loginRedirect({
onRedirectNavigate: url => {
expect(url).to.be.not.null;

return true
}
})
});

it("exits login function with error if interaction is true", function (done) {
cacheStorage.setItem(TemporaryCacheKeys.INTERACTION_STATUS, Constants.inProgress);
window.location = oldWindowLocation;
Expand Down

0 comments on commit 5f9340a

Please sign in to comment.