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

fix: TokenManager.renew(key) should renew only requested token #656

Closed
wants to merge 7 commits into from

Conversation

denysoblohin-okta
Copy link
Contributor

@denysoblohin-okta denysoblohin-okta commented Mar 11, 2021

Current implementation of TokenManager.renew refreshes access token and id token together if user not specified responseType in OktaAuth config (as in #559 - responseType was used only in getWithoutPrompt and not in OktaAuth contructor).

const { responseType } = getDefaultTokenParams(sdk);

If user uses multiple access tokens with multiple keys (as in issue #559), TokenManager.renew will always use key of first access token in token storage to update any access token with new value:

emitRenewed(tokenMgmtRef, accessTokenKey, accessToken, oldTokenStorage[accessTokenKey]),

function getKeyByType(storage, type: TokenType): string {

This will lead to a problem when expired access token will stay in storage. And on resetExpireEventTimeoutAll timer to update will fire again

for(var key in tokenStorage) {

Internal ref: OKTA-352791
Issue: #559

@denysoblohin-okta denysoblohin-okta changed the title fix: TokenManager.renew(key) should not renew all tokens fix: TokenManager.renew(key) should renew only requested token Mar 12, 2021
@denysoblohin-okta denysoblohin-okta marked this pull request as ready for review March 12, 2021 15:20
})
.then(function(freshTokens) {
// store and emit events for freshTokens
tokenMgmtRef.renewPromise[key] = sdk.token.renew(token)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sdk.token.renew (oidc/renewToken) only handles renew in the hidden iframe way. This change should still keep the renew with refresh approach.

@codecov-io
Copy link

codecov-io commented Mar 13, 2021

Codecov Report

Merging #656 (e5a8d01) into master (a81cd6d) will increase coverage by 0.15%.
The diff coverage is 90.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #656      +/-   ##
==========================================
+ Coverage   92.72%   92.88%   +0.15%     
==========================================
  Files          67       67              
  Lines        2447     2599     +152     
  Branches      517      559      +42     
==========================================
+ Hits         2269     2414     +145     
- Misses        178      185       +7     
Impacted Files Coverage Δ
lib/TokenManager.ts 96.25% <89.28%> (-1.48%) ⬇️
lib/browser/browser.ts 93.29% <100.00%> (+2.38%) ⬆️
lib/oidc/index.ts 100.00% <100.00%> (ø)
lib/OktaAuthBase.ts 88.88% <0.00%> (-1.12%) ⬇️
lib/oidc/getWithPopup.ts 100.00% <0.00%> (ø)
lib/oidc/getWithRedirect.ts 100.00% <0.00%> (ø)
lib/oidc/getWithoutPrompt.ts 100.00% <0.00%> (ø)
lib/oidc/util/defaultTokenParams.ts 100.00% <0.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a81cd6d...e5a8d01. Read the comment docs.

issuer
})

// If we have a refresh token, renew using that, otherwise getWithoutPrompt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense to add a helper renew function in TokenManager to make the flows check (it's actually looks like a manager function).

Also, as we expose the renew functions from the top-level (browser.ts), behavior change in the lower-level functions looks like a breaking change to me (changes the public API authClient.token.renew, which should use getWithoutPrompt only).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved renew token with refresh token flow to TokenManager

@denysoblohin-okta
Copy link
Contributor Author

@shuowu What do you think on the latest change?
I've moved renew token (with or without refresh token) logic to helper function _renewToken in TokenManager.
I've exposed renewTokensWithRefresh to Token API, to be able to use it in TokenManager (maybe should add _ prefix?).

@@ -50,6 +50,7 @@ import {
revokeToken,
renewToken,
renewTokens,
renewTokensWithRefresh,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this method does not exist anymore, remove?

@@ -213,6 +214,7 @@ class OktaAuthBrowser extends OktaAuthBase implements OktaAuth, SignoutAPI {
decode: decodeToken,
revoke: revokeToken.bind(null, this),
renew: renewToken.bind(null, this),
renewTokensWithRefresh: renewTokensWithRefresh.bind(null, this),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

@@ -16,6 +16,7 @@ export * from './util';
export { decodeToken } from './decodeToken';
export { revokeToken } from './revokeToken';
export { renewToken } from './renewToken';
export { renewTokensWithRefresh } from './renewTokensWithRefresh';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

@@ -148,6 +148,7 @@ export interface TokenAPI extends BaseTokenAPI {
revoke(token: RevocableToken): Promise<object>;
renew(token: Token): Promise<Token>;
renewTokens(): Promise<Tokens>;
renewTokensWithRefresh(refreshTokenObject: RefreshToken): Promise<Tokens>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove?

@shuowu-okta
Copy link
Contributor

We don't need to add renewTokensWithRefresh to the top level api, otherwise LGTM.

CHANGELOG.md Outdated
@@ -1,5 +1,11 @@
# Changelog

## 4.8.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make the change as 4.9.0 (minor), since it adds new top level API.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants