| | | 1 | | #nullable enable |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Etcdserverpb; |
| | | 6 | | using Grpc.Core; |
| | | 7 | | using Grpc.Core.Interceptors; |
| | | 8 | | |
| | | 9 | | namespace dotnet_etcd; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Interceptor that automatically adds authentication tokens to all gRPC calls |
| | | 13 | | /// </summary> |
| | | 14 | | internal class AuthenticationInterceptor : Interceptor |
| | | 15 | | { |
| | | 16 | | private const string AuthorizationHeader = "authorization"; |
| | | 17 | | |
| | | 18 | | private readonly Func<string?> _getToken; |
| | | 19 | | |
| | 83 | 20 | | public AuthenticationInterceptor(Func<string?> getToken) |
| | 83 | 21 | | { |
| | 83 | 22 | | _getToken = getToken ?? throw new ArgumentNullException(nameof(getToken)); |
| | 82 | 23 | | } |
| | | 24 | | |
| | | 25 | | public override AsyncUnaryCall<TResponse> AsyncUnaryCall<TRequest, TResponse>( |
| | | 26 | | TRequest request, |
| | | 27 | | ClientInterceptorContext<TRequest, TResponse> context, |
| | | 28 | | AsyncUnaryCallContinuation<TRequest, TResponse> continuation) |
| | 3 | 29 | | { |
| | 3 | 30 | | var headers = AddAuthToken(context.Options.Headers); |
| | 3 | 31 | | var newOptions = context.Options.WithHeaders(headers); |
| | 3 | 32 | | var newContext = new ClientInterceptorContext<TRequest, TResponse>( |
| | 3 | 33 | | context.Method, context.Host, newOptions); |
| | | 34 | | |
| | 3 | 35 | | return continuation(request, newContext); |
| | 3 | 36 | | } |
| | | 37 | | |
| | | 38 | | public override AsyncClientStreamingCall<TRequest, TResponse> AsyncClientStreamingCall<TRequest, TResponse>( |
| | | 39 | | ClientInterceptorContext<TRequest, TResponse> context, |
| | | 40 | | AsyncClientStreamingCallContinuation<TRequest, TResponse> continuation) |
| | 2 | 41 | | { |
| | 2 | 42 | | var headers = AddAuthToken(context.Options.Headers); |
| | 2 | 43 | | var newOptions = context.Options.WithHeaders(headers); |
| | 2 | 44 | | var newContext = new ClientInterceptorContext<TRequest, TResponse>( |
| | 2 | 45 | | context.Method, context.Host, newOptions); |
| | | 46 | | |
| | 2 | 47 | | return continuation(newContext); |
| | 2 | 48 | | } |
| | | 49 | | |
| | | 50 | | public override AsyncServerStreamingCall<TResponse> AsyncServerStreamingCall<TRequest, TResponse>( |
| | | 51 | | TRequest request, |
| | | 52 | | ClientInterceptorContext<TRequest, TResponse> context, |
| | | 53 | | AsyncServerStreamingCallContinuation<TRequest, TResponse> continuation) |
| | 2 | 54 | | { |
| | 2 | 55 | | var headers = AddAuthToken(context.Options.Headers); |
| | 2 | 56 | | var newOptions = context.Options.WithHeaders(headers); |
| | 2 | 57 | | var newContext = new ClientInterceptorContext<TRequest, TResponse>( |
| | 2 | 58 | | context.Method, context.Host, newOptions); |
| | | 59 | | |
| | 2 | 60 | | return continuation(request, newContext); |
| | 2 | 61 | | } |
| | | 62 | | |
| | | 63 | | public override AsyncDuplexStreamingCall<TRequest, TResponse> AsyncDuplexStreamingCall<TRequest, TResponse>( |
| | | 64 | | ClientInterceptorContext<TRequest, TResponse> context, |
| | | 65 | | AsyncDuplexStreamingCallContinuation<TRequest, TResponse> continuation) |
| | 2 | 66 | | { |
| | 2 | 67 | | var headers = AddAuthToken(context.Options.Headers); |
| | 2 | 68 | | var newOptions = context.Options.WithHeaders(headers); |
| | 2 | 69 | | var newContext = new ClientInterceptorContext<TRequest, TResponse>( |
| | 2 | 70 | | context.Method, context.Host, newOptions); |
| | | 71 | | |
| | 2 | 72 | | return continuation(newContext); |
| | 2 | 73 | | } |
| | | 74 | | |
| | | 75 | | public override TResponse BlockingUnaryCall<TRequest, TResponse>( |
| | | 76 | | TRequest request, |
| | | 77 | | ClientInterceptorContext<TRequest, TResponse> context, |
| | | 78 | | BlockingUnaryCallContinuation<TRequest, TResponse> continuation) |
| | 10 | 79 | | { |
| | 10 | 80 | | var headers = AddAuthToken(context.Options.Headers); |
| | 10 | 81 | | var newOptions = context.Options.WithHeaders(headers); |
| | 10 | 82 | | var newContext = new ClientInterceptorContext<TRequest, TResponse>( |
| | 10 | 83 | | context.Method, context.Host, newOptions); |
| | | 84 | | |
| | 10 | 85 | | return continuation(request, newContext); |
| | 10 | 86 | | } |
| | | 87 | | |
| | | 88 | | private Metadata AddAuthToken(Metadata? headers) |
| | 19 | 89 | | { |
| | | 90 | | // Create new metadata or use existing |
| | 19 | 91 | | var newHeaders = headers == null ? new Metadata() : new Metadata(); |
| | | 92 | | |
| | | 93 | | // Preserve all existing headers (except Authorization, which we'll replace if needed) |
| | 19 | 94 | | if (headers != null) |
| | 4 | 95 | | { |
| | 28 | 96 | | foreach (var entry in headers) |
| | 8 | 97 | | { |
| | | 98 | | // Skip authorization header - we'll add it if needed |
| | 8 | 99 | | if (!string.Equals(entry.Key, AuthorizationHeader, StringComparison.OrdinalIgnoreCase)) |
| | 5 | 100 | | { |
| | 5 | 101 | | newHeaders.Add(entry); |
| | 5 | 102 | | } |
| | 8 | 103 | | } |
| | 4 | 104 | | } |
| | | 105 | | |
| | | 106 | | // Get token from provider (this is now just a simple field read, no method calls) |
| | 19 | 107 | | var token = _getToken?.Invoke(); |
| | | 108 | | |
| | | 109 | | // Add the authorization header with the token (only if it's not empty or whitespace) |
| | 19 | 110 | | if (!string.IsNullOrWhiteSpace(token)) |
| | 12 | 111 | | { |
| | 12 | 112 | | newHeaders.Add(AuthorizationHeader, token); |
| | 12 | 113 | | } |
| | | 114 | | |
| | 19 | 115 | | return newHeaders; |
| | 19 | 116 | | } |
| | | 117 | | } |