68 lines
2.9 KiB
C++
68 lines
2.9 KiB
C++
#pragma once
|
|
|
|
// Maps Platform API-key handling. A Maps key is a Cloud API key restricted by
|
|
// application (referrer / IP / Android / iOS) and by API target.
|
|
// - MapsKey: env lookup (GOOGLE_MAPS_API_KEY, else GOOGLE_API_KEY), shape
|
|
// check, redaction, and a GoogleClient that sends it in x-goog-api-key.
|
|
// - KeyRestrictions: a local description of a key's restrictions, validated
|
|
// against the API Keys v2 schema (V2Restrictions), with a safety lint
|
|
// (an unrestricted key is reported).
|
|
// - ApiKeys: read-only list and get through the API Keys API (OAuth). The
|
|
// secret key string is never read: getKeyString is deliberately absent.
|
|
|
|
#include <memory>
|
|
#include <string>
|
|
#include <utility>
|
|
#include <vector>
|
|
|
|
#include "clients/body/json.h"
|
|
#include "google/maps/maps_service.h"
|
|
|
|
namespace molo::google::maps {
|
|
|
|
class MapsKey {
|
|
public:
|
|
explicit MapsKey(std::string key = "");
|
|
// GOOGLE_MAPS_API_KEY, else GOOGLE_API_KEY (process env, .env.json.local, .env.json).
|
|
static MapsKey from_env();
|
|
bool present() const { return !key_.empty(); }
|
|
// Classic Cloud API keys are "AIza" + 35 URL-safe characters. Vertex express
|
|
// keys ("AQ.") are a different credential and are NOT Maps keys.
|
|
bool looks_like_maps_key() const;
|
|
// "AIza...wxyz" style: first 4 and last 4 only; "" when absent.
|
|
std::string redacted() const;
|
|
// Never exposes the key; a client that sends it as x-goog-api-key.
|
|
std::shared_ptr<GoogleClient> make_client() const;
|
|
|
|
private:
|
|
std::string key_;
|
|
};
|
|
|
|
struct KeyRestrictions {
|
|
std::vector<std::string> allowed_referrers; // browser keys, e.g. "https://*.example.com/*"
|
|
std::vector<std::string> allowed_ips; // server keys, IPv4/IPv6/CIDR
|
|
std::vector<std::string> allowed_bundle_ids; // iOS
|
|
std::vector<std::pair<std::string, std::string>> android_apps; // Android: (package, sha1 fingerprint)
|
|
std::vector<std::string> api_services; // apiTargets[].service, e.g. "places.googleapis.com"
|
|
void validate() const;
|
|
// V2Restrictions document. Only one application restriction kind is allowed.
|
|
clients::body::Json to_json() const;
|
|
// Findings that make a key unsafe to ship ("no application restriction", ...).
|
|
std::vector<std::string> lint() const;
|
|
static KeyRestrictions from_json(const clients::body::Json& restrictions);
|
|
};
|
|
|
|
class ApiKeys : public MapsService {
|
|
public:
|
|
explicit ApiKeys(std::shared_ptr<GoogleClient> client);
|
|
// project like "my-project" or "123456"; location is always global.
|
|
clients::body::Json list(const std::string& project, int page_size = 0,
|
|
const std::string& page_token = "") const;
|
|
// Key resource name "projects/{p}/locations/global/keys/{uid}".
|
|
clients::body::Json get(const std::string& key_name) const;
|
|
// The restrictions of a listed key, as local value.
|
|
static std::vector<std::pair<std::string, KeyRestrictions>> restrictions_of(const clients::body::Json& list);
|
|
};
|
|
|
|
} // namespace molo::google::maps
|