Android-cuttlefish cvd tool
credential_source.h
Go to the documentation of this file.
1//
2// Copyright (C) 2019 The Android Open Source Project
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#pragma once
17
18#include <chrono>
19#include <memory>
20#include <mutex>
21#include <string>
22#include <utility>
23
24#include "json/value.h"
25#include "openssl/evp.h"
26
29
30namespace cuttlefish {
31
33 public:
34 virtual ~CredentialSource() = default;
36};
37
38// Credentials with known expiration times with behavior to load new
39// credentials.
41 public:
42 virtual Result<std::string> Credential() final override;
43
44 protected:
46
47 private:
49
50 std::string latest_credential_;
52 std::chrono::steady_clock::time_point expiration_;
53};
54
55// OAuth2 credentials from the GCE metadata server.
56//
57// -
58// https://cloud.google.com/compute/docs/access/authenticate-workloads#applications
59// - https://cloud.google.com/compute/docs/metadata/overview
61 public:
62 static std::unique_ptr<CredentialSource> Make(HttpClient&);
63
64 private:
66
68
70};
71
72// Pass through a string as an authentication token with unknown expiration.
74 public:
76
77 static std::unique_ptr<CredentialSource> Make(const std::string& credential);
78
79 private:
80 FixedCredentialSource(const std::string& credential);
81 std::string credential_;
82};
83
84// OAuth2 tokens from a desktop refresh token.
85//
86// https://developers.google.com/identity/protocols/oauth2/native-app
88 public:
91 const std::string& oauth_contents);
92
94 HttpClient& http_client, const std::string& client_id,
95 const std::string& client_secret, const std::string& refresh_token);
96
97 private:
99 const std::string& client_id,
100 const std::string& client_secret,
101 const std::string& refresh_token);
102
104 HttpClient& http_client, const Json::Value& credential);
105
107
109 std::string client_id_;
110 std::string client_secret_;
111 std::string refresh_token_;
112};
113
114// OAuth2 tokens from service account files.
115//
116// https://developers.google.com/identity/protocols/oauth2/service-account
118 public:
120 HttpClient& http_client, const Json::Value& service_account_json,
121 const std::string& scope);
122
123 private:
125
127
129 std::string email_;
130 std::string scope_;
131 std::unique_ptr<EVP_PKEY, void (*)(EVP_PKEY*)> private_key_;
132};
133
134} // namespace cuttlefish
Definition: credential_source.h:32
virtual Result< std::string > Credential()=0
virtual ~CredentialSource()=default
Definition: credential_source.h:73
FixedCredentialSource(const std::string &credential)
Definition: credential_source.cc:170
static std::unique_ptr< CredentialSource > Make(const std::string &credential)
Definition: credential_source.cc:176
Result< std::string > Credential() override
Definition: credential_source.cc:174
std::string credential_
Definition: credential_source.h:81
Definition: credential_source.h:60
static std::unique_ptr< CredentialSource > Make(HttpClient &)
Definition: credential_source.cc:164
Result< std::pair< std::string, std::chrono::seconds > > Refresh() override
Definition: credential_source.cc:142
HttpClient & http_client_
Definition: credential_source.h:69
GceMetadataCredentialSource(HttpClient &)
Definition: credential_source.cc:137
Definition: http_client.h:95
Definition: credential_source.h:87
HttpClient & http_client_
Definition: credential_source.h:108
std::string refresh_token_
Definition: credential_source.h:111
static Result< std::unique_ptr< RefreshTokenCredentialSource > > FromOauth2ClientFile(HttpClient &http_client, const std::string &oauth_contents)
Definition: credential_source.cc:204
Result< std::pair< std::string, std::chrono::seconds > > Refresh() override
Definition: credential_source.cc:299
std::string client_id_
Definition: credential_source.h:109
RefreshTokenCredentialSource(HttpClient &http_client, const std::string &client_id, const std::string &client_secret, const std::string &refresh_token)
Definition: credential_source.cc:283
static Result< std::unique_ptr< RefreshTokenCredentialSource > > FromJson(HttpClient &http_client, const Json::Value &credential)
Definition: credential_source.cc:183
std::string client_secret_
Definition: credential_source.h:110
static Result< std::unique_ptr< CredentialSource > > Make(HttpClient &http_client, const std::string &client_id, const std::string &client_secret, const std::string &refresh_token)
Definition: credential_source.cc:291
Definition: credential_source.h:40
std::mutex latest_credential_mutex_
Definition: credential_source.h:51
std::chrono::steady_clock::time_point expiration_
Definition: credential_source.h:52
RefreshingCredentialSource()
Definition: credential_source.cc:124
std::string latest_credential_
Definition: credential_source.h:50
virtual Result< std::pair< std::string, std::chrono::seconds > > Refresh()=0
virtual Result< std::string > Credential() final override
Definition: credential_source.cc:127
Definition: credential_source.h:117
HttpClient & http_client_
Definition: credential_source.h:128
Result< std::pair< std::string, std::chrono::seconds > > Refresh() override
Definition: credential_source.cc:361
std::string scope_
Definition: credential_source.h:130
ServiceAccountOauthCredentialSource(HttpClient &http_client)
Definition: credential_source.cc:356
std::unique_ptr< EVP_PKEY, void(*)(EVP_PKEY *)> private_key_
Definition: credential_source.h:131
std::string email_
Definition: credential_source.h:129
static Result< std::unique_ptr< ServiceAccountOauthCredentialSource > > FromJson(HttpClient &http_client, const Json::Value &service_account_json, const std::string &scope)
Definition: credential_source.cc:329
Definition: alloc_driver.h:20
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30