| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/common/net/google_apis/google_api_keys.h" | |
| 6 | |
| 7 #include "base/command_line.h" | |
| 8 #include "base/environment.h" | |
| 9 #include "base/lazy_instance.h" | |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/stringize_macros.h" | |
| 12 | |
| 13 #if defined(GOOGLE_CHROME_BUILD) | |
| 14 // TODO(joi): Include a src-internal file that defines the official | |
| 15 // keys for official builds, something like: #include | |
| 16 // "chrome/app/internal/official_google_api_keys.h" | |
| 17 #endif | |
| 18 | |
| 19 #if !defined(GOOGLE_API_KEY) | |
| 20 // TODO(joi): This should be blank here, with the official key | |
| 21 // provided in official builds. | |
| 22 #define GOOGLE_API_KEY "abcNOTREALKEYxyz" | |
| 23 #endif | |
| 24 | |
| 25 #if !defined(GOOGLE_CLIENT_ID_MAIN) | |
| 26 // TODO(joi): This should be blank here, but provided in official | |
| 27 // builds. | |
| 28 #define GOOGLE_CLIENT_ID_MAIN "77185425430.apps.googleusercontent.com" | |
| 29 #endif | |
| 30 | |
| 31 #if !defined(GOOGLE_CLIENT_SECRET_MAIN) | |
| 32 // TODO(joi): This should be blank here, but provided in official | |
| 33 // builds. | |
| 34 #define GOOGLE_CLIENT_SECRET_MAIN "OTJgUOQcT7lO7GsGZq2G4IlT" | |
| 35 #endif | |
| 36 | |
| 37 #if !defined(GOOGLE_CLIENT_ID_CLOUD_PRINT) | |
| 38 // TODO(joi): This should be blank here, but provided in official | |
| 39 // builds. | |
| 40 #define GOOGLE_CLIENT_ID_CLOUD_PRINT "551556820943.apps.googleusercontent.com" | |
| 41 #endif | |
| 42 | |
| 43 #if !defined(GOOGLE_CLIENT_SECRET_CLOUD_PRINT) | |
| 44 // TODO(joi): This should be blank here, but provided in official | |
| 45 // builds. | |
| 46 #define GOOGLE_CLIENT_SECRET_CLOUD_PRINT "u3/mp8CgLFxh4uiX1855/MHe" | |
| 47 #endif | |
| 48 | |
| 49 #if !defined(GOOGLE_CLIENT_ID_REMOTING) | |
| 50 // TODO(joi): This should be blank here, but provided in official | |
| 51 // builds. | |
| 52 #define GOOGLE_CLIENT_ID_REMOTING \ | |
| 53 "440925447803-avn2sj1kc099s0r7v62je5s339mu0am1.apps.googleusercontent.com" | |
| 54 #endif | |
| 55 | |
| 56 #if !defined(GOOGLE_CLIENT_SECRET_REMOTING) | |
| 57 // TODO(joi): This should be blank here, but provided in official | |
| 58 // builds. | |
| 59 #define GOOGLE_CLIENT_SECRET_REMOTING "Bgur6DFiOMM1h8x-AQpuTQlK" | |
| 60 #endif | |
| 61 | |
| 62 // These are used as shortcuts for developers and users providing | |
| 63 // OAuth credentials via preprocessor defines or environment | |
| 64 // variables. If set, they will be used to replace any of the client | |
| 65 // IDs and secrets above that have not been set (and only those; they | |
| 66 // will not override already-set values). | |
| 67 #if !defined(GOOGLE_DEFAULT_CLIENT_ID) | |
| 68 #define GOOGLE_DEFAULT_CLIENT_ID "" | |
| 69 #endif | |
| 70 #if !defined(GOOGLE_DEFAULT_CLIENT_SECRET) | |
| 71 #define GOOGLE_DEFAULT_CLIENT_SECRET "" | |
| 72 #endif | |
| 73 | |
| 74 namespace switches { | |
| 75 | |
| 76 // Specifies custom OAuth2 client id for testing purposes. | |
| 77 const char kOAuth2ClientID[] = "oauth2-client-id"; | |
| 78 | |
| 79 // Specifies custom OAuth2 client secret for testing purposes. | |
| 80 const char kOAuth2ClientSecret[] = "oauth2-client-secret"; | |
| 81 | |
| 82 } // namespace switches | |
| 83 | |
| 84 namespace google_apis { | |
| 85 | |
| 86 namespace { | |
| 87 | |
| 88 // This is used as a lazy instance to determine keys once and cache them. | |
| 89 class APIKeyCache { | |
| 90 public: | |
| 91 APIKeyCache() { | |
| 92 scoped_ptr<base::Environment> environment(base::Environment::Create()); | |
| 93 CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 94 | |
| 95 api_key_ = CalculateKeyValue(GOOGLE_API_KEY, | |
| 96 STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY), | |
| 97 NULL, "", | |
| 98 environment.get(), | |
| 99 command_line); | |
| 100 | |
| 101 std::string default_client_id = CalculateKeyValue( | |
| 102 GOOGLE_DEFAULT_CLIENT_ID, | |
| 103 STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_ID), | |
| 104 NULL, "", | |
| 105 environment.get(), | |
| 106 command_line); | |
| 107 std::string default_client_secret = CalculateKeyValue( | |
| 108 GOOGLE_DEFAULT_CLIENT_SECRET, | |
| 109 STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_SECRET), | |
| 110 NULL, "", | |
| 111 environment.get(), | |
| 112 command_line); | |
| 113 | |
| 114 // We currently only allow overriding the baked-in values for the | |
| 115 // default OAuth2 client ID and secret using a command-line | |
| 116 // argument, since that is useful to enable testing against | |
| 117 // staging servers, and since that was what was possible and | |
| 118 // likely practiced by the QA team before this implementation was | |
| 119 // written. | |
| 120 client_ids_[CLIENT_MAIN] = CalculateKeyValue( | |
| 121 GOOGLE_CLIENT_ID_MAIN, | |
| 122 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_MAIN), | |
| 123 switches::kOAuth2ClientID, | |
| 124 default_client_id, | |
| 125 environment.get(), | |
| 126 command_line); | |
| 127 client_secrets_[CLIENT_MAIN] = CalculateKeyValue( | |
| 128 GOOGLE_CLIENT_SECRET_MAIN, | |
| 129 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_MAIN), | |
| 130 switches::kOAuth2ClientSecret, | |
| 131 default_client_secret, | |
| 132 environment.get(), | |
| 133 command_line); | |
| 134 | |
| 135 client_ids_[CLIENT_CLOUD_PRINT] = CalculateKeyValue( | |
| 136 GOOGLE_CLIENT_ID_CLOUD_PRINT, | |
| 137 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_CLOUD_PRINT), | |
| 138 NULL, | |
| 139 default_client_id, | |
| 140 environment.get(), | |
| 141 command_line); | |
| 142 client_secrets_[CLIENT_CLOUD_PRINT] = CalculateKeyValue( | |
| 143 GOOGLE_CLIENT_SECRET_CLOUD_PRINT, | |
| 144 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_CLOUD_PRINT), | |
| 145 NULL, | |
| 146 default_client_secret, | |
| 147 environment.get(), | |
| 148 command_line); | |
| 149 | |
| 150 client_ids_[CLIENT_REMOTING] = CalculateKeyValue( | |
| 151 GOOGLE_CLIENT_ID_REMOTING, | |
| 152 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_REMOTING), | |
| 153 NULL, | |
| 154 default_client_id, | |
| 155 environment.get(), | |
| 156 command_line); | |
| 157 client_secrets_[CLIENT_REMOTING] = CalculateKeyValue( | |
| 158 GOOGLE_CLIENT_SECRET_REMOTING, | |
| 159 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_REMOTING), | |
| 160 NULL, | |
| 161 default_client_secret, | |
| 162 environment.get(), | |
| 163 command_line); | |
| 164 } | |
| 165 | |
| 166 std::string api_key() const { return api_key_; } | |
| 167 | |
| 168 std::string GetClientID(OAuth2Client client) const { | |
| 169 DCHECK_LT(client, CLIENT_NUM_ITEMS); | |
| 170 return client_ids_[client]; | |
| 171 } | |
| 172 | |
| 173 std::string GetClientSecret(OAuth2Client client) const { | |
| 174 DCHECK_LT(client, CLIENT_NUM_ITEMS); | |
| 175 return client_secrets_[client]; | |
| 176 } | |
| 177 | |
| 178 private: | |
| 179 // Gets a value for a key. In priority order, this will be the value | |
| 180 // provided via a command-line switch, the value provided via an | |
| 181 // environment variable, or finally a value baked into the build. | |
| 182 // |command_line_switch| may be NULL. | |
| 183 static std::string CalculateKeyValue(const char* baked_in_value, | |
| 184 const char* environment_variable_name, | |
| 185 const char* command_line_switch, | |
| 186 const std::string& default_if_unset, | |
| 187 base::Environment* environment, | |
| 188 CommandLine* command_line) { | |
| 189 std::string key_value = baked_in_value; | |
| 190 std::string temp; | |
| 191 if (environment->GetVar(environment_variable_name, &temp)) | |
| 192 key_value = temp; | |
| 193 if (command_line_switch && command_line->HasSwitch(command_line_switch)) | |
| 194 key_value = command_line->GetSwitchValueASCII(command_line_switch); | |
| 195 | |
| 196 if (key_value.size() == 0) { | |
| 197 #if defined(GOOGLE_CHROME_BUILD) | |
| 198 // No key should be empty in an official build, except the | |
| 199 // default keys themselves, which will have an empty default. | |
| 200 CHECK(default_if_unset.size() == 0); | |
| 201 #endif | |
| 202 key_value = default_if_unset; | |
| 203 } | |
| 204 | |
| 205 return key_value; | |
| 206 } | |
| 207 | |
| 208 // If |default_value| itself is not empty, any empty strings in | |
| 209 // |array| (which has |array_count| items) will be replaced with a | |
| 210 // copy of the default value. | |
| 211 static void ReplaceEmptyItemsWithDefaultValue( | |
| 212 const std::string& default_value, | |
| 213 std::string* array, | |
| 214 size_t array_count) { | |
| 215 if (default_value.size() > 0) { | |
| 216 for (size_t i = 0; i < array_count; ++i) { | |
| 217 if (array[i].size() == 0) { | |
| 218 #if defined(GOOGLE_CHROME_BUILD) | |
| 219 // None of these should be empty in official builds. | |
| 220 CHECK(false); | |
| 221 #endif | |
| 222 array[i] = default_value; | |
| 223 } | |
| 224 } | |
| 225 } | |
| 226 } | |
| 227 | |
| 228 std::string api_key_; | |
| 229 std::string client_ids_[CLIENT_NUM_ITEMS]; | |
| 230 std::string client_secrets_[CLIENT_NUM_ITEMS]; | |
| 231 }; | |
| 232 | |
| 233 static base::LazyInstance<APIKeyCache> g_api_key_cache = | |
| 234 LAZY_INSTANCE_INITIALIZER; | |
| 235 | |
| 236 } // namespace | |
| 237 | |
| 238 std::string GetAPIKey() { | |
| 239 return g_api_key_cache.Get().api_key(); | |
| 240 } | |
| 241 | |
| 242 std::string GetOAuth2ClientID(OAuth2Client client) { | |
| 243 return g_api_key_cache.Get().GetClientID(client); | |
| 244 } | |
| 245 | |
| 246 std::string GetOAuth2ClientSecret(OAuth2Client client) { | |
| 247 return g_api_key_cache.Get().GetClientSecret(client); | |
| 248 } | |
| 249 | |
| 250 } // namespace google_apis | |
| OLD | NEW |