gaia_urls.cc revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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 "google_apis/gaia/gaia_urls.h"
6
7#include "base/command_line.h"
8#include "base/logging.h"
9#include "google_apis/gaia/gaia_switches.h"
10#include "google_apis/google_api_keys.h"
11
12namespace {
13
14// Gaia service constants
15const char kDefaultGaiaUrl[] = "https://accounts.google.com";
16const char kDefaultGoogleApisBaseUrl[] = "https://www.googleapis.com";
17
18// API calls from accounts.google.com
19const char kClientLoginUrlSuffix[] = "ClientLogin";
20const char kServiceLoginUrlSuffix[] = "ServiceLogin";
21const char kServiceLoginAuthUrlSuffix[] = "ServiceLoginAuth";
22const char kServiceLogoutUrlSuffix[] = "Logout";
23const char kIssueAuthTokenUrlSuffix[] = "IssueAuthToken";
24const char kGetUserInfoUrlSuffix[] = "GetUserInfo";
25const char kTokenAuthUrlSuffix[] = "TokenAuth";
26const char kMergeSessionUrlSuffix[] = "MergeSession";
27const char kOAuthGetAccessTokenUrlSuffix[] = "OAuthGetAccessToken";
28const char kOAuthWrapBridgeUrlSuffix[] = "OAuthWrapBridge";
29const char kOAuth1LoginUrlSuffix[] = "OAuthLogin";
30const char kOAuthRevokeTokenUrlSuffix[] = "AuthSubRevokeToken";
31
32// OAuth scopes
33const char kOAuth1LoginScope[] = "https://www.google.com/accounts/OAuthLogin";
34const char kOAuthWrapBridgeUserInfoScope[] =
35    "https://www.googleapis.com/auth/userinfo.email";
36
37// API calls from accounts.google.com (LSO)
38const char kGetOAuthTokenUrlSuffix[] = "o/oauth/GetOAuthToken/";
39const char kClientLoginToOAuth2UrlSuffix[] = "o/oauth2/programmatic_auth";
40const char kOAuth2AuthUrlSuffix[] = "o/oauth2/auth";
41const char kOAuth2RevokeUrlSuffix[] = "o/oauth2/revoke";
42const char kOAuth2TokenUrlSuffix[] = "o/oauth2/token";
43const char kClientOAuthUrlSuffix[] = "ClientOAuth";
44
45// API calls from www.googleapis.com
46const char kOAuth2IssueTokenUrlSuffix[] = "oauth2/v2/IssueToken";
47const char kOAuth2TokenInfoUrlSuffix[] = "oauth2/v2/tokeninfo";
48const char kOAuthUserInfoUrlSuffix[] = "oauth2/v1/userinfo";
49
50void GetSwitchValueWithDefault(const char* switch_value,
51                               const char* default_value,
52                               std::string* output_value) {
53  CommandLine* command_line = CommandLine::ForCurrentProcess();
54  if (command_line->HasSwitch(switch_value)) {
55    *output_value = command_line->GetSwitchValueASCII(switch_value);
56  } else {
57    *output_value = default_value;
58  }
59}
60
61GURL GetURLSwitchValueWithDefault(const char* switch_value,
62                                  const char* default_value) {
63  std::string string_value;
64  GetSwitchValueWithDefault(switch_value, default_value, &string_value);
65  const GURL result(string_value);
66  DCHECK(result.is_valid());
67  return result;
68}
69
70
71}  // namespace
72
73GaiaUrls* GaiaUrls::GetInstance() {
74  return Singleton<GaiaUrls>::get();
75}
76
77GaiaUrls::GaiaUrls() {
78  gaia_url_ = GetURLSwitchValueWithDefault(switches::kGaiaUrl, kDefaultGaiaUrl);
79  lso_origin_url_ =
80      GetURLSwitchValueWithDefault(switches::kLsoUrl, kDefaultGaiaUrl);
81  google_apis_origin_url_ = GetURLSwitchValueWithDefault(
82      switches::kGoogleApisUrl, kDefaultGoogleApisBaseUrl);
83
84  captcha_base_url_ =
85      GURL("http://" + gaia_url_.host() +
86           (gaia_url_.has_port() ? ":" + gaia_url_.port() : ""));
87
88  oauth2_chrome_client_id_ =
89      google_apis::GetOAuth2ClientID(google_apis::CLIENT_MAIN);
90  oauth2_chrome_client_secret_ =
91      google_apis::GetOAuth2ClientSecret(google_apis::CLIENT_MAIN);
92
93  // URLs from accounts.google.com.
94  client_login_url_ = gaia_url_.Resolve(kClientLoginUrlSuffix);
95  service_login_url_ = gaia_url_.Resolve(kServiceLoginUrlSuffix);
96  service_login_auth_url_ = gaia_url_.Resolve(kServiceLoginAuthUrlSuffix);
97  service_logout_url_ = gaia_url_.Resolve(kServiceLogoutUrlSuffix);
98  issue_auth_token_url_ = gaia_url_.Resolve(kIssueAuthTokenUrlSuffix);
99  get_user_info_url_ = gaia_url_.Resolve(kGetUserInfoUrlSuffix);
100  token_auth_url_ = gaia_url_.Resolve(kTokenAuthUrlSuffix);
101  merge_session_url_ = gaia_url_.Resolve(kMergeSessionUrlSuffix);
102  oauth_get_access_token_url_ =
103      gaia_url_.Resolve(kOAuthGetAccessTokenUrlSuffix);
104  oauth_wrap_bridge_url_ = gaia_url_.Resolve(kOAuthWrapBridgeUrlSuffix);
105  oauth_revoke_token_url_ = gaia_url_.Resolve(kOAuthRevokeTokenUrlSuffix);
106  oauth1_login_url_ = gaia_url_.Resolve(kOAuth1LoginUrlSuffix);
107
108  // URLs from accounts.google.com (LSO).
109  get_oauth_token_url_ = lso_origin_url_.Resolve(kGetOAuthTokenUrlSuffix);
110  client_login_to_oauth2_url_ =
111      lso_origin_url_.Resolve(kClientLoginToOAuth2UrlSuffix);
112  oauth2_auth_url_ = lso_origin_url_.Resolve(kOAuth2AuthUrlSuffix);
113  oauth2_token_url_ = lso_origin_url_.Resolve(kOAuth2TokenUrlSuffix);
114  oauth2_revoke_url_ = lso_origin_url_.Resolve(kOAuth2RevokeUrlSuffix);
115
116  // URLs from www.googleapis.com.
117  oauth2_issue_token_url_ =
118      google_apis_origin_url_.Resolve(kOAuth2IssueTokenUrlSuffix);
119  oauth2_token_info_url_ =
120      google_apis_origin_url_.Resolve(kOAuth2TokenInfoUrlSuffix);
121  oauth_user_info_url_ =
122      google_apis_origin_url_.Resolve(kOAuthUserInfoUrlSuffix);
123
124  gaia_login_form_realm_ = gaia_url_;
125
126  // OAuth scopes.
127  GetSwitchValueWithDefault(switches::kOAuthWrapBridgeUserInfoScope,
128                            kOAuthWrapBridgeUserInfoScope,
129                            &oauth_wrap_bridge_user_info_scope_);
130  GetSwitchValueWithDefault(switches::kOAuth1LoginScope,
131                            kOAuth1LoginScope,
132                            &oauth1_login_scope_);
133}
134
135GaiaUrls::~GaiaUrls() {
136}
137
138const GURL& GaiaUrls::gaia_url() const {
139  return gaia_url_;
140}
141
142const GURL& GaiaUrls::captcha_base_url() const {
143  return captcha_base_url_;
144}
145
146const GURL& GaiaUrls::client_login_url() const {
147  return client_login_url_;
148}
149
150const GURL& GaiaUrls::service_login_url() const {
151  return service_login_url_;
152}
153
154const GURL& GaiaUrls::service_login_auth_url() const {
155  return service_login_auth_url_;
156}
157
158const GURL& GaiaUrls::service_logout_url() const {
159  return service_logout_url_;
160}
161
162const GURL& GaiaUrls::issue_auth_token_url() const {
163  return issue_auth_token_url_;
164}
165
166const GURL& GaiaUrls::get_user_info_url() const {
167  return get_user_info_url_;
168}
169
170const GURL& GaiaUrls::token_auth_url() const {
171  return token_auth_url_;
172}
173
174const GURL& GaiaUrls::merge_session_url() const {
175  return merge_session_url_;
176}
177
178const GURL& GaiaUrls::get_oauth_token_url() const {
179  return get_oauth_token_url_;
180}
181
182const GURL& GaiaUrls::oauth_get_access_token_url() const {
183  return oauth_get_access_token_url_;
184}
185
186const GURL& GaiaUrls::oauth_wrap_bridge_url() const {
187  return oauth_wrap_bridge_url_;
188}
189
190const GURL& GaiaUrls::oauth_user_info_url() const {
191  return oauth_user_info_url_;
192}
193
194const GURL& GaiaUrls::oauth_revoke_token_url() const {
195  return oauth_revoke_token_url_;
196}
197
198const GURL& GaiaUrls::oauth1_login_url() const {
199  return oauth1_login_url_;
200}
201
202const std::string& GaiaUrls::oauth1_login_scope() const {
203  return oauth1_login_scope_;
204}
205
206const std::string& GaiaUrls::oauth_wrap_bridge_user_info_scope() const {
207  return oauth_wrap_bridge_user_info_scope_;
208}
209
210const std::string& GaiaUrls::oauth2_chrome_client_id() const {
211  return oauth2_chrome_client_id_;
212}
213
214const std::string& GaiaUrls::oauth2_chrome_client_secret() const {
215  return oauth2_chrome_client_secret_;
216}
217
218const GURL& GaiaUrls::client_login_to_oauth2_url() const {
219  return client_login_to_oauth2_url_;
220}
221
222const GURL& GaiaUrls::oauth2_auth_url() const {
223  return oauth2_auth_url_;
224}
225
226const GURL& GaiaUrls::oauth2_token_url() const {
227  return oauth2_token_url_;
228}
229
230const GURL& GaiaUrls::oauth2_issue_token_url() const {
231  return oauth2_issue_token_url_;
232}
233
234const GURL& GaiaUrls::oauth2_token_info_url() const {
235  return oauth2_token_info_url_;
236}
237
238const GURL& GaiaUrls::oauth2_revoke_url() const {
239  return oauth2_revoke_url_;
240}
241
242const GURL& GaiaUrls::gaia_login_form_realm() const {
243  return gaia_url_;
244}
245