15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright 2013 The Chromium Authors. All rights reserved.
25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file.
45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/ui/webui/chromeos/login/inline_login_handler_chromeos.h"
65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
7cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/chromeos/login/signin/oauth2_token_fetcher.h"
85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/profiles/profile.h"
95f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "chrome/browser/signin/chrome_signin_client_factory.h"
105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/signin/signin_manager_factory.h"
12cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/browser/signin/signin_promo.h"
13cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "chrome/common/url_constants.h"
14effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch#include "components/signin/core/browser/profile_oauth2_token_service.h"
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/signin/core/browser/signin_client.h"
16e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch#include "components/signin/core/browser/signin_manager.h"
17cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/storage_partition.h"
18cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "content/public/browser/web_contents.h"
195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "content/public/browser/web_ui.h"
20cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "google_apis/gaia/gaia_urls.h"
21cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)#include "net/base/url_util.h"
225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace chromeos {
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class InlineLoginHandlerChromeOS::InlineLoginUIOAuth2Delegate
265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    : public OAuth2TokenFetcher::Delegate {
275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public:
28cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  explicit InlineLoginUIOAuth2Delegate(content::WebUI* web_ui,
29cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)                                       const std::string& account_id)
30cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      : web_ui_(web_ui), account_id_(account_id) {}
31cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual ~InlineLoginUIOAuth2Delegate() {}
335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // OAuth2TokenFetcher::Delegate overrides:
355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnOAuth2TokensAvailable(
365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const GaiaAuthConsumer::ClientOAuthResult& oauth2_tokens) OVERRIDE {
375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // Closes sign-in dialog before update token service. Token service update
385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // might trigger a permission dialog and if this dialog does not close,
395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // a DCHECK would be triggered because attempting to activate a window
405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    // while there is a modal dialog.
415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    web_ui_->CallJavascriptFunction("inline.login.closeDialog");
425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    Profile* profile = Profile::FromWebUI(web_ui_);
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    ProfileOAuth2TokenService* token_service =
455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)        ProfileOAuth2TokenServiceFactory::GetForProfile(profile);
46cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)    token_service->UpdateCredentials(account_id_, oauth2_tokens.refresh_token);
475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void OnOAuth2TokensFetchFailed() OVERRIDE {
505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    LOG(ERROR) << "Failed to fetch oauth2 token with inline login.";
515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)    web_ui_->CallJavascriptFunction("inline.login.handleOAuth2TokenFailure");
525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  }
535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private:
555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  content::WebUI* web_ui_;
56cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::string account_id_;
57cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
58cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(InlineLoginUIOAuth2Delegate);
595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)};
605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)InlineLoginHandlerChromeOS::InlineLoginHandlerChromeOS() {}
625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)InlineLoginHandlerChromeOS::~InlineLoginHandlerChromeOS() {}
645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)void InlineLoginHandlerChromeOS::CompleteLogin(const base::ListValue* args) {
665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  Profile* profile = Profile::FromWebUI(web_ui());
675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
68cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  const base::DictionaryValue* dict = NULL;
69cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  args->GetDictionary(0, &dict);
70cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
71cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::string session_index;
72cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  dict->GetString("sessionIndex", &session_index);
73cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CHECK(!session_index.empty()) << "Session index is empty.";
74cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
75cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  std::string account_id;
76cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  dict->GetString("email", &account_id);
77cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  CHECK(!account_id.empty()) << "Account ID is empty.";
78cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)
79cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  oauth2_delegate_.reset(new InlineLoginUIOAuth2Delegate(web_ui(), account_id));
80cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  net::URLRequestContextGetter* request_context =
81cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      content::BrowserContext::GetStoragePartitionForSite(
82cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          profile, GURL(chrome::kChromeUIChromeSigninURL))
83cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)          ->GetURLRequestContext();
84cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  oauth2_token_fetcher_.reset(
85cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)      new OAuth2TokenFetcher(oauth2_delegate_.get(), request_context));
865f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  SigninClient* signin_client =
875f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      ChromeSigninClientFactory::GetForProfile(profile);
885f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  std::string signin_scoped_device_id =
895f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)      signin_client->GetSigninScopedDeviceId();
905f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)  oauth2_token_fetcher_->StartExchangeFromCookies(session_index,
915f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)                                                  signin_scoped_device_id);
925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}
935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace chromeos
95