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 callback to NatsAuthOpts that allows refreshing a Token #712

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

Conversation

garrett-sutton
Copy link

@garrett-sutton garrett-sutton commented Jan 13, 2025

Adds the following to NatsAuthOpts

  • Func<ValueTask<string>>? TokenHandler
  • Func<ValueTask<string>>? NKeyHandler
  • Func<ValueTask<string>>? JwtHandler
  • Func<ValueTask<string>>? SeedHandler

The purpose of this PR is to allow for use cases to refresh a Token, JWT, or NKey associated with their NatsConnection during a reconnect scenario.

resolves #356

@garrett-sutton garrett-sutton marked this pull request as ready for review January 13, 2025 23:18
@garrett-sutton
Copy link
Author

@mtmk are you the right person to review this? Or is there someone else that might be good to reach out to? Thanks!

@@ -31,6 +32,8 @@ public UserCredentials(NatsAuthOpts authOpts)

public string? Token { get; }

public Func<Task<string>>? TokenHandler { get; }
Copy link
Collaborator

Choose a reason for hiding this comment

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

to be consistent with other callbacks should we make this ValueTask. Can't remember if we also pass cancellation token. Also what about JWT? would that benefit from a callback like this?

Copy link
Author

Choose a reason for hiding this comment

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

I modified my initial commit to be a ValueTask rather than a Task: 21c6654
From what I can tell in other places, it doesn't appear that a cancellation token is passed.

In 58864dc, I added callbacks for JWT, NKey, and seed. Based on my understanding, if the JWT is being refreshed, the seed also needs to be refreshed. And the same goes with NKeys. I added unit tests to show that the callbacks will take precedence over any raw values that are set on initialization of NatsAuthOpts.

@mtmk
Copy link
Collaborator

mtmk commented Jan 14, 2025

@garrett-sutton could you sign your commits please?

@garrett-sutton garrett-sutton force-pushed the auth-callback branch 3 times, most recently from 34d3941 to 0bdb854 Compare January 14, 2025 23:02
@garrett-sutton
Copy link
Author

@garrett-sutton could you sign your commits please?

Done. As a note, this should probably be called out in CONTRIBUTING.md. I missed this initially because I was only looking there.

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.

Allow updating the Connection's token and/or JWT upon disconnect
2 participants