blob: d859075c3bd254eeeb4c6248efe5fda741c648f2 [file] [log] [blame]
Jan Tattermusch9e144142015-08-17 14:58:09 -07001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermusch9e144142015-08-17 14:58:09 -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 Tattermusch9e144142015-08-17 14:58:09 -07008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermusch9e144142015-08-17 14:58:09 -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 Tattermusch9e144142015-08-17 14:58:09 -070016
17#endregion
18
19using System;
20using System.Threading;
Jan Tattermusch9f254c02016-10-21 10:45:04 +020021using System.Threading.Tasks;
Jan Tattermusch9e144142015-08-17 14:58:09 -070022
23using Google.Apis.Auth.OAuth2;
24using Grpc.Core;
25using Grpc.Core.Utils;
26
27namespace Grpc.Auth
28{
29 /// <summary>
Jan Tattermusch18729a02015-10-08 18:40:00 -070030 /// Factory methods to create authorization interceptors for Google credentials.
31 /// <seealso cref="GoogleGrpcCredentials"/>
Jan Tattermusch9e144142015-08-17 14:58:09 -070032 /// </summary>
Jan Tattermusch18729a02015-10-08 18:40:00 -070033 public static class GoogleAuthInterceptors
Jan Tattermusch9e144142015-08-17 14:58:09 -070034 {
35 private const string AuthorizationHeader = "Authorization";
36 private const string Schema = "Bearer";
37
38 /// <summary>
Jan Tattermusch74f39e12015-09-23 20:14:56 -070039 /// Creates an <see cref="AsyncAuthInterceptor"/> that will obtain access token from any credential type that implements
Jan Tattermusch9e144142015-08-17 14:58:09 -070040 /// <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>).
41 /// </summary>
Jan Tattermusch12855fc2015-08-24 16:43:23 -070042 /// <param name="credential">The credential to use to obtain access tokens.</param>
Jan Tattermusch74f39e12015-09-23 20:14:56 -070043 /// <returns>The interceptor.</returns>
44 public static AsyncAuthInterceptor FromCredential(ITokenAccess credential)
Jan Tattermusch9e144142015-08-17 14:58:09 -070045 {
Jan Tattermusch189fcf82015-12-02 13:41:12 -080046 return new AsyncAuthInterceptor(async (context, metadata) =>
Jan Tattermusch9e144142015-08-17 14:58:09 -070047 {
Jan Tattermusch189fcf82015-12-02 13:41:12 -080048 var accessToken = await credential.GetAccessTokenForRequestAsync(context.ServiceUrl, CancellationToken.None).ConfigureAwait(false);
Jan Tattermusch9e144142015-08-17 14:58:09 -070049 metadata.Add(CreateBearerTokenHeader(accessToken));
50 });
51 }
52
53 /// <summary>
Jan Tattermusch74f39e12015-09-23 20:14:56 -070054 /// Creates an <see cref="AsyncAuthInterceptor"/> that will use given access token as authorization.
Jan Tattermusch9e144142015-08-17 14:58:09 -070055 /// </summary>
56 /// <param name="accessToken">OAuth2 access token.</param>
Jan Tattermusch74f39e12015-09-23 20:14:56 -070057 /// <returns>The interceptor.</returns>
58 public static AsyncAuthInterceptor FromAccessToken(string accessToken)
Jan Tattermusch9e144142015-08-17 14:58:09 -070059 {
Jan Tattermusch7a3ee6a2016-02-18 10:36:02 -080060 GrpcPreconditions.CheckNotNull(accessToken);
Jan Tattermusch9f254c02016-10-21 10:45:04 +020061 return new AsyncAuthInterceptor((context, metadata) =>
Jan Tattermusch9e144142015-08-17 14:58:09 -070062 {
63 metadata.Add(CreateBearerTokenHeader(accessToken));
Jan Tattermusch5fe5eba2016-10-21 10:54:21 +020064 return TaskUtils.CompletedTask;
Jan Tattermusch9e144142015-08-17 14:58:09 -070065 });
66 }
67
68 private static Metadata.Entry CreateBearerTokenHeader(string accessToken)
69 {
70 return new Metadata.Entry(AuthorizationHeader, Schema + " " + accessToken);
71 }
72 }
73}