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

Setting Store Tokens should only clear tokens that no longer have a value #13584

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

jon-armen
Copy link

Description of changes

Setting Store Tokens should only clear tokens that no longer have a value. This resolves thrashing events when a consumer is listening to storage events, preventing them from seeing a 'clear' followed immediately by a 'set' value.

Issue #, if available

#13583

Checklist

  • PR description included
  • yarn test passes
  • Unit Tests are changed or added
  • Relevant documentation is changed or added (and PR referenced)

Checklist for repo maintainers

  • Verify E2E tests for existing workflows are working as expected or add E2E tests for newly added workflows
  • New source file paths included in this PR have been added to CODEOWNERS, if appropriate

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

…alue. This resolves thrashing events when a consumer is listening to storage events, preventing them from seeing a 'clear' followed immediatly by a 'set' value.
@cwomack
Copy link
Member

cwomack commented Jul 15, 2024

Hello, @jon-armen and thanks for taking the time to submit this PR! We'll get this reviewed by the team internally and follow up with any questions or feedback that we have.

@itsramiel itsramiel mentioned this pull request Dec 6, 2024
3 tasks
@itsramiel
Copy link
Contributor

@cwomack Can you please bring life to this pr? I believe such changes can also fix #14033

@joon-won
Copy link
Member

Thanks for working on this issue @jon-armen, could you also add some tests for the behavior?

@jon-armen
Copy link
Author

@joon-won I'll see if I can get some tests together in the next few days. @itsramiel , do you have thoughts on tests you might like to see to validate your use case?

@itsramiel
Copy link
Contributor

@joon-won I'll see if I can get some tests together in the next few days. @itsramiel , do you have thoughts on tests you might like to see to validate your use case?

One way that comes to mind is to call storeTokens, followed by loadTokens without awaiting them, but making sure both settled, given that a loadTokens was called before to make sure there were tokens in the first place:

    // Start storeTokens and loadTokens simultaneously
    const storePromise = tokenStore.storeTokens(tokens);
    const loadPromise = tokenStore.loadTokens();

    const [storeResult, loadResult] = await Promise.allSettled([
      storePromise,
      loadPromise,
    ]);

However I am not sure this will work always, half of the times, or never in tests since it really depends on how the js env handles async calls and running other functions while sync calls are awaited. But that would be my approach

Copy link
Contributor

Choose a reason for hiding this comment

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

Typewise, tokens.username, tokens.accessToken and tokens.clockDrift are non optional types so technically no need for the if...else on line 100, 110, and 165 since also in the before changes they were just being set.

Maybe we can remove the if...else from those ones?

Copy link
Author

Choose a reason for hiding this comment

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

I've removed the extra if/else statements.

);
} else {
await this.getKeyValueStorage().removeItem(authKeys.clockDrift);
}
}

Choose a reason for hiding this comment

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

do we need a statement to removeItem(authKeys.oauthMetdata)?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, good catch. I don't think that was in tokens when I put this together a few months ago.

Copy link
Member

Choose a reason for hiding this comment

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

Don't think we should set/remove oauthMetdata here. It's separately managed on line 243.

Copy link
Member

@joon-won joon-won Dec 23, 2024

Choose a reason for hiding this comment

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

Regarding oauthMetadata, I agree with @Samaritan1011001. Do we know if we need to set/clear it in storeTokens here?

@@ -95,31 +95,43 @@ export class DefaultTokenStore implements AuthTokenStore {

async storeTokens(tokens: CognitoAuthTokens): Promise<void> {
assert(tokens !== undefined, TokenProviderErrorCode.InvalidAuthTokens);
await this.clearTokens();

Choose a reason for hiding this comment

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

we should assert that in the absence of this all keys in https://github.com/aws-amplify/amplify-js/blob/main/packages/auth/src/providers/cognito/tokenProvider/types.ts#L27 are cleared if unspecified.

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.

7 participants