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

Changed refreshing federated token logic #797

Merged
merged 1 commit into from
May 7, 2018
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
22 changes: 21 additions & 1 deletion packages/aws-amplify/__tests__/Auth/auth-unit-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,26 @@ describe('auth unit test', () => {
spyon.mockClear();
});

test('with federated info and not expired, then refresh it successfully', async () => {
const auth = new Auth(authOptions);

const spyon = jest.spyOn(Cache, 'getItem')
.mockImplementationOnce(() => {
return {
provider: 'google',
token: 'token',
expires_at: (new Date().getTime()) * 2
}
});

auth._refreshHandlers = {}

expect.assertions(1);
expect(await auth.currentUserCredentials()).not.toBeUndefined();

spyon.mockClear();
});

test('with federated info and expired, then refresh it successfully', async () => {
const auth = new Auth(authOptions);

Expand All @@ -1105,7 +1125,7 @@ describe('auth unit test', () => {
}
}
expect.assertions(1);
expect(await auth.currentUserCredentials()).not.toBeUndefined();
expect(await auth.currentUserCredentials()).not.toBeUndefined();

spyon.mockClear();
});
Expand Down
45 changes: 23 additions & 22 deletions packages/aws-amplify/src/Auth/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -857,30 +857,31 @@ export default class AuthClass {

const that = this;
logger.debug('checking if federated jwt token expired');
if (expires_at < new Date().getTime()
&& typeof that._refreshHandlers[provider] === 'function') {
logger.debug('getting refreshed jwt token from federation provider');
return that._refreshHandlers[provider]().then((data) => {
logger.debug('refresh federated token sucessfully', data);
token = data.token;
identity_id = data.identity_id;
expires_at = data.expires_at;
// Cache.setItem('federatedInfo', { provider, token, user, expires_at }, { priority: 1 });
return that._setCredentialsFromFederation({ provider, token, user, identity_id, expires_at });
}).catch(e => {
logger.debug('refresh federated token failed', e);
this.cleanCachedItems();
return Promise.reject('refreshing federation token failed: ' + e);
});
} else {
if (!that._refreshHandlers[provider]) {
logger.debug('no refresh handler for provider:', provider);
this.cleanCachedItems();
return Promise.reject('no refresh handler for provider');
if (expires_at < new Date().getTime()) {
if (typeof that._refreshHandlers[provider] === 'function') {
logger.debug('getting refreshed jwt token from federation provider');
return that._refreshHandlers[provider]().then((data) => {
logger.debug('refresh federated token sucessfully', data);
token = data.token;
identity_id = data.identity_id;
expires_at = data.expires_at;
// Cache.setItem('federatedInfo', { provider, token, user, expires_at }, { priority: 1 });
return that._setCredentialsFromFederation({provider, token, user, identity_id, expires_at});
}).catch(e => {
logger.debug('refresh federated token failed', e);
this.cleanCachedItems();
return Promise.reject('refreshing federation token failed: ' + e);
});
} else {
logger.debug('token not expired');
return this._setCredentialsFromFederation({provider, token, user, identity_id, expires_at });
if (!that._refreshHandlers[provider]) {
logger.debug('no refresh handler for provider:', provider);
this.cleanCachedItems();
return Promise.reject('no refresh handler for provider');
}
}
} else {
logger.debug('token not expired');
return this._setCredentialsFromFederation({provider, token, user, identity_id, expires_at});
}
}

Expand Down