1// Copyright 2014 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 "components/signin/core/browser/signin_oauth_helper.h"
6
7#include "base/message_loop/message_loop.h"
8#include "google_apis/gaia/gaia_auth_fetcher.h"
9#include "google_apis/gaia/gaia_constants.h"
10
11SigninOAuthHelper::SigninOAuthHelper(net::URLRequestContextGetter* getter,
12                                     const std::string& session_index,
13                                     const std::string& signin_scoped_device_id,
14                                     Consumer* consumer)
15    : gaia_auth_fetcher_(this, GaiaConstants::kChromeSource, getter),
16      consumer_(consumer) {
17  DCHECK(consumer_);
18  DCHECK(getter);
19  DCHECK(!session_index.empty());
20  gaia_auth_fetcher_.StartCookieForOAuthLoginTokenExchangeWithDeviceId(
21      session_index, signin_scoped_device_id);
22}
23
24SigninOAuthHelper::~SigninOAuthHelper() {}
25
26void SigninOAuthHelper::OnClientOAuthSuccess(const ClientOAuthResult& result) {
27  refresh_token_ = result.refresh_token;
28  gaia_auth_fetcher_.StartOAuthLogin(result.access_token,
29                                     GaiaConstants::kGaiaService);
30}
31
32void SigninOAuthHelper::OnClientOAuthFailure(
33    const GoogleServiceAuthError& error) {
34  VLOG(1) << "SigninOAuthHelper::OnClientOAuthFailure: " << error.ToString();
35  consumer_->OnSigninOAuthInformationFailure(error);
36}
37
38void SigninOAuthHelper::OnClientLoginSuccess(const ClientLoginResult& result) {
39  gaia_auth_fetcher_.StartGetUserInfo(result.lsid);
40}
41
42void SigninOAuthHelper::OnClientLoginFailure(
43    const GoogleServiceAuthError& error) {
44  VLOG(1) << "SigninOAuthHelper::OnClientLoginFailure: " << error.ToString();
45  consumer_->OnSigninOAuthInformationFailure(error);
46}
47
48void SigninOAuthHelper::OnGetUserInfoSuccess(const UserInfoMap& data) {
49  UserInfoMap::const_iterator email_iter = data.find("email");
50  UserInfoMap::const_iterator display_email_iter = data.find("displayEmail");
51  if (email_iter == data.end() || display_email_iter == data.end()) {
52    VLOG(1) << "SigninOAuthHelper::OnGetUserInfoSuccess: no email found:"
53            << " email=" << email_iter->second
54            << " displayEmail=" << display_email_iter->second;
55    consumer_->OnSigninOAuthInformationFailure(
56        GoogleServiceAuthError(GoogleServiceAuthError::SERVICE_ERROR));
57  } else {
58    VLOG(1) << "SigninOAuthHelper::OnGetUserInfoSuccess:"
59            << " email=" << email_iter->second
60            << " displayEmail=" << display_email_iter->second;
61    consumer_->OnSigninOAuthInformationAvailable(
62        email_iter->second, display_email_iter->second, refresh_token_);
63  }
64}
65
66void SigninOAuthHelper::OnGetUserInfoFailure(
67    const GoogleServiceAuthError& error) {
68  VLOG(1) << "SigninOAuthHelper::OnGetUserInfoFailure : " << error.ToString();
69  consumer_->OnSigninOAuthInformationFailure(error);
70}
71