コード例 #1
0
ファイル: InteropClient.cs プロジェクト: zinuga/grpc
        public static async Task RunOAuth2AuthTokenAsync(TestService.TestServiceClient client, string oauthScope)
        {
#if !NETCOREAPP1_0
            Console.WriteLine("running oauth2_auth_token");
            ITokenAccess credential  = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { oauthScope });
            string       oauth2Token = await credential.GetAccessTokenForRequestAsync();

            var credentials = GoogleGrpcCredentials.FromAccessToken(oauth2Token);
            var request     = new SimpleRequest
            {
                FillUsername   = true,
                FillOauthScope = true
            };

            var response = client.UnaryCall(request, new CallOptions(credentials: credentials));

            Assert.False(string.IsNullOrEmpty(response.OauthScope));
            Assert.True(oauthScope.Contains(response.OauthScope));
            Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username);
            Console.WriteLine("Passed!");
#else
            // TODO(jtattermusch): implement this
            throw new NotImplementedException("Not supported on CoreCLR yet");
#endif
        }
コード例 #2
0
        public async Task <string> GetAccessTokenForRequestAsync()
        {
            ITokenAccess c = _googleCredential as ITokenAccess;

            _accessToken = await c.GetAccessTokenForRequestAsync();

            return(_accessToken);
        }
コード例 #3
0
 /// <summary>
 /// Creates an <see cref="AsyncAuthInterceptor"/> that will obtain access token from any credential type that implements
 /// <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>).
 /// </summary>
 /// <param name="credential">The credential to use to obtain access tokens.</param>
 /// <returns>The interceptor.</returns>
 public static AsyncAuthInterceptor FromCredential(ITokenAccess credential)
 {
     return(new AsyncAuthInterceptor(async(authUri, metadata) =>
     {
         var accessToken = await credential.GetAccessTokenForRequestAsync(authUri, CancellationToken.None).ConfigureAwait(false);
         metadata.Add(CreateBearerTokenHeader(accessToken));
     }));
 }
コード例 #4
0
 /// <summary>
 /// Creates an <see cref="AsyncAuthInterceptor"/> that will obtain access token from any credential type that implements
 /// <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>).
 /// </summary>
 /// <param name="credential">The credential to use to obtain access tokens.</param>
 /// <returns>The interceptor.</returns>
 public static AsyncAuthInterceptor FromCredential(ITokenAccess credential)
 {
     return new AsyncAuthInterceptor(async (context, metadata) =>
     {
         var accessToken = await credential.GetAccessTokenForRequestAsync(context.ServiceUrl, CancellationToken.None).ConfigureAwait(false);
         metadata.Add(CreateBearerTokenHeader(accessToken));
     });
 }
コード例 #5
0
ファイル: AuthInterceptors.cs プロジェクト: larsonmpdx/grpc
 /// <summary>
 /// Creates interceptor that will obtain access token from any credential type that implements
 /// <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>).
 /// </summary>
 /// <param name="credential">The credential to use to obtain access tokens.</param>
 /// <returns>The header interceptor.</returns>
 public static HeaderInterceptor FromCredential(ITokenAccess credential)
 {
     return new HeaderInterceptor((method, authUri, metadata) =>
     {
         // TODO(jtattermusch): Rethink synchronous wait to obtain the result.
         var accessToken = credential.GetAccessTokenForRequestAsync(authUri, CancellationToken.None)
                 .ConfigureAwait(false).GetAwaiter().GetResult();
         metadata.Add(CreateBearerTokenHeader(accessToken));
     });
 }
コード例 #6
0
 /// <summary>
 /// Creates interceptor that will obtain access token from any credential type that implements
 /// <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>).
 /// </summary>
 /// <param name="credential">The credential to use to obtain access tokens.</param>
 /// <returns>The header interceptor.</returns>
 public static HeaderInterceptor FromCredential(ITokenAccess credential)
 {
     return(new HeaderInterceptor((method, authUri, metadata) =>
     {
         // TODO(jtattermusch): Rethink synchronous wait to obtain the result.
         var accessToken = credential.GetAccessTokenForRequestAsync(authUri, CancellationToken.None)
                           .ConfigureAwait(false).GetAwaiter().GetResult();
         metadata.Add(CreateBearerTokenHeader(accessToken));
     }));
 }
コード例 #7
0
        /// <summary>
        /// Creates an <see cref="AsyncAuthInterceptor"/> that will obtain access token from any credential type that implements
        /// <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>).
        /// </summary>
        /// <param name="credential">The credential to use to obtain access tokens.</param>
        /// <returns>The interceptor.</returns>
        public static AsyncAuthInterceptor FromCredential(ITokenAccess credential)
        {
            if (credential is ITokenAccessWithHeaders credentialWithHeaders)
            {
                return(FromCredential(credentialWithHeaders));
            }

            return(new AsyncAuthInterceptor(async(context, metadata) =>
            {
                var accessToken = await credential.GetAccessTokenForRequestAsync(context.ServiceUrl, CancellationToken.None).ConfigureAwait(false);
                metadata.Add(CreateBearerTokenHeader(accessToken));
            }));
        }
コード例 #8
0
        public static async Task RunOAuth2AuthTokenAsync(TestService.TestServiceClient client)
        {
            Console.WriteLine("running oauth2_auth_token");
            ITokenAccess credential  = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { AuthScope });
            string       oauth2Token = await credential.GetAccessTokenForRequestAsync();

            client.HeaderInterceptor = OAuth2Interceptors.FromAccessToken(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!");
        }
コード例 #9
0
        public static async Task RunOAuth2AuthTokenAsync(TestService.TestServiceClient client, string oauthScope)
        {
            Console.WriteLine("running oauth2_auth_token");
            ITokenAccess credential  = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { oauthScope });
            string       oauth2Token = await credential.GetAccessTokenForRequestAsync();

            var credentials = GoogleGrpcCredentials.FromAccessToken(oauth2Token);
            var request     = new SimpleRequest
            {
                FillUsername   = true,
                FillOauthScope = true
            };

            var response = client.UnaryCall(request, new CallOptions(credentials: credentials));

            Assert.False(string.IsNullOrEmpty(response.OauthScope));
            Assert.True(oauthScope.Contains(response.OauthScope));
            Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username);
            Console.WriteLine("Passed!");
        }
        public async Task <string> GetToken()
        {
            GoogleCredential credential;

            using (var stream = new System.IO.FileStream("serviceAccountKey.json", System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {
                credential = GoogleCredential.FromStream(stream).CreateScoped(
                    new string[] {
                    "https://www.googleapis.com/auth/firebase.database",
                    "https://www.googleapis.com/auth/firebase.messaging",
                    "https://www.googleapis.com/auth/identitytoolkit",
                    "https://www.googleapis.com/auth/userinfo.email"
                }
                    );
            }

            ITokenAccess c = credential as ITokenAccess;

            return(await c.GetAccessTokenForRequestAsync());
        }
コード例 #11
0
        public static async Task RunOAuth2AuthTokenAsync(IChannelWrapper channel, ClientOptions options)
        {
            var client     = CreateClient <TestService.TestServiceClient>(channel);
            var oauthScope = options.OAuthScope !;

            ITokenAccess credential  = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { oauthScope });
            var          oauth2Token = await credential.GetAccessTokenForRequestAsync();

            var credentials = GoogleGrpcCredentials.FromAccessToken(oauth2Token);
            var request     = new SimpleRequest
            {
                FillUsername   = true,
                FillOauthScope = true
            };

            var response = await client.UnaryCallAsync(request, new CallOptions(credentials : credentials));

            Assert.IsFalse(string.IsNullOrEmpty(response.OauthScope));
            Assert.IsTrue(oauthScope.Contains(response.OauthScope));
            Assert.AreEqual(GetEmailFromServiceAccountFile(), response.Username);
        }
コード例 #12
0
        public static async Task RunPerRpcCredsAsync(TestService.TestServiceClient client)
        {
            Console.WriteLine("running per_rpc_creds");

            ITokenAccess credential  = (await GoogleCredential.GetApplicationDefaultAsync()).CreateScoped(new[] { AuthScope });
            string       oauth2Token = await credential.GetAccessTokenForRequestAsync();

            var headerInterceptor = AuthInterceptors.FromAccessToken(oauth2Token);

            var request = SimpleRequest.CreateBuilder()
                          .SetFillUsername(true)
                          .SetFillOauthScope(true)
                          .Build();

            var headers = new Metadata();

            headerInterceptor(null, "", headers);
            var response = client.UnaryCall(request, headers: headers);

            Assert.AreEqual(AuthScopeResponse, response.OauthScope);
            Assert.AreEqual(ServiceAccountUser, response.Username);
            Console.WriteLine("Passed!");
        }
コード例 #13
0
 /// <summary>
 /// Gets access token and requests refreshing it if is going to expire soon.
 /// </summary>
 /// <param name="cancellationToken"></param>
 /// <returns></returns>
 public string GetAccessToken(string authUri, CancellationToken cancellationToken)
 {
     // TODO(jtattermusch): Rethink synchronous wait to obtain the result.
     return(credential.GetAccessTokenForRequestAsync(authUri, cancellationToken: cancellationToken).GetAwaiter().GetResult());
 }
コード例 #14
0
 /// <summary>
 /// Gets the authentication token
 /// </summary>
 /// <param name="cancellationToken">A <see cref="CancellationToken"/></param>
 /// <returns>The token</returns>
 public Task <string> GetTokenAsync(CancellationToken cancellationToken = default(CancellationToken))
 {
     return(_credential.GetAccessTokenForRequestAsync(cancellationToken: cancellationToken));
 }