google_api_keys.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. // Copyright 2012 The Chromium Authors
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "google_apis/google_api_keys.h"
  5. // If you add more includes to this list, you also need to add them to
  6. // google_api_keys_unittest.cc and google_api_keys_mac_unittest.mm.
  7. #include <stddef.h>
  8. #include <memory>
  9. #include <string>
  10. #include "base/command_line.h"
  11. #include "base/environment.h"
  12. #include "base/lazy_instance.h"
  13. #include "base/logging.h"
  14. #include "base/strings/stringize_macros.h"
  15. #include "build/branding_buildflags.h"
  16. #include "google_apis/buildflags.h"
  17. #include "google_apis/gaia/gaia_config.h"
  18. #include "google_apis/gaia/gaia_switches.h"
  19. #if BUILDFLAG(IS_APPLE)
  20. #include "google_apis/google_api_keys_mac.h"
  21. #endif
  22. #if defined(USE_OFFICIAL_GOOGLE_API_KEYS)
  23. #include "google_apis/internal/google_chrome_api_keys.h"
  24. #include "google_apis/internal/metrics_signing_key.h"
  25. #endif
  26. // Used to indicate an unset key/id/secret. This works better with
  27. // various unit tests than leaving the token empty.
  28. #define DUMMY_API_TOKEN "dummytoken"
  29. #if !defined(GOOGLE_API_KEY)
  30. #define GOOGLE_API_KEY DUMMY_API_TOKEN
  31. #endif
  32. #if !defined(GOOGLE_METRICS_SIGNING_KEY)
  33. #define GOOGLE_METRICS_SIGNING_KEY DUMMY_API_TOKEN
  34. #endif
  35. #if !defined(GOOGLE_CLIENT_ID_MAIN)
  36. #define GOOGLE_CLIENT_ID_MAIN DUMMY_API_TOKEN
  37. #endif
  38. #if !defined(GOOGLE_CLIENT_SECRET_MAIN)
  39. #define GOOGLE_CLIENT_SECRET_MAIN DUMMY_API_TOKEN
  40. #endif
  41. #if !defined(GOOGLE_CLIENT_ID_REMOTING)
  42. #define GOOGLE_CLIENT_ID_REMOTING DUMMY_API_TOKEN
  43. #endif
  44. #if !defined(GOOGLE_CLIENT_SECRET_REMOTING)
  45. #define GOOGLE_CLIENT_SECRET_REMOTING DUMMY_API_TOKEN
  46. #endif
  47. #if !defined(GOOGLE_CLIENT_ID_REMOTING_HOST)
  48. #define GOOGLE_CLIENT_ID_REMOTING_HOST DUMMY_API_TOKEN
  49. #endif
  50. #if !defined(GOOGLE_CLIENT_SECRET_REMOTING_HOST)
  51. #define GOOGLE_CLIENT_SECRET_REMOTING_HOST DUMMY_API_TOKEN
  52. #endif
  53. #if !defined(GOOGLE_API_KEY_ANDROID_NON_STABLE)
  54. #define GOOGLE_API_KEY_ANDROID_NON_STABLE DUMMY_API_TOKEN
  55. #endif
  56. #if !defined(GOOGLE_API_KEY_REMOTING)
  57. #define GOOGLE_API_KEY_REMOTING DUMMY_API_TOKEN
  58. #endif
  59. // API key for SharingService.
  60. #if !defined(GOOGLE_API_KEY_SHARING)
  61. #define GOOGLE_API_KEY_SHARING DUMMY_API_TOKEN
  62. #endif
  63. // API key for the Speech On-Device API (SODA).
  64. #if !defined(GOOGLE_API_KEY_SODA)
  65. #define GOOGLE_API_KEY_SODA DUMMY_API_TOKEN
  66. #endif
  67. // API key for the ReadAloud API.
  68. #if !defined(GOOGLE_API_KEY_READ_ALOUD)
  69. #define GOOGLE_API_KEY_READ_ALOUD DUMMY_API_TOKEN
  70. #endif
  71. // API key for the Fresnel API.
  72. #if !defined(GOOGLE_API_KEY_FRESNEL)
  73. #define GOOGLE_API_KEY_FRESNEL DUMMY_API_TOKEN
  74. #endif
  75. // These are used as shortcuts for developers and users providing
  76. // OAuth credentials via preprocessor defines or environment
  77. // variables. If set, they will be used to replace any of the client
  78. // IDs and secrets above that have not been set (and only those; they
  79. // will not override already-set values).
  80. #if !defined(GOOGLE_DEFAULT_CLIENT_ID)
  81. #define GOOGLE_DEFAULT_CLIENT_ID ""
  82. #endif
  83. #if !defined(GOOGLE_DEFAULT_CLIENT_SECRET)
  84. #define GOOGLE_DEFAULT_CLIENT_SECRET ""
  85. #endif
  86. namespace google_apis {
  87. const char kAPIKeysDevelopersHowToURL[] =
  88. "https://www.chromium.org/developers/how-tos/api-keys";
  89. // This is used as a lazy instance to determine keys once and cache them.
  90. class APIKeyCache {
  91. public:
  92. APIKeyCache() {
  93. std::unique_ptr<base::Environment> environment(base::Environment::Create());
  94. base::CommandLine* command_line = base::CommandLine::ForCurrentProcess();
  95. GaiaConfig* gaia_config = GaiaConfig::GetInstance();
  96. api_key_ = CalculateKeyValue(
  97. GOOGLE_API_KEY, STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY), nullptr,
  98. std::string(), environment.get(), command_line, gaia_config);
  99. // A special non-stable key is at the moment defined only for Android Chrome.
  100. #if BUILDFLAG(IS_ANDROID)
  101. api_key_non_stable_ = CalculateKeyValue(
  102. GOOGLE_API_KEY_ANDROID_NON_STABLE,
  103. STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY_ANDROID_NON_STABLE), nullptr,
  104. std::string(), environment.get(), command_line, gaia_config);
  105. #else
  106. api_key_non_stable_ = api_key_;
  107. #endif
  108. api_key_remoting_ = CalculateKeyValue(
  109. GOOGLE_API_KEY_REMOTING,
  110. STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY_REMOTING), nullptr, std::string(),
  111. environment.get(), command_line, gaia_config);
  112. api_key_sharing_ = CalculateKeyValue(
  113. GOOGLE_API_KEY_SHARING, STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY_SHARING),
  114. nullptr, std::string(), environment.get(), command_line, gaia_config);
  115. api_key_soda_ = CalculateKeyValue(
  116. GOOGLE_API_KEY_SODA, STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY_SODA),
  117. nullptr, std::string(), environment.get(), command_line, gaia_config);
  118. api_key_read_aloud_ = CalculateKeyValue(
  119. GOOGLE_API_KEY_READ_ALOUD,
  120. STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY_READ_ALOUD), nullptr,
  121. std::string(), environment.get(), command_line, gaia_config);
  122. api_key_fresnel_ = CalculateKeyValue(
  123. GOOGLE_API_KEY_FRESNEL, STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY_FRESNEL),
  124. nullptr, std::string(), environment.get(), command_line, gaia_config);
  125. metrics_key_ = CalculateKeyValue(
  126. GOOGLE_METRICS_SIGNING_KEY,
  127. STRINGIZE_NO_EXPANSION(GOOGLE_METRICS_SIGNING_KEY), nullptr,
  128. std::string(), environment.get(), command_line, gaia_config);
  129. std::string default_client_id = CalculateKeyValue(
  130. GOOGLE_DEFAULT_CLIENT_ID,
  131. STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_ID), nullptr,
  132. std::string(), environment.get(), command_line, gaia_config);
  133. std::string default_client_secret = CalculateKeyValue(
  134. GOOGLE_DEFAULT_CLIENT_SECRET,
  135. STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_SECRET), nullptr,
  136. std::string(), environment.get(), command_line, gaia_config);
  137. // We currently only allow overriding the baked-in values for the
  138. // default OAuth2 client ID and secret using a command-line
  139. // argument and gaia config, since that is useful to enable testing against
  140. // staging servers, and since that was what was possible and
  141. // likely practiced by the QA team before this implementation was
  142. // written.
  143. client_ids_[CLIENT_MAIN] = CalculateKeyValue(
  144. GOOGLE_CLIENT_ID_MAIN, STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_MAIN),
  145. ::switches::kOAuth2ClientID, default_client_id, environment.get(),
  146. command_line, gaia_config);
  147. client_secrets_[CLIENT_MAIN] = CalculateKeyValue(
  148. GOOGLE_CLIENT_SECRET_MAIN,
  149. STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_MAIN),
  150. ::switches::kOAuth2ClientSecret, default_client_secret,
  151. environment.get(), command_line, gaia_config);
  152. client_ids_[CLIENT_REMOTING] = CalculateKeyValue(
  153. GOOGLE_CLIENT_ID_REMOTING,
  154. STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_REMOTING), nullptr,
  155. default_client_id, environment.get(), command_line, gaia_config);
  156. client_secrets_[CLIENT_REMOTING] = CalculateKeyValue(
  157. GOOGLE_CLIENT_SECRET_REMOTING,
  158. STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_REMOTING), nullptr,
  159. default_client_secret, environment.get(), command_line, gaia_config);
  160. client_ids_[CLIENT_REMOTING_HOST] = CalculateKeyValue(
  161. GOOGLE_CLIENT_ID_REMOTING_HOST,
  162. STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_REMOTING_HOST), nullptr,
  163. default_client_id, environment.get(), command_line, gaia_config);
  164. client_secrets_[CLIENT_REMOTING_HOST] = CalculateKeyValue(
  165. GOOGLE_CLIENT_SECRET_REMOTING_HOST,
  166. STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_REMOTING_HOST), nullptr,
  167. default_client_secret, environment.get(), command_line, gaia_config);
  168. }
  169. std::string api_key() const { return api_key_; }
  170. #if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)
  171. void set_api_key(const std::string& api_key) { api_key_ = api_key; }
  172. #endif
  173. std::string api_key_non_stable() const { return api_key_non_stable_; }
  174. std::string api_key_remoting() const { return api_key_remoting_; }
  175. std::string api_key_sharing() const { return api_key_sharing_; }
  176. std::string api_key_soda() const { return api_key_soda_; }
  177. std::string api_key_read_aloud() const { return api_key_read_aloud_; }
  178. std::string api_key_fresnel() const { return api_key_fresnel_; }
  179. std::string metrics_key() const { return metrics_key_; }
  180. std::string GetClientID(OAuth2Client client) const {
  181. DCHECK_LT(client, CLIENT_NUM_ITEMS);
  182. return client_ids_[client];
  183. }
  184. #if BUILDFLAG(IS_IOS)
  185. void SetClientID(OAuth2Client client, const std::string& client_id) {
  186. client_ids_[client] = client_id;
  187. }
  188. #endif
  189. std::string GetClientSecret(OAuth2Client client) const {
  190. DCHECK_LT(client, CLIENT_NUM_ITEMS);
  191. return client_secrets_[client];
  192. }
  193. #if BUILDFLAG(IS_IOS)
  194. void SetClientSecret(OAuth2Client client, const std::string& client_secret) {
  195. client_secrets_[client] = client_secret;
  196. }
  197. #endif
  198. std::string GetSpdyProxyAuthValue() {
  199. #if defined(SPDY_PROXY_AUTH_VALUE)
  200. return SPDY_PROXY_AUTH_VALUE;
  201. #else
  202. return std::string();
  203. #endif
  204. }
  205. private:
  206. // Gets a value for a key. In priority order, this will be the value
  207. // provided via:
  208. // 1. Command-line switch
  209. // 2. Config file
  210. // 3. Environment variable
  211. // 4. Baked into the build
  212. // |command_line_switch| may be NULL. Official Google Chrome builds will not
  213. // use the value provided by an environment variable.
  214. static std::string CalculateKeyValue(const char* baked_in_value,
  215. const char* environment_variable_name,
  216. const char* command_line_switch,
  217. const std::string& default_if_unset,
  218. base::Environment* environment,
  219. base::CommandLine* command_line,
  220. GaiaConfig* gaia_config) {
  221. std::string key_value = baked_in_value;
  222. std::string temp;
  223. #if BUILDFLAG(IS_APPLE)
  224. // macOS and iOS can also override the API key with a value from the
  225. // Info.plist.
  226. temp = ::google_apis::GetAPIKeyFromInfoPlist(environment_variable_name);
  227. if (!temp.empty()) {
  228. key_value = temp;
  229. VLOG(1) << "Overriding API key " << environment_variable_name
  230. << " with value " << key_value << " from Info.plist.";
  231. }
  232. #endif
  233. #if !BUILDFLAG(GOOGLE_CHROME_BRANDING)
  234. // Don't allow using the environment to override API keys for official
  235. // Google Chrome builds. There have been reports of mangled environments
  236. // affecting users (crbug.com/710575).
  237. if (environment->GetVar(environment_variable_name, &temp)) {
  238. key_value = temp;
  239. VLOG(1) << "Overriding API key " << environment_variable_name
  240. << " with value " << key_value << " from environment variable.";
  241. }
  242. #endif
  243. if (gaia_config &&
  244. gaia_config->GetAPIKeyIfExists(environment_variable_name, &temp)) {
  245. key_value = temp;
  246. VLOG(1) << "Overriding API key " << environment_variable_name
  247. << " with value " << key_value << " from gaia config.";
  248. }
  249. if (command_line_switch && command_line->HasSwitch(command_line_switch)) {
  250. key_value = command_line->GetSwitchValueASCII(command_line_switch);
  251. VLOG(1) << "Overriding API key " << environment_variable_name
  252. << " with value " << key_value << " from command-line switch.";
  253. }
  254. if (key_value == DUMMY_API_TOKEN) {
  255. // TODO(crbug.com/1294915): Rewrite this condition using
  256. // BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY).
  257. #if BUILDFLAG(GOOGLE_CHROME_BRANDING) && !BUILDFLAG(IS_FUCHSIA)
  258. // No key should be unset in an official build except the
  259. // GOOGLE_DEFAULT_* keys. The default keys don't trigger this
  260. // check as their "unset" value is not DUMMY_API_TOKEN.
  261. CHECK(false);
  262. #endif
  263. if (default_if_unset.size() > 0) {
  264. VLOG(1) << "Using default value \"" << default_if_unset
  265. << "\" for API key " << environment_variable_name;
  266. key_value = default_if_unset;
  267. }
  268. }
  269. // This should remain a debug-only log.
  270. DVLOG(1) << "API key " << environment_variable_name << "=" << key_value;
  271. return key_value;
  272. }
  273. std::string api_key_;
  274. std::string api_key_non_stable_;
  275. std::string api_key_remoting_;
  276. std::string api_key_sharing_;
  277. std::string api_key_soda_;
  278. std::string api_key_read_aloud_;
  279. std::string api_key_fresnel_;
  280. std::string metrics_key_;
  281. std::string client_ids_[CLIENT_NUM_ITEMS];
  282. std::string client_secrets_[CLIENT_NUM_ITEMS];
  283. };
  284. static base::LazyInstance<APIKeyCache>::DestructorAtExit g_api_key_cache =
  285. LAZY_INSTANCE_INITIALIZER;
  286. bool HasAPIKeyConfigured() {
  287. return GetAPIKey() != DUMMY_API_TOKEN;
  288. }
  289. std::string GetAPIKey() {
  290. return g_api_key_cache.Get().api_key();
  291. }
  292. std::string GetNonStableAPIKey() {
  293. return g_api_key_cache.Get().api_key_non_stable();
  294. }
  295. std::string GetRemotingAPIKey() {
  296. return g_api_key_cache.Get().api_key_remoting();
  297. }
  298. std::string GetSharingAPIKey() {
  299. return g_api_key_cache.Get().api_key_sharing();
  300. }
  301. std::string GetSodaAPIKey() {
  302. return g_api_key_cache.Get().api_key_soda();
  303. }
  304. std::string GetReadAloudAPIKey() {
  305. return g_api_key_cache.Get().api_key_read_aloud();
  306. }
  307. std::string GetFresnelAPIKey() {
  308. return g_api_key_cache.Get().api_key_fresnel();
  309. }
  310. #if BUILDFLAG(SUPPORT_EXTERNAL_GOOGLE_API_KEY)
  311. void SetAPIKey(const std::string& api_key) {
  312. g_api_key_cache.Get().set_api_key(api_key);
  313. }
  314. #endif
  315. std::string GetMetricsKey() {
  316. return g_api_key_cache.Get().metrics_key();
  317. }
  318. bool HasOAuthClientConfigured() {
  319. for (size_t client_id = 0; client_id < CLIENT_NUM_ITEMS; ++client_id) {
  320. OAuth2Client client = static_cast<OAuth2Client>(client_id);
  321. if (GetOAuth2ClientID(client) == DUMMY_API_TOKEN ||
  322. GetOAuth2ClientSecret(client) == DUMMY_API_TOKEN) {
  323. return false;
  324. }
  325. }
  326. return true;
  327. }
  328. std::string GetOAuth2ClientID(OAuth2Client client) {
  329. return g_api_key_cache.Get().GetClientID(client);
  330. }
  331. std::string GetOAuth2ClientSecret(OAuth2Client client) {
  332. return g_api_key_cache.Get().GetClientSecret(client);
  333. }
  334. #if BUILDFLAG(IS_IOS)
  335. void SetOAuth2ClientID(OAuth2Client client, const std::string& client_id) {
  336. g_api_key_cache.Get().SetClientID(client, client_id);
  337. }
  338. void SetOAuth2ClientSecret(OAuth2Client client,
  339. const std::string& client_secret) {
  340. g_api_key_cache.Get().SetClientSecret(client, client_secret);
  341. }
  342. #endif
  343. std::string GetSpdyProxyAuthValue() {
  344. return g_api_key_cache.Get().GetSpdyProxyAuthValue();
  345. }
  346. bool IsGoogleChromeAPIKeyUsed() {
  347. #if defined(USE_OFFICIAL_GOOGLE_API_KEYS)
  348. return true;
  349. #else
  350. return false;
  351. #endif
  352. }
  353. } // namespace google_apis