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