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.
5 #include "google_apis/google_api_keys.h"
7 // If you add more includes to this list, you also need to add them to
8 // google_api_keys_unittest.cc.
9 #include "base/command_line.h"
10 #include "base/environment.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/memory/scoped_ptr.h"
14 #include "base/strings/stringize_macros.h"
15 #include "google_apis/gaia/gaia_switches.h"
17 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS)
18 #include "google_apis/internal/google_chrome_api_keys.h"
21 // Used to indicate an unset key/id/secret. This works better with
22 // various unit tests than leaving the token empty.
23 #define DUMMY_API_TOKEN "dummytoken"
25 #if !defined(GOOGLE_API_KEY)
26 #define GOOGLE_API_KEY DUMMY_API_TOKEN
29 #if !defined(GOOGLE_CLIENT_ID_MAIN)
30 #define GOOGLE_CLIENT_ID_MAIN DUMMY_API_TOKEN
33 #if !defined(GOOGLE_CLIENT_SECRET_MAIN)
34 #define GOOGLE_CLIENT_SECRET_MAIN DUMMY_API_TOKEN
37 #if !defined(GOOGLE_CLIENT_ID_CLOUD_PRINT)
38 #define GOOGLE_CLIENT_ID_CLOUD_PRINT DUMMY_API_TOKEN
41 #if !defined(GOOGLE_CLIENT_SECRET_CLOUD_PRINT)
42 #define GOOGLE_CLIENT_SECRET_CLOUD_PRINT DUMMY_API_TOKEN
45 #if !defined(GOOGLE_CLIENT_ID_REMOTING)
46 #define GOOGLE_CLIENT_ID_REMOTING DUMMY_API_TOKEN
49 #if !defined(GOOGLE_CLIENT_SECRET_REMOTING)
50 #define GOOGLE_CLIENT_SECRET_REMOTING DUMMY_API_TOKEN
53 #if !defined(GOOGLE_CLIENT_ID_REMOTING_HOST)
54 #define GOOGLE_CLIENT_ID_REMOTING_HOST DUMMY_API_TOKEN
57 #if !defined(GOOGLE_CLIENT_SECRET_REMOTING_HOST)
58 #define GOOGLE_CLIENT_SECRET_REMOTING_HOST DUMMY_API_TOKEN
61 // These are used as shortcuts for developers and users providing
62 // OAuth credentials via preprocessor defines or environment
63 // variables. If set, they will be used to replace any of the client
64 // IDs and secrets above that have not been set (and only those; they
65 // will not override already-set values).
66 #if !defined(GOOGLE_DEFAULT_CLIENT_ID)
67 #define GOOGLE_DEFAULT_CLIENT_ID ""
69 #if !defined(GOOGLE_DEFAULT_CLIENT_SECRET)
70 #define GOOGLE_DEFAULT_CLIENT_SECRET ""
73 namespace google_apis
{
75 // This is used as a lazy instance to determine keys once and cache them.
79 scoped_ptr
<base::Environment
> environment(base::Environment::Create());
80 CommandLine
* command_line
= CommandLine::ForCurrentProcess();
82 api_key_
= CalculateKeyValue(GOOGLE_API_KEY
,
83 STRINGIZE_NO_EXPANSION(GOOGLE_API_KEY
),
89 std::string default_client_id
=
90 CalculateKeyValue(GOOGLE_DEFAULT_CLIENT_ID
,
91 STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_ID
),
96 std::string default_client_secret
=
97 CalculateKeyValue(GOOGLE_DEFAULT_CLIENT_SECRET
,
98 STRINGIZE_NO_EXPANSION(GOOGLE_DEFAULT_CLIENT_SECRET
),
104 // We currently only allow overriding the baked-in values for the
105 // default OAuth2 client ID and secret using a command-line
106 // argument, since that is useful to enable testing against
107 // staging servers, and since that was what was possible and
108 // likely practiced by the QA team before this implementation was
110 client_ids_
[CLIENT_MAIN
] = CalculateKeyValue(
111 GOOGLE_CLIENT_ID_MAIN
,
112 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_MAIN
),
113 switches::kOAuth2ClientID
,
117 client_secrets_
[CLIENT_MAIN
] = CalculateKeyValue(
118 GOOGLE_CLIENT_SECRET_MAIN
,
119 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_MAIN
),
120 switches::kOAuth2ClientSecret
,
121 default_client_secret
,
125 client_ids_
[CLIENT_CLOUD_PRINT
] = CalculateKeyValue(
126 GOOGLE_CLIENT_ID_CLOUD_PRINT
,
127 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_CLOUD_PRINT
),
132 client_secrets_
[CLIENT_CLOUD_PRINT
] = CalculateKeyValue(
133 GOOGLE_CLIENT_SECRET_CLOUD_PRINT
,
134 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_CLOUD_PRINT
),
136 default_client_secret
,
140 client_ids_
[CLIENT_REMOTING
] = CalculateKeyValue(
141 GOOGLE_CLIENT_ID_REMOTING
,
142 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_REMOTING
),
147 client_secrets_
[CLIENT_REMOTING
] = CalculateKeyValue(
148 GOOGLE_CLIENT_SECRET_REMOTING
,
149 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_REMOTING
),
151 default_client_secret
,
155 client_ids_
[CLIENT_REMOTING_HOST
] = CalculateKeyValue(
156 GOOGLE_CLIENT_ID_REMOTING_HOST
,
157 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_ID_REMOTING_HOST
),
162 client_secrets_
[CLIENT_REMOTING_HOST
] = CalculateKeyValue(
163 GOOGLE_CLIENT_SECRET_REMOTING_HOST
,
164 STRINGIZE_NO_EXPANSION(GOOGLE_CLIENT_SECRET_REMOTING_HOST
),
166 default_client_secret
,
171 std::string
api_key() const { return api_key_
; }
173 std::string
GetClientID(OAuth2Client client
) const {
174 DCHECK_LT(client
, CLIENT_NUM_ITEMS
);
175 return client_ids_
[client
];
178 std::string
GetClientSecret(OAuth2Client client
) const {
179 DCHECK_LT(client
, CLIENT_NUM_ITEMS
);
180 return client_secrets_
[client
];
183 std::string
GetSpdyProxyAuthValue() {
184 #if defined(SPDY_PROXY_AUTH_VALUE)
185 return SPDY_PROXY_AUTH_VALUE
;
187 return std::string();
192 // Gets a value for a key. In priority order, this will be the value
193 // provided via a command-line switch, the value provided via an
194 // environment variable, or finally a value baked into the build.
195 // |command_line_switch| may be NULL.
196 static std::string
CalculateKeyValue(const char* baked_in_value
,
197 const char* environment_variable_name
,
198 const char* command_line_switch
,
199 const std::string
& default_if_unset
,
200 base::Environment
* environment
,
201 CommandLine
* command_line
) {
202 std::string key_value
= baked_in_value
;
204 if (environment
->GetVar(environment_variable_name
, &temp
)) {
206 VLOG(1) << "Overriding API key " << environment_variable_name
207 << " with value " << key_value
<< " from environment variable.";
210 if (command_line_switch
&& command_line
->HasSwitch(command_line_switch
)) {
211 key_value
= command_line
->GetSwitchValueASCII(command_line_switch
);
212 VLOG(1) << "Overriding API key " << environment_variable_name
213 << " with value " << key_value
<< " from command-line switch.";
216 if (key_value
== DUMMY_API_TOKEN
) {
217 #if defined(GOOGLE_CHROME_BUILD)
218 // No key should be unset in an official build except the
219 // GOOGLE_DEFAULT_* keys. The default keys don't trigger this
220 // check as their "unset" value is not DUMMY_API_TOKEN.
223 if (default_if_unset
.size() > 0) {
224 VLOG(1) << "Using default value \"" << default_if_unset
225 << "\" for API key " << environment_variable_name
;
226 key_value
= default_if_unset
;
230 // This should remain a debug-only log.
231 DVLOG(1) << "API key " << environment_variable_name
<< "=" << key_value
;
236 std::string api_key_
;
237 std::string client_ids_
[CLIENT_NUM_ITEMS
];
238 std::string client_secrets_
[CLIENT_NUM_ITEMS
];
241 static base::LazyInstance
<APIKeyCache
> g_api_key_cache
=
242 LAZY_INSTANCE_INITIALIZER
;
244 bool HasKeysConfigured() {
245 if (GetAPIKey() == DUMMY_API_TOKEN
)
248 for (size_t client_id
= 0; client_id
< CLIENT_NUM_ITEMS
; ++client_id
) {
249 OAuth2Client client
= static_cast<OAuth2Client
>(client_id
);
250 if (GetOAuth2ClientID(client
) == DUMMY_API_TOKEN
||
251 GetOAuth2ClientSecret(client
) == DUMMY_API_TOKEN
) {
259 std::string
GetAPIKey() {
260 return g_api_key_cache
.Get().api_key();
263 std::string
GetOAuth2ClientID(OAuth2Client client
) {
264 return g_api_key_cache
.Get().GetClientID(client
);
267 std::string
GetOAuth2ClientSecret(OAuth2Client client
) {
268 return g_api_key_cache
.Get().GetClientSecret(client
);
271 std::string
GetSpdyProxyAuthValue() {
272 return g_api_key_cache
.Get().GetSpdyProxyAuthValue();
275 bool IsGoogleChromeAPIKeyUsed() {
276 #if defined(GOOGLE_CHROME_BUILD) || defined(USE_OFFICIAL_GOOGLE_API_KEYS)
283 } // namespace google_apis