Apply the person token refresh to the request that triggered it - #42
Open
eastagiletracker wants to merge 2 commits into
Open
Apply the person token refresh to the request that triggered it#42eastagiletracker wants to merge 2 commits into
eastagiletracker wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR proposes applying the person token refresh to the request that triggered it, so a call made after the access token has expired goes out with the refreshed token instead of the expired one. We include this PR work along with a full history of your repo at https://eastagiletracker.com/projects/334. You can sign in with your GitHub ID to claim ownership of the project.
What's wrong
BearerTokenAPI.request()builds the outgoingRequestobject — including theAuthorizationheader, read straight offthis.token_set.access_token— and only afterwards checks whether the person token set needs refreshing. By the time the refresh runs and writes the new token back ontothis.token_set,reqhas already captured the old header, sofetch(req)still sends the expired access token and the call fails with an invalid-access-token error. The refreshed token only takes effect on the next request.That is the opposite of what the SDK promises. The note on
PersonTokenSetinsrc/types/common.tsreads "If you do not provide a refresh token the SDK will not be able to refresh the token for you and requests made an hour after the initial token exchange will fail" — i.e. supplying arefresh_tokenis supposed to keep requests working past the token lifetime. Today it does not: the first request after expiry fails anyway, and only a manual retry succeeds. EveryGraph,UserandAuditcall goes through this method, so it affects any integration using person token sets. Introduced in d4225db (reset the api file changes from prettier), where the refresh block ended up below theRequestconstruction.The change
The refresh check moves ahead of the header assignment, so the access token is read onto the request after any refresh has completed. The block itself is unchanged — same condition, same error handling, same token-set update — it only runs earlier, and no public API changed.
Verifying it
test/token_refresh.test.tsstubsglobal.fetchand asserts whichAuthorizationheader actually reaches the wire, so it needs no credentials and makes no network calls. It covers both branches ofrequest()(the string-URL path viamy.profile()and theRequestConfigbody path viasubmissions.update()), plus two controls that must not refresh: a person token set that has not expired, and an integration token set.On
mainat 9127e9f, two of the four fail:On this branch all four pass.
yarn buildandyarn testwere run onmainfirst and then here for comparison:build:coresucceeds in both,build:typesreports the same 50tscdiagnostics in both and every one of them is insidenode_modules/@types/node(none insrc), andtest/main.test.tsandtest/vanilla.test.jsfail identically in both because they needCLIENT_SECRET/INTEGRATION_ACCESS_TOKEN/REFRESH_TOKEN. Nothing that was passing changed, and the new suite is the only one that goes from red to green.One merge note: #38 also edits
src/types/common.ts, but for the request-options filter types — it does not touchrequest()or the files here, so the two should not conflict.How this was managed
This work was tracked as a story on a board imported from this repository's own issues and pull requests (37 stories, 5 labels): Person token refresh does not apply to the request that triggered it, on the board at eastagiletracker.com/projects/334.
If you'd rather not receive contributions like this, reply
no-more-prson this pull request and we won't open any further ones on your repositories.Lawrence W. Sinclair
CEO / East Agile
linkedin.com/in/lwsinclair/
eastagile.com