blob: 9936cf583ca1de71c50a2dff447ccf7baa8843a2 [file] [log] [blame]
Jan Tattermuschbb9d7882015-04-22 16:54:59 -07001#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
34using System;
35using System.Collections.Generic;
Jan Tattermuschbb9d7882015-04-22 16:54:59 -070036using System.IO;
Jan Tattermusch1ca56b92015-04-27 11:03:06 -070037using System.Security.Cryptography;
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -070038using System.Threading;
39using System.Threading.Tasks;
Jan Tattermuschbb9d7882015-04-22 16:54:59 -070040
41using Google.Apis.Auth.OAuth2;
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -070042using Google.Apis.Auth.OAuth2.Responses;
Jan Tattermusch1ca56b92015-04-27 11:03:06 -070043using Newtonsoft.Json.Linq;
Jan Tattermuschdca6e882015-04-22 16:56:27 -070044using Org.BouncyCastle.Crypto.Parameters;
Jan Tattermuschdca6e882015-04-22 16:56:27 -070045using Org.BouncyCastle.Security;
Jan Tattermuschbb9d7882015-04-22 16:54:59 -070046
47namespace Grpc.Auth
48{
49 // TODO(jtattermusch): Remove this class once possible.
50 /// <summary>
51 /// A temporary placeholder for Google credential from
52 /// Google Auth library for .NET. It emulates the usage pattern
53 /// for Usable auth.
54 /// </summary>
55 public class GoogleCredential
56 {
57 private const string GoogleApplicationCredentialsEnvName = "GOOGLE_APPLICATION_CREDENTIALS";
Jan Tattermuschdca6e882015-04-22 16:56:27 -070058 private const string ClientEmailFieldName = "client_email";
59 private const string PrivateKeyFieldName = "private_key";
Jan Tattermuschbb9d7882015-04-22 16:54:59 -070060
61 private ServiceCredential credential;
62
63 private GoogleCredential(ServiceCredential credential)
64 {
65 this.credential = credential;
66 }
67
68 public static GoogleCredential GetApplicationDefault()
69 {
70 return new GoogleCredential(null);
71 }
72
73 public bool IsCreateScopedRequired
74 {
75 get
76 {
77 return true;
78 }
79 }
80
81 public GoogleCredential CreateScoped(IEnumerable<string> scopes)
82 {
Jan Tattermuschdca6e882015-04-22 16:56:27 -070083 var credsPath = Environment.GetEnvironmentVariable(GoogleApplicationCredentialsEnvName);
Jan Tattermusch0bbfa382015-04-27 16:11:59 -070084 if (credsPath == null)
85 {
86 // Default to ComputeCredentials if path to JSON key is not set.
87 // ComputeCredential is not scoped actually, but for our use case it's
88 // fine to treat is as such.
89 return new GoogleCredential(new ComputeCredential(new ComputeCredential.Initializer()));
90 }
Jan Tattermuschbb9d7882015-04-22 16:54:59 -070091
Jan Tattermuscha6b82882015-07-23 22:48:28 -070092 JObject jsonCredentialParameters = JObject.Parse(File.ReadAllText(credsPath));
93 string clientEmail = jsonCredentialParameters.GetValue(ClientEmailFieldName).Value<string>();
94 string privateKeyString = jsonCredentialParameters.GetValue(PrivateKeyFieldName).Value<string>();
Jan Tattermuschbb9d7882015-04-22 16:54:59 -070095
96 var serviceCredential = new ServiceAccountCredential(
Jan Tattermuschdca6e882015-04-22 16:56:27 -070097 new ServiceAccountCredential.Initializer(clientEmail)
Jan Tattermuschbb9d7882015-04-22 16:54:59 -070098 {
Jan Tattermuschdca6e882015-04-22 16:56:27 -070099 Scopes = scopes,
Jan Tattermuscha6b82882015-07-23 22:48:28 -0700100 }.FromPrivateKey(privateKeyString));
Jan Tattermuschbb9d7882015-04-22 16:54:59 -0700101 return new GoogleCredential(serviceCredential);
102 }
103
Jan Tattermusch7b4a31f2015-07-20 17:08:13 -0700104 public Task<bool> RequestAccessTokenAsync(CancellationToken taskCancellationToken)
105 {
106 return credential.RequestAccessTokenAsync(taskCancellationToken);
107 }
108
109 public TokenResponse Token
110 {
111 get
112 {
113 return credential.Token;
114 }
115 }
116
Jan Tattermuschbb9d7882015-04-22 16:54:59 -0700117 internal ServiceCredential InternalCredential
118 {
119 get
120 {
121 return credential;
122 }
123 }
Jan Tattermuschbb9d7882015-04-22 16:54:59 -0700124 }
125}