Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 3 | // Copyright 2015 gRPC authors. |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 4 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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 |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 8 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 10 | // |
Jan Tattermusch | 7897ae9 | 2017-06-07 22:57:36 +0200 | [diff] [blame] | 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. |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 16 | |
| 17 | #endregion |
| 18 | |
| 19 | using System; |
| 20 | using System.Threading; |
Jan Tattermusch | 9f254c0 | 2016-10-21 10:45:04 +0200 | [diff] [blame] | 21 | using System.Threading.Tasks; |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 22 | |
| 23 | using Google.Apis.Auth.OAuth2; |
| 24 | using Grpc.Core; |
| 25 | using Grpc.Core.Utils; |
| 26 | |
| 27 | namespace Grpc.Auth |
| 28 | { |
| 29 | /// <summary> |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 30 | /// Factory methods to create authorization interceptors for Google credentials. |
| 31 | /// <seealso cref="GoogleGrpcCredentials"/> |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 32 | /// </summary> |
Jan Tattermusch | 18729a0 | 2015-10-08 18:40:00 -0700 | [diff] [blame] | 33 | public static class GoogleAuthInterceptors |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 34 | { |
| 35 | private const string AuthorizationHeader = "Authorization"; |
| 36 | private const string Schema = "Bearer"; |
| 37 | |
| 38 | /// <summary> |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 39 | /// Creates an <see cref="AsyncAuthInterceptor"/> that will obtain access token from any credential type that implements |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 40 | /// <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>). |
| 41 | /// </summary> |
Jan Tattermusch | 12855fc | 2015-08-24 16:43:23 -0700 | [diff] [blame] | 42 | /// <param name="credential">The credential to use to obtain access tokens.</param> |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 43 | /// <returns>The interceptor.</returns> |
| 44 | public static AsyncAuthInterceptor FromCredential(ITokenAccess credential) |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 45 | { |
Jan Tattermusch | 189fcf8 | 2015-12-02 13:41:12 -0800 | [diff] [blame] | 46 | return new AsyncAuthInterceptor(async (context, metadata) => |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 47 | { |
Jan Tattermusch | 189fcf8 | 2015-12-02 13:41:12 -0800 | [diff] [blame] | 48 | var accessToken = await credential.GetAccessTokenForRequestAsync(context.ServiceUrl, CancellationToken.None).ConfigureAwait(false); |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 49 | metadata.Add(CreateBearerTokenHeader(accessToken)); |
| 50 | }); |
| 51 | } |
| 52 | |
| 53 | /// <summary> |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 54 | /// Creates an <see cref="AsyncAuthInterceptor"/> that will use given access token as authorization. |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 55 | /// </summary> |
| 56 | /// <param name="accessToken">OAuth2 access token.</param> |
Jan Tattermusch | 74f39e1 | 2015-09-23 20:14:56 -0700 | [diff] [blame] | 57 | /// <returns>The interceptor.</returns> |
| 58 | public static AsyncAuthInterceptor FromAccessToken(string accessToken) |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 59 | { |
Jan Tattermusch | 7a3ee6a | 2016-02-18 10:36:02 -0800 | [diff] [blame] | 60 | GrpcPreconditions.CheckNotNull(accessToken); |
Jan Tattermusch | 9f254c0 | 2016-10-21 10:45:04 +0200 | [diff] [blame] | 61 | return new AsyncAuthInterceptor((context, metadata) => |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 62 | { |
| 63 | metadata.Add(CreateBearerTokenHeader(accessToken)); |
Jan Tattermusch | 5fe5eba | 2016-10-21 10:54:21 +0200 | [diff] [blame] | 64 | return TaskUtils.CompletedTask; |
Jan Tattermusch | 9e14414 | 2015-08-17 14:58:09 -0700 | [diff] [blame] | 65 | }); |
| 66 | } |
| 67 | |
| 68 | private static Metadata.Entry CreateBearerTokenHeader(string accessToken) |
| 69 | { |
| 70 | return new Metadata.Entry(AuthorizationHeader, Schema + " " + accessToken); |
| 71 | } |
| 72 | } |
| 73 | } |