feat(clients): google service-account OAuth and per-endpoint credential choice
ServiceAccountAuth signs an RS256 JWT with wolfCrypt from the key file in GOOGLE_APPLICATION_CREDENTIALS and exchanges it for a self-refreshing bearer token. GoogleClient holds a key and an OAuth credential and uses OAuth everywhere except express endpoints, which only accept the key. Signing is tested with generated RSA keys and public-key verification. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+18
-3
@@ -17,13 +17,26 @@ clients::ClientConfig make_config(std::shared_ptr<clients::Auth> auth) {
|
||||
|
||||
GoogleClient::GoogleClient(std::unique_ptr<clients::Transport> transport,
|
||||
std::shared_ptr<clients::Auth> auth)
|
||||
: client_(std::move(transport), make_config(auth)),
|
||||
configured_(auth != nullptr) {}
|
||||
: GoogleClient(std::move(transport), std::move(auth), nullptr) {}
|
||||
|
||||
GoogleClient::GoogleClient(std::unique_ptr<clients::Transport> transport,
|
||||
std::shared_ptr<clients::Auth> key,
|
||||
std::shared_ptr<clients::Auth> oauth)
|
||||
: client_(std::move(transport), make_config(nullptr)),
|
||||
key_auth_(std::move(key)),
|
||||
oauth_auth_(std::move(oauth)),
|
||||
configured_(key_auth_ != nullptr || oauth_auth_ != nullptr) {}
|
||||
|
||||
std::shared_ptr<clients::Auth> GoogleClient::pick_auth(
|
||||
const ServiceEndpoint& service) const {
|
||||
if (oauth_auth_ && !service.prefer_key) return oauth_auth_;
|
||||
return key_auth_ ? key_auth_ : oauth_auth_;
|
||||
}
|
||||
|
||||
std::shared_ptr<GoogleClient> GoogleClient::from_env() {
|
||||
return std::make_shared<GoogleClient>(
|
||||
std::make_unique<clients::transports::HttpTransport>(),
|
||||
Credentials::from_env());
|
||||
Credentials::api_key_from_env(), Credentials::oauth_from_env());
|
||||
}
|
||||
|
||||
clients::body::Json GoogleClient::request(clients::Method method,
|
||||
@@ -35,6 +48,7 @@ clients::body::Json GoogleClient::request(clients::Method method,
|
||||
clients::ClientRequest req(method,
|
||||
clients::Url::parse(service.base_url + path));
|
||||
if (body) req.json(*body);
|
||||
pick_auth(service)->apply(req);
|
||||
return finish(service, client_.send_blocking(std::move(req)));
|
||||
} catch (const GoogleError&) {
|
||||
throw;
|
||||
@@ -75,6 +89,7 @@ void GoogleClient::stream_json(const ServiceEndpoint& service,
|
||||
clients::ClientRequest req(
|
||||
clients::Method::kPost, clients::Url::parse(service.base_url + path));
|
||||
req.json(body);
|
||||
pick_auth(service)->apply(req);
|
||||
client_.send_stream_blocking(
|
||||
std::move(req),
|
||||
[&](const std::string& chunk) { return decoder.feed(chunk); });
|
||||
|
||||
Reference in New Issue
Block a user