blob: a979b0d8ee8081ccee1819fe3fc2b56a520b3f95 [file] [log] [blame]
Jan Tattermusch18729a02015-10-08 18:40:00 -07001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermusch18729a02015-10-08 18:40:00 -07004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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
Jan Tattermusch18729a02015-10-08 18:40:00 -07008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch18729a02015-10-08 18:40:00 -070010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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.
Jan Tattermusch18729a02015-10-08 18:40:00 -070016
17#endregion
18
19using System;
20using System.Threading;
21using System.Threading.Tasks;
22
23using Google.Apis.Auth.OAuth2;
24using Grpc.Core;
25using Grpc.Core.Utils;
26
27namespace 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>
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>
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>
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>
76 public static ChannelCredentials ToChannelCredentials(this ITokenAccess googleCredential)
77 {
78 return ChannelCredentials.Create(new SslCredentials(), googleCredential.ToCallCredentials());
79 }
80 }
81}