summaryrefslogtreecommitdiff
path: root/chromium/components/autofill/content/browser/wallet/wallet_signin_helper.cc
blob: fc9b66fad232324382dc2e8bd413d442438ccc55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/autofill/content/browser/wallet/wallet_signin_helper.h"

#include "base/callback_helpers.h"
#include "base/json/json_reader.h"
#include "base/logging.h"
#include "base/rand_util.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/time/time.h"
#include "base/values.h"
#include "components/autofill/content/browser/wallet/wallet_service_url.h"
#include "components/autofill/content/browser/wallet/wallet_signin_helper_delegate.h"
#include "content/public/browser/browser_thread.h"
#include "google_apis/gaia/google_service_auth_error.h"
#include "net/base/escape.h"
#include "net/cookies/canonical_cookie.h"
#include "net/cookies/cookie_monster.h"
#include "net/cookies/cookie_options.h"
#include "net/cookies/cookie_store.h"
#include "net/url_request/url_fetcher.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_context_getter.h"

namespace autofill {
namespace wallet {

namespace {

const char kWalletCookieName[] = "gdtoken";

// Callback for retrieving Google Wallet cookies. |callback| is passed the
// retrieved cookies and posted back to the UI thread. |cookies| is any Google
// Wallet cookies.
void GetGoogleCookiesCallback(
    const base::Callback<void(const std::string&)>& callback,
    const net::CookieList& cookies) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));

  // Cookies for parent domains will also be returned; we only want cookies with
  // exact host matches. TODO(estade): really?
  std::string host = wallet::GetPassiveAuthUrl(0).host();
  std::string wallet_cookie;
  for (size_t i = 0; i < cookies.size(); ++i) {
    if (LowerCaseEqualsASCII(cookies[i].Name(), kWalletCookieName) &&
        LowerCaseEqualsASCII(cookies[i].Domain(), host.c_str())) {
      wallet_cookie = cookies[i].Value();
      break;
    }
  }
  content::BrowserThread::PostTask(content::BrowserThread::UI,
                                   FROM_HERE,
                                   base::Bind(callback, wallet_cookie));
}

// Gets Google Wallet cookies. Must be called on the IO thread.
// |request_context_getter| is a getter for the current request context.
// |callback| is called when retrieving cookies is completed.
void GetGoogleCookies(
    scoped_refptr<net::URLRequestContextGetter> request_context_getter,
    const base::Callback<void(const std::string&)>& callback) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));

  net::URLRequestContext* url_request_context =
      request_context_getter->GetURLRequestContext();
  net::CookieStore* cookie_store = url_request_context ?
      url_request_context->cookie_store() : NULL;
  net::CookieMonster* cookie_monster = cookie_store ?
      cookie_store->GetCookieMonster() : NULL;
  if (!cookie_monster) {
    content::BrowserThread::PostTask(content::BrowserThread::UI,
                                     FROM_HERE,
                                     base::Bind(callback, std::string()));
    return;
  }

  net::CookieOptions cookie_options;
  cookie_options.set_include_httponly();
  cookie_monster->GetAllCookiesForURLWithOptionsAsync(
      wallet::GetPassiveAuthUrl(0).GetWithEmptyPath(),
      cookie_options,
      base::Bind(&GetGoogleCookiesCallback, callback));
}

}  // namespace

WalletSigninHelper::WalletSigninHelper(
    WalletSigninHelperDelegate* delegate,
    net::URLRequestContextGetter* getter)
    : delegate_(delegate),
      getter_(getter),
      weak_ptr_factory_(this) {
  DCHECK(delegate_);
}

WalletSigninHelper::~WalletSigninHelper() {
}

void WalletSigninHelper::StartPassiveSignin(size_t user_index) {
  DCHECK(!url_fetcher_);

  const GURL& url = wallet::GetPassiveAuthUrl(user_index);
  url_fetcher_.reset(net::URLFetcher::Create(
      0, url, net::URLFetcher::GET, this));
  url_fetcher_->SetRequestContext(getter_);
  url_fetcher_->Start();
}

void WalletSigninHelper::StartWalletCookieValueFetch() {
  scoped_refptr<net::URLRequestContextGetter> request_context(getter_);
  if (!request_context.get()) {
    ReturnWalletCookieValue(std::string());
    return;
  }

  base::Callback<void(const std::string&)> callback = base::Bind(
      &WalletSigninHelper::ReturnWalletCookieValue,
      weak_ptr_factory_.GetWeakPtr());

  base::Closure task = base::Bind(&GetGoogleCookies, request_context, callback);
  content::BrowserThread::PostTask(content::BrowserThread::IO, FROM_HERE, task);
}

void WalletSigninHelper::OnServiceError(const GoogleServiceAuthError& error) {
  delegate_->OnPassiveSigninFailure(error);
}

void WalletSigninHelper::OnOtherError() {
  OnServiceError(GoogleServiceAuthError::AuthErrorNone());
}

void WalletSigninHelper::OnURLFetchComplete(
    const net::URLFetcher* fetcher) {
  DCHECK_EQ(url_fetcher_.get(), fetcher);
  scoped_ptr<net::URLFetcher> url_fetcher(url_fetcher_.release());

  if (!fetcher->GetStatus().is_success() ||
      fetcher->GetResponseCode() < 200 ||
      fetcher->GetResponseCode() >= 300) {
    DVLOG(1) << "URLFetchFailure:"
             << " r=" << fetcher->GetResponseCode()
             << " s=" << fetcher->GetStatus().status()
             << " e=" << fetcher->GetStatus().error();
    OnOtherError();
    return;
  }

  std::string data;
  if (!fetcher->GetResponseAsString(&data)) {
    DVLOG(1) << "failed to GetResponseAsString";
    OnOtherError();
    return;
  }

  if (!LowerCaseEqualsASCII(data, "yes")) {
    OnServiceError(
        GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP));
    return;
  }

  delegate_->OnPassiveSigninSuccess();
}

void WalletSigninHelper::ReturnWalletCookieValue(
    const std::string& cookie_value) {
  DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));

  delegate_->OnDidFetchWalletCookieValue(cookie_value);
}

}  // namespace wallet
}  // namespace autofill