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