implement per_rpc_creds and oauth2_auth_token interop tests

pull/2553/head
Jan Tattermusch 9 years ago
parent aabf72c218
commit 7b4a31f982
  1. 16
      src/csharp/Grpc.Auth/GoogleCredential.cs
  2. 3
      src/csharp/Grpc.Core.Tests/Internal/MetadataArraySafeHandleTest.cs
  3. 53
      src/csharp/Grpc.IntegrationTesting/InteropClient.cs

@ -35,8 +35,11 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Auth.OAuth2; using Google.Apis.Auth.OAuth2;
using Google.Apis.Auth.OAuth2.Responses;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using Org.BouncyCastle.Crypto.Parameters; using Org.BouncyCastle.Crypto.Parameters;
using Org.BouncyCastle.Security; using Org.BouncyCastle.Security;
@ -100,6 +103,19 @@ namespace Grpc.Auth
return new GoogleCredential(serviceCredential); return new GoogleCredential(serviceCredential);
} }
public Task<bool> RequestAccessTokenAsync(CancellationToken taskCancellationToken)
{
return credential.RequestAccessTokenAsync(taskCancellationToken);
}
public TokenResponse Token
{
get
{
return credential.Token;
}
}
internal ServiceCredential InternalCredential internal ServiceCredential InternalCredential
{ {
get get

@ -51,7 +51,8 @@ namespace Grpc.Core.Internal.Tests
[Test] [Test]
public void CreateAndDestroy() public void CreateAndDestroy()
{ {
var metadata = new Metadata { var metadata = new Metadata
{
new Metadata.Entry("host", "somehost"), new Metadata.Entry("host", "somehost"),
new Metadata.Entry("header2", "header value"), new Metadata.Entry("header2", "header value"),
}; };

@ -135,7 +135,7 @@ namespace Grpc.IntegrationTesting
GrpcEnvironment.Shutdown(); GrpcEnvironment.Shutdown();
} }
private void RunTestCase(string testCase, TestService.ITestServiceClient client) private void RunTestCase(string testCase, TestService.TestServiceClient client)
{ {
switch (testCase) switch (testCase)
{ {
@ -163,6 +163,12 @@ namespace Grpc.IntegrationTesting
case "compute_engine_creds": case "compute_engine_creds":
RunComputeEngineCreds(client); RunComputeEngineCreds(client);
break; break;
case "oauth2_auth_token":
RunOAuth2AuthToken(client);
break;
case "per_rpc_creds":
RunPerRpcCreds(client);
break;
case "cancel_after_begin": case "cancel_after_begin":
RunCancelAfterBegin(client); RunCancelAfterBegin(client);
break; break;
@ -355,6 +361,51 @@ namespace Grpc.IntegrationTesting
Console.WriteLine("Passed!"); Console.WriteLine("Passed!");
} }
public static void RunOAuth2AuthToken(TestService.TestServiceClient client)
{
Console.WriteLine("running oauth2_auth_token");
var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope });
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));
});
var request = SimpleRequest.CreateBuilder()
.SetFillUsername(true)
.SetFillOauthScope(true)
.Build();
var response = client.UnaryCall(request);
Assert.AreEqual(AuthScopeResponse, response.OauthScope);
Assert.AreEqual(ServiceAccountUser, response.Username);
Console.WriteLine("Passed!");
}
public static void RunPerRpcCreds(TestService.TestServiceClient client)
{
Console.WriteLine("running per_rpc_creds");
var credential = GoogleCredential.GetApplicationDefault().CreateScoped(new[] { AuthScope });
Assert.IsTrue(credential.RequestAccessTokenAsync(CancellationToken.None).Result);
string oauth2Token = credential.Token.AccessToken;
var request = SimpleRequest.CreateBuilder()
.SetFillUsername(true)
.SetFillOauthScope(true)
.Build();
var response = client.UnaryCall(request, headers: new Metadata { new Metadata.Entry("Authorization", "Bearer " + oauth2Token) } );
Assert.AreEqual(AuthScopeResponse, response.OauthScope);
Assert.AreEqual(ServiceAccountUser, response.Username);
Console.WriteLine("Passed!");
}
public static void RunCancelAfterBegin(TestService.ITestServiceClient client) public static void RunCancelAfterBegin(TestService.ITestServiceClient client)
{ {
Task.Run(async () => Task.Run(async () =>

Loading…
Cancel
Save