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

[SDK-1877] Add refresh method to access token #124

Merged
merged 4 commits into from
Aug 10, 2020
Merged

Conversation

adamjmcgrath
Copy link
Contributor

Description

You should be able to use the openid-client to refresh an access token.

let accessToken = req.oidc.accessToken;
if (accessToken.isExpired()) {
  accessToken = await accessToken.refresh();
}

Also, add fetchUserInfo for completeness

const userInfo = await req.oidc.fetchUserInfo();

References

https://github.com/panva/node-openid-client/blob/master/docs/README.md#clientrefreshrefreshtoken-extras
https://github.com/panva/node-openid-client/blob/master/docs/README.md#clientuserinfoaccesstoken-options

Testing

  • This change adds test coverage for new/changed/fixed functionality

Checklist

  • I have added documentation for new/changed functionality in this PR or in auth0.com/docs
  • All active GitHub checks for tests, formatting, and security are passing
  • The correct base branch is being used, if not master

@adamjmcgrath adamjmcgrath added the review:medium Medium review label Aug 10, 2020
@adamjmcgrath adamjmcgrath requested review from panva and a team August 10, 2020 14:11
lib/context.js Outdated
Comment on lines 21 to 22
const oldTokenSet = tokenSet.call(this);
const newTokenSet = await client.refresh(oldTokenSet, opts);
Copy link
Contributor

Choose a reason for hiding this comment

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

It is entirely possible that a refresh() will only yield the access token and no refresh_token. We should look for that specific scenario and keep the old refresh token value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, thanks!

index.d.ts Outdated
Comment on lines 113 to 118
fetchUserInfo(options?: {
verb?: 'GET' | 'POST';
via?: 'header' | 'body' | 'query';
tokenType?: string;
params?: object;
}): Promise<UserinfoResponse>;
Copy link
Contributor

Choose a reason for hiding this comment

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

my recommendation: don't expose any of these options. The default is the REQUIRED to implement for all providers that support the userinfo endpoint.

lib/context.js Outdated
@@ -112,6 +127,13 @@ class RequestContext {
return undefined;
}
}

async fetchUserInfo(opts) {
Copy link
Contributor

Choose a reason for hiding this comment

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

my recommendation: don't expose the options. The default is the REQUIRED to implement for all providers that support the userinfo endpoint.

index.d.ts Outdated
*
* @param opts Add extra parameters to the Token Endpoint Request and/or Client Authentication JWT Assertion
*/
refresh(opts?: RefreshExtras): Promise<AccessToken>;
Copy link
Contributor

Choose a reason for hiding this comment

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

omit the options.

lib/context.js Outdated
@@ -15,6 +15,20 @@ function isExpired() {
return tokenSet.call(this).expired();
}

async function refresh(opts) {
Copy link
Contributor

Choose a reason for hiding this comment

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

omit the options.

@adamjmcgrath adamjmcgrath requested a review from panva August 10, 2020 16:08
@adamjmcgrath adamjmcgrath merged commit caf1de4 into beta-2 Aug 10, 2020
@adamjmcgrath adamjmcgrath deleted the refresh-token branch August 10, 2020 17:02
adamjmcgrath added a commit that referenced this pull request Aug 13, 2020
* Add refresh method to access token

* Add method to fetch userinfo

* Add refresh example to docs

* Not all refresh token grants get a new refresh token back (eg non rotating) and remove unneeded opts
adamjmcgrath added a commit that referenced this pull request Sep 17, 2020
* Update beta version

* Initial commit of Beta 2 branch

Co-authored-by: Filip Skokan <[email protected]>

* Add PKCE tests and fix multiple servers in tests (#110)

* [SDK-1714] [SDK-1723] Session cookie checks (#111)

* Refactor the tests a little to use a server
* simplify test
* tests for unordered cookie chunks
* Test format changes for transient handler
* Update lib/transientHandler.js

Co-authored-by: Filip Skokan <[email protected]>

* [SDK-1715] Configuration and API updates (#109)

* Simplify the config tests, test more with less code
* Validate config fixes and tests
* Add comment, update tests
* `Issuer.discover` only takes a fully qualified url
* Simpler scope assertion and keep all config tests in same file
* Let auth server set default for response_type: code
* clientSecret is required for HS* algs regardless of response_type

Co-authored-by: Filip Skokan <[email protected]>

* [SDK-1712] Test token set (#108)

* Add tests for TokenSet
* Refactor the tests a little
* Split up code flow tests
* Add tests for access token expiry

* Add Prettier for style formatting (#112)

* Add prettier

* Run `prettier --write .`

* [SDK-1716] Add tests for claim* MW (#113)

* Add tests for claim* MW

* Fix tests

* Add some tests for session duration behaviour

* Revert "Add some tests for session duration behaviour"

This reverts commit e3ce510.

* Add some tests for session duration behaviour (#114)

* Add test for passing custom param to logout (#115)

* [SDK-1721] Auto generated docs (#117)

* Auto generated docs with typedoc

* fix lgtm

* Fix auth params

* Fix incorrect import of `requiresAuth` (#118)

* Add TROUBLESHOOTING and update debug logging (#120)

* Add TROUBLESHOOTING and update debug logging

* fix tests

* Update lib/context.js

Co-authored-by: Filip Skokan <[email protected]>

Co-authored-by: Filip Skokan <[email protected]>

* attemptSilentLogin feature (#121)

* attemptSilentLogin feature

* Resume silent login after successful login so that users try silent login again after their session's expire

* `postLogoutRedirectUri` isn't always a URI and login needs a check for `response_type` (#123)

* [SDK-1877] Add refresh method to access token (#124)

* Add refresh method to access token

* Add method to fetch userinfo

* Add refresh example to docs

* Not all refresh token grants get a new refresh token back (eg non rotating) and remove unneeded opts

* Scope all cookies (skipSilentLogin, transient and appSession) to the app session cookie path and domain config (if specified) (#125)

* [SDK-1722] Architecture (#128)

* Default Login flow docs

* Add logout

* add link from readme

* [SDK-1876] [SDK-1726] Add samples and smoke tests (#127)

* Add samples and smoke tests

* Fix CI

* Make the test clearer that discovery alg is ignored (#130)

* Make the test clearer that discovery alg is ignored

* Add test to show "none" disallowed for idTokenSigningAlg

* Disallow "none" in any case (#131)

* Disallow "none" in any case

* Fix puppeteer in CircleCI https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-puppeteer-on-circleci

* Revert "Fix puppeteer in CircleCI https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#running-puppeteer-on-circleci"

This reverts commit c0fd31e

* use active lts

* [SDK-1914] Add a migration guide for v1 to v2 (#129)

* Add a migration guide for v1 to v2

* Apply suggestions from code review

Co-authored-by: Filip Skokan <[email protected]>

* Updates per PR comments

Co-authored-by: Filip Skokan <[email protected]>

* Release 2.0.0-beta.0 (#132)

* Release 2.0.0-beta.0

* Ignore docs from lint

* Fixes the numbering on examples (#136)

* chore: update jose and openid-client (#134)

Co-authored-by: Filip Skokan <[email protected]>
Co-authored-by: David Patrick <[email protected]>
@abinashdatta
Copy link

How we can refresh the token without deleting the current session. I am getting an issue like after every refresh auth code flow is re-invoked.

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

Successfully merging this pull request may close these issues.

3 participants