57 lines
2.2 KiB
C++
57 lines
2.2 KiB
C++
#pragma once
|
|||
|
|
|
||
|
|
// IAM Service Account Credentials v1: signJwt, signBlob, access tokens and id
|
||
|
|
// tokens for a service account the caller may impersonate. Signing changes no
|
||
|
|
// state, so these run directly. Payload sizes are capped.
|
||
|
|
|
||
|
|
#include <vector>
|
||
|
|
|
||
|
|
#include "google/errors.h"
|
||
|
|
#include "google/ops/ops_product.h"
|
||
|
|
|
||
|
|
namespace molo::google::ops {
|
||
|
|
|
||
|
|
class IamCredentials : public OpsProduct {
|
||
|
|
public:
|
||
|
|
using Json = clients::body::Json;
|
||
|
|
explicit IamCredentials(std::shared_ptr<GoogleClient> c, Sleeper s = nullptr)
|
||
|
|
: OpsProduct(std::move(c), "iamcredentials", std::move(s)) {}
|
||
|
|
|
||
|
|
// payload: a JSON object serialized as a string (the claim set).
|
||
|
|
Json sign_jwt(const std::string& account_name, const std::string& payload) const {
|
||
|
|
check_payload(payload);
|
||
|
|
Args a = at("name", account_name);
|
||
|
|
a.field("payload", Json(payload));
|
||
|
|
return read("iamcredentials.projects.serviceAccounts.signJwt", a);
|
||
|
|
}
|
||
|
|
// base64_payload: the bytes to sign, base64 encoded.
|
||
|
|
Json sign_blob(const std::string& account_name, const std::string& base64_payload) const {
|
||
|
|
check_payload(base64_payload);
|
||
|
|
Args a = at("name", account_name);
|
||
|
|
a.field("payload", Json(base64_payload));
|
||
|
|
return read("iamcredentials.projects.serviceAccounts.signBlob", a);
|
||
|
|
}
|
||
|
|
Json generate_access_token(const std::string& account_name,
|
||
|
|
const std::vector<std::string>& scopes) const {
|
||
|
|
Json arr = Json::array();
|
||
|
|
for (const std::string& s : scopes) arr.push(Json(s));
|
||
|
|
Args a = at("name", account_name);
|
||
|
|
a.field("scope", arr);
|
||
|
|
return read("iamcredentials.projects.serviceAccounts.generateAccessToken", a);
|
||
|
|
}
|
||
|
|
Json generate_id_token(const std::string& account_name, const std::string& audience) const {
|
||
|
|
Args a = at("name", account_name);
|
||
|
|
a.field("audience", Json(audience));
|
||
|
|
return read("iamcredentials.projects.serviceAccounts.generateIdToken", a);
|
||
|
|
}
|
||
|
|
|
||
|
|
private:
|
||
|
|
static void check_payload(const std::string& p) {
|
||
|
|
if (p.empty() || p.size() > OpsCaps::kMaxSignPayloadBytes)
|
||
|
|
throw GoogleError::invalid("payload must be 1.." +
|
||
|
|
std::to_string(OpsCaps::kMaxSignPayloadBytes) + " bytes");
|
||
|
|
}
|
||
|
|
};
|
||
|
|
|
||
|
|
} // namespace molo::google::ops
|