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 methods to create authorization interceptors for Google credentials. 31 /// <seealso cref="GoogleGrpcCredentials"/> 32 /// </summary> 33 public static class GoogleAuthInterceptors 34 { 35 private const string AuthorizationHeader = "Authorization"; 36 private const string Schema = "Bearer"; 37 38 /// <summary> 39 /// Creates an <see cref="AsyncAuthInterceptor"/> that will obtain access token from any credential type that implements 40 /// <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>). 41 /// </summary> 42 /// <param name="credential">The credential to use to obtain access tokens.</param> 43 /// <returns>The interceptor.</returns> FromCredential(ITokenAccess credential)44 public static AsyncAuthInterceptor FromCredential(ITokenAccess credential) 45 { 46 return new AsyncAuthInterceptor(async (context, metadata) => 47 { 48 var accessToken = await credential.GetAccessTokenForRequestAsync(context.ServiceUrl, CancellationToken.None).ConfigureAwait(false); 49 metadata.Add(CreateBearerTokenHeader(accessToken)); 50 }); 51 } 52 53 /// <summary> 54 /// Creates an <see cref="AsyncAuthInterceptor"/> that will use given access token as authorization. 55 /// </summary> 56 /// <param name="accessToken">OAuth2 access token.</param> 57 /// <returns>The interceptor.</returns> FromAccessToken(string accessToken)58 public static AsyncAuthInterceptor FromAccessToken(string accessToken) 59 { 60 GrpcPreconditions.CheckNotNull(accessToken); 61 return new AsyncAuthInterceptor((context, metadata) => 62 { 63 metadata.Add(CreateBearerTokenHeader(accessToken)); 64 return TaskUtils.CompletedTask; 65 }); 66 } 67 CreateBearerTokenHeader(string accessToken)68 private static Metadata.Entry CreateBearerTokenHeader(string accessToken) 69 { 70 return new Metadata.Entry(AuthorizationHeader, Schema + " " + accessToken); 71 } 72 } 73 } 74