• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #region Copyright notice and license
2 
3 // Copyright 2015 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #endregion
18 
19 using System;
20 using System.Threading;
21 using System.Threading.Tasks;
22 
23 using Google.Apis.Auth.OAuth2;
24 using Grpc.Core;
25 using Grpc.Core.Utils;
26 
27 namespace Grpc.Auth
28 {
29     /// <summary>
30     /// Factory/extension methods to create instances of <see cref="ChannelCredentials"/> and <see cref="CallCredentials"/> classes
31     /// based on credential objects originating from Google auth library.
32     /// </summary>
33     public static class GoogleGrpcCredentials
34     {
35         /// <summary>
36         /// Retrieves an instance of Google's Application Default Credentials using
37         /// <c>GoogleCredential.GetApplicationDefaultAsync()</c> and converts them
38         /// into a gRPC <see cref="ChannelCredentials"/> that use the default SSL credentials.
39         /// </summary>
40         /// <returns>The <c>ChannelCredentials</c> instance.</returns>
GetApplicationDefaultAsync()41         public static async Task<ChannelCredentials> GetApplicationDefaultAsync()
42         {
43             var googleCredential = await GoogleCredential.GetApplicationDefaultAsync().ConfigureAwait(false);
44             return googleCredential.ToChannelCredentials();
45         }
46 
47         /// <summary>
48         /// Creates an instance of <see cref="CallCredentials"/> that will use given access token to authenticate
49         /// with a gRPC service.
50         /// </summary>
51         /// <param name="accessToken">OAuth2 access token.</param>
52         /// /// <returns>The <c>MetadataCredentials</c> instance.</returns>
FromAccessToken(string accessToken)53         public static CallCredentials FromAccessToken(string accessToken)
54         {
55             return CallCredentials.FromInterceptor(GoogleAuthInterceptors.FromAccessToken(accessToken));
56         }
57 
58         /// <summary>
59         /// Converts a <c>ITokenAccess</c> (e.g. <c>GoogleCredential</c>) object
60         /// into a gRPC <see cref="CallCredentials"/> object.
61         /// </summary>
62         /// <param name="credential">The credential to use to obtain access tokens.</param>
63         /// <returns>The <c>CallCredentials</c> instance.</returns>
ToCallCredentials(this ITokenAccess credential)64         public static CallCredentials ToCallCredentials(this ITokenAccess credential)
65         {
66             return CallCredentials.FromInterceptor(GoogleAuthInterceptors.FromCredential(credential));
67         }
68 
69         /// <summary>
70         /// Converts a <c>ITokenAccess</c> (e.g. <c>GoogleCredential</c>) object
71         /// into a gRPC <see cref="ChannelCredentials"/> object.
72         /// Default SSL credentials are used.
73         /// </summary>
74         /// <param name="googleCredential">The credential to use to obtain access tokens.</param>
75         /// <returns>>The <c>ChannelCredentials</c> instance.</returns>
ToChannelCredentials(this ITokenAccess googleCredential)76         public static ChannelCredentials ToChannelCredentials(this ITokenAccess googleCredential)
77         {
78             return ChannelCredentials.Create(new SslCredentials(), googleCredential.ToCallCredentials());
79         }
80     }
81 }
82