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 early expiration of Access Token in cache #233

Merged
merged 2 commits into from
Oct 3, 2019
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: 6 additions & 2 deletions __tests__/cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ describe('cache', () => {
user: { name: 'Test' }
}
});
jest.advanceTimersByTime(1001);
jest.advanceTimersByTime(799);
expect(Object.keys(cache.cache).length).toBe(1);
jest.advanceTimersByTime(1);
expect(Object.keys(cache.cache).length).toBe(0);
});
it('expires after `user.exp` when `user.exp` < `expires_in`', () => {
Expand All @@ -73,7 +75,9 @@ describe('cache', () => {
user: { name: 'Test' }
}
});
jest.advanceTimersByTime(1001);
jest.advanceTimersByTime(799);
expect(Object.keys(cache.cache).length).toBe(1);
jest.advanceTimersByTime(1);
expect(Object.keys(cache.cache).length).toBe(0);
});
});
2 changes: 1 addition & 1 deletion src/cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const createKey = (e: CacheKeyData) => `${e.audience}::${e.scope}`;
const getExpirationTimeoutInMilliseconds = (expiresIn: number, exp: number) => {
const expTime =
(new Date(exp * 1000).getTime() - new Date().getTime()) / 1000;
return Math.min(expiresIn, expTime) * 1000;
return Math.min(expiresIn, expTime) * 1000 * 0.8;
};

export default class Cache {
Expand Down