OLD | NEW |
(Empty) | |
| 1 #region Copyright notice and license |
| 2 |
| 3 // Copyright 2015, Google Inc. |
| 4 // All rights reserved. |
| 5 // |
| 6 // Redistribution and use in source and binary forms, with or without |
| 7 // modification, are permitted provided that the following conditions are |
| 8 // met: |
| 9 // |
| 10 // * Redistributions of source code must retain the above copyright |
| 11 // notice, this list of conditions and the following disclaimer. |
| 12 // * Redistributions in binary form must reproduce the above |
| 13 // copyright notice, this list of conditions and the following disclaimer |
| 14 // in the documentation and/or other materials provided with the |
| 15 // distribution. |
| 16 // * Neither the name of Google Inc. nor the names of its |
| 17 // contributors may be used to endorse or promote products derived from |
| 18 // this software without specific prior written permission. |
| 19 // |
| 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
| 32 #endregion |
| 33 |
| 34 using System; |
| 35 using System.Threading; |
| 36 using System.Threading.Tasks; |
| 37 |
| 38 using Google.Apis.Auth.OAuth2; |
| 39 using Grpc.Core; |
| 40 using Grpc.Core.Utils; |
| 41 |
| 42 namespace Grpc.Auth |
| 43 { |
| 44 /// <summary> |
| 45 /// Factory/extension methods to create instances of <see cref="ChannelCrede
ntials"/> and <see cref="CallCredentials"/> classes |
| 46 /// based on credential objects originating from Google auth library. |
| 47 /// </summary> |
| 48 public static class GoogleGrpcCredentials |
| 49 { |
| 50 /// <summary> |
| 51 /// Retrieves an instance of Google's Application Default Credentials us
ing |
| 52 /// <c>GoogleCredential.GetApplicationDefaultAsync()</c> and converts th
em |
| 53 /// into a gRPC <see cref="ChannelCredentials"/> that use the default SS
L credentials. |
| 54 /// </summary> |
| 55 /// <returns>The <c>ChannelCredentials</c> instance.</returns> |
| 56 public static async Task<ChannelCredentials> GetApplicationDefaultAsync(
) |
| 57 { |
| 58 var googleCredential = await GoogleCredential.GetApplicationDefaultA
sync().ConfigureAwait(false); |
| 59 return googleCredential.ToChannelCredentials(); |
| 60 } |
| 61 |
| 62 /// <summary> |
| 63 /// Creates an instance of <see cref="CallCredentials"/> that will use g
iven access token to authenticate |
| 64 /// with a gRPC service. |
| 65 /// </summary> |
| 66 /// <param name="accessToken">OAuth2 access token.</param> |
| 67 /// /// <returns>The <c>MetadataCredentials</c> instance.</returns> |
| 68 public static CallCredentials FromAccessToken(string accessToken) |
| 69 { |
| 70 return CallCredentials.FromInterceptor(GoogleAuthInterceptors.FromAc
cessToken(accessToken)); |
| 71 } |
| 72 |
| 73 /// <summary> |
| 74 /// Converts a <c>ITokenAccess</c> (e.g. <c>GoogleCredential</c>) object |
| 75 /// into a gRPC <see cref="CallCredentials"/> object. |
| 76 /// </summary> |
| 77 /// <param name="credential">The credential to use to obtain access toke
ns.</param> |
| 78 /// <returns>The <c>CallCredentials</c> instance.</returns> |
| 79 public static CallCredentials ToCallCredentials(this ITokenAccess creden
tial) |
| 80 { |
| 81 return CallCredentials.FromInterceptor(GoogleAuthInterceptors.FromCr
edential(credential)); |
| 82 } |
| 83 |
| 84 /// <summary> |
| 85 /// Converts a <c>ITokenAccess</c> (e.g. <c>GoogleCredential</c>) object |
| 86 /// into a gRPC <see cref="ChannelCredentials"/> object. |
| 87 /// Default SSL credentials are used. |
| 88 /// </summary> |
| 89 /// <param name="googleCredential">The credential to use to obtain acces
s tokens.</param> |
| 90 /// <returns>>The <c>ChannelCredentials</c> instance.</returns> |
| 91 public static ChannelCredentials ToChannelCredentials(this ITokenAccess
googleCredential) |
| 92 { |
| 93 return ChannelCredentials.Create(new SslCredentials(), googleCredent
ial.ToCallCredentials()); |
| 94 } |
| 95 } |
| 96 } |
OLD | NEW |