Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add onRedirectNavigate callback to stop navigatation and get redirect url #1691

Merged
merged 4 commits into from
Jun 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -602,8 +602,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