Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
fix[okta-react]: Fixes login_required error (#302)
Browse files Browse the repository at this point in the history
The TokenManager throws an error when tries to renew a token but Okta session is expired.
The SDK should capture that error in the getAccessToken and getIdToken functions and return undefined instead.
In this way, also the isAuthenticated function will return false, so the router can correctly redirect to a new login.
  • Loading branch information
manueltanzi-okta authored Sep 21, 2018
1 parent eb33a6b commit dbfb7de
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
22 changes: 18 additions & 4 deletions packages/okta-react/src/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,27 @@ export default class Auth {
}

async getIdToken() {
const idToken = await this._oktaAuth.tokenManager.get('idToken');
return idToken ? idToken.idToken : undefined;
try {
const idToken = await this._oktaAuth.tokenManager.get('idToken');
return idToken.idToken;
} catch (err) {
// The user no longer has an existing SSO session in the browser.
// (OIDC error `login_required`)
// Ask the user to authenticate again.
return undefined;
}
}

async getAccessToken() {
const accessToken = await this._oktaAuth.tokenManager.get('accessToken');
return accessToken ? accessToken.accessToken : undefined;
try {
const accessToken = await this._oktaAuth.tokenManager.get('accessToken');
return accessToken.accessToken;
} catch (err) {
// The user no longer has an existing SSO session in the browser.
// (OIDC error `login_required`)
// Ask the user to authenticate again.
return undefined;
}
}

async login(fromUri, additionalParams) {
Expand Down
37 changes: 34 additions & 3 deletions packages/okta-react/test/jest/auth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,24 @@ const mockAuthJsInstance = {
}
};

AuthJS.mockImplementation(() => {
return mockAuthJsInstance
});
const mockAuthJsInstanceWithError = {
userAgent: 'okta-auth-js',
tokenManager: {
get: jest.fn().mockImplementation(() => {
throw new Error();
})
},
token: {
getWithRedirect: jest.fn()
}
};

describe('Auth component', () => {
beforeEach(() => {
AuthJS.mockImplementation(() => {
return mockAuthJsInstance
});
});
test('sets the right user agent on AuthJS', () => {
const auth = new Auth({
issuer: 'https://foo/oauth2/default'
Expand Down Expand Up @@ -122,4 +135,22 @@ describe('Auth component', () => {
foo: 'bar'
});
});
test('isAuthenticated() returns true when the TokenManager returns an access token', async () => {
const auth = new Auth({
issuer: 'https://foo/oauth2/default'
});
const authenticated = await auth.isAuthenticated();
expect(mockAuthJsInstance.tokenManager.get).toHaveBeenCalledWith('accessToken');
expect(authenticated).toBeTruthy();
});
test('isAuthenticated() returns false when the TokenManager does not return an access token', async () => {
AuthJS.mockImplementation(() => {
return mockAuthJsInstanceWithError
});
const auth = new Auth({
issuer: 'https://foo/oauth2/default'
});
const authenticated = await auth.isAuthenticated();
expect(authenticated).toBeFalsy();
});
});

0 comments on commit dbfb7de

Please sign in to comment.