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
|
// Copyright (c) 2010 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.
#ifndef CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR2_H_
#define CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR2_H_
#include <string>
#include "base/gtest_prod_util.h"
#include "chrome/common/net/gaia/gaia_auth_consumer.h"
#include "chrome/common/net/url_fetcher.h"
#include "googleurl/src/gurl.h"
// Authenticate a user against the Google Accounts ClientLogin API
// with various capabilities and return results to a GaiaAuthConsumer.
//
// In the future, we will also issue auth tokens from this class.
// This class should be used on a single thread, but it can be whichever thread
// that you like.
//
// This class can handle one request at a time. To parallelize requests,
// create multiple GaiaAuthenticator2's.
class GaiaAuthenticator2Test;
class GaiaAuthenticator2 : public URLFetcher::Delegate {
public:
// Constants to use in the ClientLogin request POST body.
static const char kChromeOSSource[];
static const char kContactsService[];
// The URLs for different calls in the Google Accounts programmatic login API.
static const char kClientLoginUrl[];
static const char kIssueAuthTokenUrl[];
// Magic string indicating that, while a second factor is still
// needed to complete authentication, the user provided the right password.
static const char kSecondFactor[];
// This will later be hidden behind an auth service which caches
// tokens.
GaiaAuthenticator2(GaiaAuthConsumer* consumer,
const std::string& source,
URLRequestContextGetter* getter);
virtual ~GaiaAuthenticator2();
// GaiaAuthConsumer will be called on the original thread
// after results come back. This class is thread agnostic.
// You can't make more than request at a time.
void StartClientLogin(const std::string& username,
const std::string& password,
const char* const service,
const std::string& login_token,
const std::string& login_captcha);
// GaiaAuthConsumer will be called on the original thread
// after results come back. This class is thread agnostic.
// You can't make more than one request at a time.
void StartIssueAuthToken(const std::string& sid,
const std::string& lsid,
const char* const service);
// Implementation of URLFetcher::Delegate
void OnURLFetchComplete(const URLFetcher* source,
const GURL& url,
const URLRequestStatus& status,
int response_code,
const ResponseCookies& cookies,
const std::string& data);
// StartClientLogin been called && results not back yet?
bool HasPendingFetch();
// Stop any URL fetches in progress.
void CancelRequest();
private:
// ClientLogin body constants that don't change
static const char kCookiePersistence[];
static const char kAccountType[];
// The format of the POST body for ClientLogin.
static const char kClientLoginFormat[];
// The format of said POST body when CAPTCHA token & answer are specified.
static const char kClientLoginCaptchaFormat[];
// The format of the POST body for IssueAuthToken.
static const char kIssueAuthTokenFormat[];
// Process the results of a ClientLogin fetch.
void OnClientLoginFetched(const std::string& data,
const URLRequestStatus& status,
int response_code);
void OnIssueAuthTokenFetched(const std::string& data,
const URLRequestStatus& status,
int response_code);
// Tokenize the results of a ClientLogin fetch.
static void ParseClientLoginResponse(const std::string& data,
std::string* sid,
std::string* lsid,
std::string* token);
// From a URLFetcher result, generate an appropriate GaiaAuthError.
// From the API documentation, both IssueAuthToken and ClientLogin have
// the same error returns.
static GaiaAuthConsumer::GaiaAuthError GenerateAuthError(
const std::string& data,
const URLRequestStatus& status);
// Is this a special case Gaia error for TwoFactor auth?
static bool IsSecondFactorSuccess(const std::string& alleged_error);
// Given parameters, create a ClientLogin request body.
static std::string MakeClientLoginBody(const std::string& username,
const std::string& password,
const std::string& source,
const char* const service,
const std::string& login_token,
const std::string& login_captcha);
// Supply the sid / lsid returned from ClientLogin in order to
// request a long lived auth token for a service.
static std::string MakeIssueAuthTokenBody(const std::string& sid,
const std::string& lsid,
const char* const service);
// Create a fetcher useable for making any Gaia request.
static URLFetcher* CreateGaiaFetcher(URLRequestContextGetter* getter,
const std::string& body,
const GURL& gaia_gurl_,
URLFetcher::Delegate* delegate);
// These fields are common to GaiaAuthenticator2, same every request
GaiaAuthConsumer* const consumer_;
URLRequestContextGetter* const getter_;
std::string source_;
const GURL client_login_gurl_;
const GURL issue_auth_token_gurl_;
// While a fetch is going on:
scoped_ptr<URLFetcher> fetcher_;
std::string request_body_;
std::string requested_service_; // Currently tracked for IssueAuthToken only
bool fetch_pending_;
friend class GaiaAuthenticator2Test;
FRIEND_TEST_ALL_PREFIXES(GaiaAuthenticator2Test, LoginNetFailure);
FRIEND_TEST_ALL_PREFIXES(GaiaAuthenticator2Test, CheckNormalErrorCode);
FRIEND_TEST_ALL_PREFIXES(GaiaAuthenticator2Test, CheckTwoFactorResponse);
DISALLOW_COPY_AND_ASSIGN(GaiaAuthenticator2);
};
#endif // CHROME_COMMON_NET_GAIA_GAIA_AUTHENTICATOR2_H_
|