Skip to content

Add example how to use the AsyncAuthInterceptor #25620

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

Closed
wants to merge 4 commits into from

Conversation

danm-de
Copy link

@danm-de danm-de commented Apr 13, 2022

  • added example service registration that uses the AsyncAuthInterceptor to request a bearer token

Fixes grpc/grpc-dotnet#1682
Fixes #24132

* added example service registration that uses the AsyncAuthInterceptor to request a bearer token

See grpc/grpc-dotnet#1682
@serpent5 serpent5 requested a review from JamesNK April 13, 2022 11:52
@wadepickett wadepickett self-requested a review April 13, 2022 19:18
@wadepickett
Copy link
Contributor

@JamesNK, I can move this into a .NET 6.0 gRPC client factory app sample and then pull the relevant code snippets back into the topic if there is a version of this code sample you sign off on.

@danm-de, Thanks for taking the time and effort to provide a code example!

@@ -171,6 +171,43 @@ The preceeding code:
* Registers the `GreeterClient` type with client factory.
* Configures the `AuthInterceptor` for this client using `InterceptorScope.Client`. A new interceptor is created for each client instance. When a client is created for a gRPC service or Web API controller, the scoped `ITokenProvider` is injected into the interceptor.

##### Bearer token with gRPC client factory using AsyncAuthInterceptor
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll move the final version of this sample if there is one approved by JamesNK to a full client factory app sample for .NET 6 and then pull in the relevant snippets

var scopeFactory = sp.GetRequiredService<IServiceScopeFactory>();
var asyncAuthInterceptor = new AsyncAuthInterceptor(async (context, metadata) =>
{
using var scope = scopeFactory.CreateScope();
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, is creating a new scope here, and registering ITokenProvider with a scoped lifetime providing any benefit?

Each call is creating a new scope, and each time a new ITokenProvider instance is created. Is that much different from a transient lifetime? I haven't built a full gRPC app using scoped auth, but I would have thought the benefit that you're aiming for is caching the token per-request, so calls for that request share it. Will you get that here?

I wonder if there should be a new extension method for the gRPC client factory that is just for configuring AsyncAuthInterceptor. An overload can include an IServiceProvider that uses the current request scope.

Copy link
Author

Choose a reason for hiding this comment

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

I figured this would be necessary if ITokenProvider is registered as 'scoped' - or itself has dependencies registered as scoped services. If a background process opens an RPC connection (service-2-service communication) we don't necessarily have a scope - then there would be an exception here, right?

Copy link
Author

Choose a reason for hiding this comment

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

The example would be shorter and probably easier to understand if we say

[...] and has been registered as singleton service [...]

Copy link
Contributor

Choose a reason for hiding this comment

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

@JamesNK, it is not clear to me what state this is in at the moment. Does the example require a changed from what it is at this point?

danm-de and others added 3 commits April 14, 2022 10:52
Co-authored-by: James Newton-King <james@newtonking.com>
Co-authored-by: James Newton-King <james@newtonking.com>
Co-authored-by: James Newton-King <james@newtonking.com>
@JamesNK
Copy link
Member

JamesNK commented Apr 25, 2022

This docs PR has inspired some new features in the gRPC client factory. It should make this much easier. See grpc/grpc-dotnet#1705

Let's pause this PR and then adapt it to the new feature once it's merged.

@JamesNK
Copy link
Member

JamesNK commented Apr 25, 2022

@danm-de See #25734 for docs on a new auth feature that's being added to grpc-dotnet. It would replace this PR.

@JamesNK
Copy link
Member

JamesNK commented Apr 29, 2022

Thanks for working on this. A new feature and docs - #25734 - replaces this. Closing

@JamesNK JamesNK closed this Apr 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants