diff options
author | Michael Lumish <mlumish@google.com> | 2015-07-27 10:13:04 -0700 |
---|---|---|
committer | Michael Lumish <mlumish@google.com> | 2015-07-27 10:13:04 -0700 |
commit | 1f21f336ebe032e7dff185d551a3f48c8dcd4b5b (patch) | |
tree | 7c5552b29bf562b9eb4706e5e159000e1e4a3d96 /src/csharp | |
parent | d116a2d3a9cb8d44c6190f875835f6c87001f3ce (diff) | |
parent | cd7a4054de7b77a425b544fa8b3f424b9fede142 (diff) |
Merge pull request #2666 from jtattermusch/oauth_token_api_fix
Make work with out-of-band oauth2 token more pleasant
Diffstat (limited to 'src/csharp')
-rw-r--r-- | src/csharp/Grpc.Auth/Grpc.Auth.csproj | 2 | ||||
-rw-r--r-- | src/csharp/Grpc.Auth/OAuth2Interceptors.cs (renamed from src/csharp/Grpc.Auth/OAuth2InterceptorFactory.cs) | 29 | ||||
-rw-r--r-- | src/csharp/Grpc.IntegrationTesting/InteropClient.cs | 13 |
3 files changed, 32 insertions, 12 deletions
diff --git a/src/csharp/Grpc.Auth/Grpc.Auth.csproj b/src/csharp/Grpc.Auth/Grpc.Auth.csproj index afb8204c07..5615ffd620 100644 --- a/src/csharp/Grpc.Auth/Grpc.Auth.csproj +++ b/src/csharp/Grpc.Auth/Grpc.Auth.csproj @@ -79,7 +79,7 @@ </Compile> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="GoogleCredential.cs" /> - <Compile Include="OAuth2InterceptorFactory.cs" /> + <Compile Include="OAuth2Interceptors.cs" /> </ItemGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <ItemGroup> diff --git a/src/csharp/Grpc.Auth/OAuth2InterceptorFactory.cs b/src/csharp/Grpc.Auth/OAuth2Interceptors.cs index 420c4cb537..c785ca5a16 100644 --- a/src/csharp/Grpc.Auth/OAuth2InterceptorFactory.cs +++ b/src/csharp/Grpc.Auth/OAuth2Interceptors.cs @@ -47,18 +47,32 @@ using Grpc.Core.Utils; namespace Grpc.Auth { - public static class OAuth2InterceptorFactory + public static class OAuth2Interceptors { /// <summary> - /// Creates OAuth2 interceptor. + /// Creates OAuth2 interceptor that will obtain access token from GoogleCredentials. /// </summary> - public static MetadataInterceptorDelegate Create(GoogleCredential googleCredential) + public static MetadataInterceptorDelegate FromCredential(GoogleCredential googleCredential) { var interceptor = new OAuth2Interceptor(googleCredential.InternalCredential, SystemClock.Default); return new MetadataInterceptorDelegate(interceptor.InterceptHeaders); } /// <summary> + /// Creates OAuth2 interceptor that will use given OAuth2 token. + /// </summary> + /// <param name="oauth2Token"></param> + /// <returns></returns> + public static MetadataInterceptorDelegate FromAccessToken(string oauth2Token) + { + Preconditions.CheckNotNull(oauth2Token); + return new MetadataInterceptorDelegate((metadata) => + { + metadata.Add(OAuth2Interceptor.CreateBearerTokenHeader(oauth2Token)); + }); + } + + /// <summary> /// Injects OAuth2 authorization header into initial metadata (= request headers). /// </summary> private class OAuth2Interceptor @@ -97,8 +111,15 @@ namespace Grpc.Auth public void InterceptHeaders(Metadata metadata) { var accessToken = GetAccessToken(CancellationToken.None); - metadata.Add(new Metadata.Entry(AuthorizationHeader, Schema + " " + accessToken)); + metadata.Add(CreateBearerTokenHeader(accessToken)); + } + + public static Metadata.Entry CreateBearerTokenHeader(string accessToken) + { + return new Metadata.Entry(AuthorizationHeader, Schema + " " + accessToken); } } + + } } diff --git a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs index 4f8a5a5da7..7411d91d5a 100644 --- a/src/csharp/Grpc.IntegrationTesting/InteropClient.cs +++ b/src/csharp/Grpc.IntegrationTesting/InteropClient.cs @@ -127,7 +127,7 @@ namespace Grpc.IntegrationTesting { credential = credential.CreateScoped(new[] { AuthScope }); } - client.HeaderInterceptor = OAuth2InterceptorFactory.Create(credential); + client.HeaderInterceptor = OAuth2Interceptors.FromCredential(credential); } RunTestCaseAsync(options.testCase, client).Wait(); @@ -356,11 +356,7 @@ namespace Grpc.IntegrationTesting Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result); string oauth2Token = credential.Token.AccessToken; - // Intercept calls with an OAuth2 token obtained out-of-band. - client.HeaderInterceptor = new MetadataInterceptorDelegate((metadata) => - { - metadata.Add(new Metadata.Entry("Authorization", "Bearer " + oauth2Token)); - }); + client.HeaderInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token); var request = SimpleRequest.CreateBuilder() .SetFillUsername(true) @@ -381,13 +377,16 @@ namespace Grpc.IntegrationTesting var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope }); Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result); string oauth2Token = credential.Token.AccessToken; + var headerInterceptor = OAuth2Interceptors.FromAccessToken(oauth2Token); var request = SimpleRequest.CreateBuilder() .SetFillUsername(true) .SetFillOauthScope(true) .Build(); - var response = client.UnaryCall(request, headers: new Metadata { new Metadata.Entry("Authorization", "Bearer " + oauth2Token) }); + var headers = new Metadata(); + headerInterceptor(headers); + var response = client.UnaryCall(request, headers: headers); Assert.AreEqual(AuthScopeResponse, response.OauthScope); Assert.AreEqual(ServiceAccountUser, response.Username); |