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

Check redirect URI in token.isLoginRedirect() #520

Merged
merged 2 commits into from
Oct 26, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion lib/oauthUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ function hasCodeInUrl(hashOrSearch: string): boolean {
return /(code=)/i.test(hashOrSearch);
}

function isRedirectUri(uri: string, sdk: OktaAuth): boolean {
var authParams = sdk.options;
return uri && uri.indexOf(authParams.redirectUri) == 0;
}

/**
* Check if tokens or a code have been passed back into the url, which happens in
* the social auth IDP redirect flow.
Expand All @@ -239,9 +244,10 @@ function isLoginRedirect (sdk: OktaAuth) {
var authParams = sdk.options;
if (authParams.pkce || authParams.responseType === 'code' || authParams.responseMode === 'query') {
// Look for code
return authParams.responseMode === 'fragment' ?
var hasCode = authParams.responseMode === 'fragment' ?
hasCodeInUrl(window.location.hash) :
hasCodeInUrl(window.location.search);
return isRedirectUri(window.location.href, sdk) && hasCode;
}
// Look for tokens (Implicit OIDC flow)
return hasTokensInHash(window.location.hash);
Expand Down
34 changes: 28 additions & 6 deletions test/spec/oauthUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -963,13 +963,15 @@ describe('isLoginRedirect', function() {
it('there should be code in hash when responseMode is fragment', () => {
delete window.location;
window.location = {
hash: '#code=fakecode'
hash: '#code=fakecode',
href: 'https://exmple.com/implicit/callback#code=fakecode'
};
sdk = new OktaAuth({
pkce: true,
responseMode: 'fragment',
issuer: 'https://auth-js-test.okta.com',
clientId: 'foo'
clientId: 'foo',
redirectUri: 'https://exmple.com/implicit/callback'
});
const result = oauthUtil.isLoginRedirect(sdk);
expect(result).toBe(true);
Expand All @@ -978,12 +980,14 @@ describe('isLoginRedirect', function() {
it('there should be code in query when use default responseMode', () => {
delete window.location;
window.location = {
search: '?code=fakecode'
search: '?code=fakecode',
href: 'https://exmple.com/implicit/callback?code=fakecode'
};
sdk = new OktaAuth({
pkce: true,
issuer: 'https://auth-js-test.okta.com',
clientId: 'foo'
clientId: 'foo',
redirectUri: 'https://exmple.com/implicit/callback'
});
const result = oauthUtil.isLoginRedirect(sdk);
expect(result).toBe(true);
Expand All @@ -992,16 +996,34 @@ describe('isLoginRedirect', function() {
it('there should be code in query when responseMode is query', () => {
delete window.location;
window.location = {
search: '?code=fakecode'
search: '?code=fakecode',
href: 'https://exmple.com/implicit/callback?code=fakecode'
};
sdk = new OktaAuth({
pkce: true,
responseMode: 'query',
issuer: 'https://auth-js-test.okta.com',
clientId: 'foo'
clientId: 'foo',
redirectUri: 'https://exmple.com/implicit/callback'
});
const result = oauthUtil.isLoginRedirect(sdk);
expect(result).toBe(true);
});

it('should return false if current URI is not redirect URI', () => {
delete window.location;
window.location = {
search: '?code=somecode',
href: 'https://exmple.com/products/search?code=somecode'
};
sdk = new OktaAuth({
pkce: true,
issuer: 'https://auth-js-test.okta.com',
clientId: 'foo',
redirectUri: 'https://exmple.com/implicit/callback'
});
const result = oauthUtil.isLoginRedirect(sdk);
expect(result).toBe(false);
});
});
});
18 changes: 12 additions & 6 deletions test/spec/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -877,12 +877,14 @@ describe('token.getWithoutPrompt', function() {
global.window.location = {
protocol: 'https:',
hostname: 'somesite.local',
search: '?code=fakecode'
search: '?code=fakecode',
href: 'https://somesite.local/implicit/callback?code=fakecode'
};
const client = new OktaAuth({
pkce: true,
issuer: 'https://auth-js-test.okta.com',
clientId: 'foo'
clientId: 'foo',
redirectUri: 'https://somesite.local/implicit/callback'
});

try {
Expand Down Expand Up @@ -1373,12 +1375,14 @@ describe('token.getWithPopup', function() {
global.window.location = {
protocol: 'https:',
hostname: 'somesite.local',
search: '?code=fakecode'
search: '?code=fakecode',
href: 'https://somesite.local/implicit/callback?code=fakecode'
};
const client = new OktaAuth({
pkce: true,
issuer: 'https://auth-js-test.okta.com',
clientId: 'foo'
clientId: 'foo',
redirectUri: 'https://somesite.local/implicit/callback'
});

try {
Expand Down Expand Up @@ -2186,12 +2190,14 @@ describe('token.getWithRedirect', function() {
global.window.location = {
protocol: 'https:',
hostname: 'somesite.local',
search: '?code=fakecode'
search: '?code=fakecode',
href: 'https://somesite.local/implicit/callback?code=fakecode'
};
const client = new OktaAuth({
pkce: true,
issuer: 'https://auth-js-test.okta.com',
clientId: 'foo'
clientId: 'foo',
redirectUri: 'https://somesite.local/implicit/callback'
});

try {
Expand Down