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 CancellationToken support for GoogleJsonWebSignature #2672

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

Conversation

gunsh
Copy link
Contributor

@gunsh gunsh commented Feb 12, 2024

GoogleJsonWebSignature features async methods but offer no CancellationSupport. Async methods are now updated to expose a CancellationToken.

@gunsh gunsh requested a review from a team as a code owner February 12, 2024 16:12
@jskeet
Copy link
Collaborator

jskeet commented Feb 12, 2024

This is a (binary) breaking change - old compiled code won't find the new method signatures. Overloads can be added, but will need to be added with care to avoid creating ambiguous calls.

Copy link
Contributor

@amanda-tarafa amanda-tarafa left a comment

Choose a reason for hiding this comment

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

@gunsh As @jskeet mentioned, this is a binary breaking change so we cannot accept this PR as is. You can add overloads to both public methods, that receive the cancellation token, or you can maybe add a cancellation token on ValidationSettings. That will cover the public method that receives the ValidationSettings and I think we could leave at that, adding a note to the ValidateAsync(string jwt, IClock clock, bool forceGoogleCertRefresh) existing overload with something like "If you want to use a cancellation token you can use the overload that accepts ValidationSettings. ValidationSettings accepts a cancellation token".

I'd prefer this last option over adding overloads.

@amanda-tarafa amanda-tarafa added the needs more info This issue needs more information from the customer to proceed. label Feb 12, 2024
@gunsh
Copy link
Contributor Author

gunsh commented Feb 20, 2024

@amanda-tarafa I have submitted a different set of changes that will hopefully not affect binary compatibility. Please note that two methods were affected (just like the first time). Both issues were solved with an overload. I understand this was not your preference but having CancellationToken buried inside ValidationSettings is just not intuitive.

/// <param name="cancellationToken">Cancellation token</param>
/// <returns>The JWT payload, if the JWT is valid. Throws an <see cref="InvalidJwtException"/> otherwise.</returns>
/// <exception cref="InvalidJwtException">Thrown when passed a JWT that is not a valid JWT signed by Google.</exception>
public static Task<Payload> ValidateWithCancellationAsync(string jwt, IClock clock = null, bool forceGoogleCertRefresh = false, CancellationToken cancellationToken = default) =>
Copy link
Contributor

Choose a reason for hiding this comment

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

The problem with this naming pattern is that it can explode very quickly. Next option we have to add is then ValidateWithCancellationAndOptionAsync, etc.
So, let's not add this method and only add the overload ValidateAsync(string, ValidationSettings, CancellationToken) then ValidateAsync(string, IClock, bool) can stay for backward compatibility as a very specific shortcut (to not having to create settings).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Alright. Removed.

// internal for testing
internal static async Task<Payload> ValidateInternalAsync(string jwt, ValidationSettings validationSettings)
internal static async Task<Payload> ValidateInternalAsync(string jwt, ValidationSettings validationSettings, CancellationToken cancellationToken = default)
Copy link
Contributor

Choose a reason for hiding this comment

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

Better to be explicit in our own internal code.

Suggested change
internal static async Task<Payload> ValidateInternalAsync(string jwt, ValidationSettings validationSettings, CancellationToken cancellationToken = default)
internal static async Task<Payload> ValidateInternalAsync(string jwt, ValidationSettings validationSettings, CancellationToken cancellationToken)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After removing the default value I had to create a version of ValidateInternalAsync without a token in order for the tests to build. An alternative would be to alter all tests.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes please, alter all the tests. Thanks!

Add ValidateInternalAsync in order to avoid CancellationToken with a default value.
@amanda-tarafa amanda-tarafa self-assigned this Feb 20, 2024
@amanda-tarafa amanda-tarafa removed the needs more info This issue needs more information from the customer to proceed. label Feb 20, 2024
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.

3 participants