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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
// Copyright (c) 2006-2009 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 "chrome/browser/sync/engine/auth_watcher.h"
#include "base/file_util.h"
#include "base/string_util.h"
#include "chrome/browser/sync/engine/all_status.h"
#include "chrome/browser/sync/engine/authenticator.h"
#include "chrome/browser/sync/engine/net/gaia_authenticator.h"
#include "chrome/browser/sync/engine/net/server_connection_manager.h"
#include "chrome/browser/sync/syncable/directory_manager.h"
#include "chrome/browser/sync/syncable/syncable.h"
#include "chrome/browser/sync/util/user_settings.h"
#include "chrome/common/deprecated/event_sys-inl.h"
#include "chrome/common/net/notifier/listener/talk_mediator.h"
// How authentication happens:
//
// Kick Off:
// The sync API looks to see if the user's name and
// password are stored. If so, it calls authwatcher.Authenticate() with
// them. Otherwise it fires an error event.
//
// On failed Gaia Auth:
// The AuthWatcher attempts to use saved hashes to authenticate
// locally, and on success opens the share.
// On failure, fires an error event.
//
// On successful Gaia Auth:
// AuthWatcher launches a thread to open the share and to get the
// authentication token from the sync server.
using std::pair;
using std::string;
using std::vector;
namespace browser_sync {
AuthWatcher::AuthWatcher(DirectoryManager* dirman,
ServerConnectionManager* scm,
AllStatus* allstatus,
const string& user_agent,
const string& service_id,
const string& gaia_url,
UserSettings* user_settings,
GaiaAuthenticator* gaia_auth,
notifier::TalkMediator* talk_mediator)
: gaia_(gaia_auth),
dirman_(dirman),
scm_(scm),
allstatus_(allstatus),
status_(NOT_AUTHENTICATED),
user_settings_(user_settings),
talk_mediator_(talk_mediator),
auth_backend_thread_("SyncEngine_AuthWatcherThread"),
current_attempt_trigger_(AuthWatcherEvent::USER_INITIATED) {
if (!auth_backend_thread_.Start())
NOTREACHED() << "Couldn't start SyncEngine_AuthWatcherThread";
gaia_->set_message_loop(message_loop());
connmgr_hookup_.reset(
NewEventListenerHookup(scm->channel(), this,
&AuthWatcher::HandleServerConnectionEvent));
AuthWatcherEvent done = { AuthWatcherEvent::AUTHWATCHER_DESTROYED };
channel_.reset(new Channel(done));
}
void AuthWatcher::PersistCredentials() {
DCHECK_EQ(MessageLoop::current(), message_loop());
GaiaAuthenticator::AuthResults results = gaia_->results();
// We just successfully signed in again, let's clear out any residual cached
// login data from earlier sessions.
ClearAuthenticationData();
user_settings_->StoreEmailForSignin(results.email, results.primary_email);
user_settings_->RememberSigninType(results.email, results.signin);
user_settings_->RememberSigninType(results.primary_email, results.signin);
results.email = results.primary_email;
gaia_->SetUsernamePassword(results.primary_email, results.password);
if (!user_settings_->VerifyAgainstStoredHash(results.email, results.password))
user_settings_->StoreHashedPassword(results.email, results.password);
if (PERSIST_TO_DISK == results.credentials_saved) {
user_settings_->SetAuthTokenForService(results.email,
SYNC_SERVICE_NAME,
gaia_->auth_token());
}
}
// TODO(chron): Full integration test suite needed. http://crbug.com/35429
void AuthWatcher::RenewAuthToken(const std::string& updated_token) {
message_loop()->PostTask(FROM_HERE, NewRunnableMethod(this,
&AuthWatcher::DoRenewAuthToken, updated_token));
}
void AuthWatcher::DoRenewAuthToken(const std::string& updated_token) {
DCHECK_EQ(MessageLoop::current(), message_loop());
// TODO(chron): We should probably only store auth token in one place.
if (scm_->auth_token() == updated_token) {
return; // This thread is the only one writing to the SCM's auth token.
}
LOG(INFO) << "Updating auth token:" << updated_token;
scm_->set_auth_token(updated_token);
gaia_->RenewAuthToken(updated_token); // Must be on AuthWatcher thread
talk_mediator_->SetAuthToken(user_settings_->email(), updated_token,
SYNC_SERVICE_NAME);
// TODO(akalin): to see if we need to call Login() here, too.
user_settings_->SetAuthTokenForService(user_settings_->email(),
SYNC_SERVICE_NAME,
updated_token);
AuthWatcherEvent event = { AuthWatcherEvent::AUTH_RENEWED };
NotifyListeners(&event);
}
void AuthWatcher::AuthenticateWithLsid(const std::string& lsid) {
message_loop()->PostTask(FROM_HERE, NewRunnableMethod(this,
&AuthWatcher::DoAuthenticateWithLsid, lsid));
}
void AuthWatcher::DoAuthenticateWithLsid(const std::string& lsid) {
DCHECK_EQ(MessageLoop::current(), message_loop());
AuthWatcherEvent event = { AuthWatcherEvent::AUTHENTICATION_ATTEMPT_START };
NotifyListeners(&event);
if (gaia_->AuthenticateWithLsid(lsid, true)) {
PersistCredentials();
DoAuthenticateWithToken(gaia_->email(), gaia_->auth_token());
} else {
ProcessGaiaAuthFailure();
}
}
const char kAuthWatcher[] = "AuthWatcher";
void AuthWatcher::AuthenticateWithToken(const std::string& gaia_email,
const std::string& auth_token) {
message_loop()->PostTask(FROM_HERE, NewRunnableMethod(this,
&AuthWatcher::DoAuthenticateWithToken, gaia_email, auth_token));
}
void AuthWatcher::DoAuthenticateWithToken(const std::string& gaia_email,
const std::string& auth_token) {
DCHECK_EQ(MessageLoop::current(), message_loop());
Authenticator auth(scm_, user_settings_);
Authenticator::AuthenticationResult result =
auth.AuthenticateToken(auth_token);
string email = gaia_email;
if (auth.display_email() && *auth.display_email()) {
email = auth.display_email();
LOG(INFO) << "Auth returned email " << email << " for gaia email " <<
gaia_email;
}
AuthWatcherEvent event = {AuthWatcherEvent::ILLEGAL_VALUE , 0};
gaia_->SetUsername(email);
gaia_->SetAuthToken(auth_token, SAVE_IN_MEMORY_ONLY);
const bool was_authenticated = NOT_AUTHENTICATED != status_;
switch (result) {
case Authenticator::SUCCESS:
{
status_ = GAIA_AUTHENTICATED;
const std::string& share_name = email;
user_settings_->SwitchUser(email);
// Set the authentication token for notifications
talk_mediator_->SetAuthToken(email, auth_token, SYNC_SERVICE_NAME);
talk_mediator_->Login();
scm_->set_auth_token(auth_token);
if (!was_authenticated) {
LOG(INFO) << "Opening DB for AuthenticateWithToken ("
<< share_name << ")";
dirman_->Open(share_name);
}
NotifyAuthSucceeded(email);
return;
}
case Authenticator::BAD_AUTH_TOKEN:
event.what_happened = AuthWatcherEvent::SERVICE_AUTH_FAILED;
break;
case Authenticator::CORRUPT_SERVER_RESPONSE:
case Authenticator::SERVICE_DOWN:
event.what_happened = AuthWatcherEvent::SERVICE_CONNECTION_FAILED;
break;
case Authenticator::USER_NOT_ACTIVATED:
event.what_happened = AuthWatcherEvent::SERVICE_USER_NOT_SIGNED_UP;
break;
default:
LOG(FATAL) << "Illegal return from AuthenticateToken";
return;
}
// Always fall back to local authentication.
if (was_authenticated || AuthenticateLocally(email)) {
if (AuthWatcherEvent::SERVICE_CONNECTION_FAILED == event.what_happened)
return;
}
DCHECK_NE(event.what_happened, AuthWatcherEvent::ILLEGAL_VALUE);
NotifyListeners(&event);
}
bool AuthWatcher::AuthenticateLocally(string email) {
DCHECK_EQ(MessageLoop::current(), message_loop());
user_settings_->GetEmailForSignin(&email);
if (file_util::PathExists(FilePath(dirman_->GetSyncDataDatabasePath()))) {
gaia_->SetUsername(email);
status_ = LOCALLY_AUTHENTICATED;
user_settings_->SwitchUser(email);
LOG(INFO) << "Opening DB for AuthenticateLocally (" << email << ")";
dirman_->Open(email);
NotifyAuthSucceeded(email);
return true;
} else {
return false;
}
}
bool AuthWatcher::AuthenticateLocally(string email, const string& password) {
DCHECK_EQ(MessageLoop::current(), message_loop());
user_settings_->GetEmailForSignin(&email);
return user_settings_->VerifyAgainstStoredHash(email, password)
&& AuthenticateLocally(email);
}
void AuthWatcher::ProcessGaiaAuthFailure() {
DCHECK_EQ(MessageLoop::current(), message_loop());
GaiaAuthenticator::AuthResults results = gaia_->results();
if (LOCALLY_AUTHENTICATED == status_) {
return; // nothing todo
} else if (AuthenticateLocally(results.email, results.password)) {
// We save the "Remember me" checkbox by putting a non-null auth
// token into the last_user table. So if we're offline and the
// user checks the box, insert a bogus auth token.
if (PERSIST_TO_DISK == results.credentials_saved) {
const string auth_token("bogus");
user_settings_->SetAuthTokenForService(results.email,
SYNC_SERVICE_NAME,
auth_token);
}
const bool unavailable = ConnectionUnavailable == results.auth_error ||
Unknown == results.auth_error ||
ServiceUnavailable == results.auth_error;
if (unavailable)
return;
}
AuthWatcherEvent myevent = { AuthWatcherEvent::GAIA_AUTH_FAILED, &results };
NotifyListeners(&myevent);
}
void AuthWatcher::DoAuthenticate(const AuthRequest& request) {
DCHECK_EQ(MessageLoop::current(), message_loop());
AuthWatcherEvent event = { AuthWatcherEvent::AUTHENTICATION_ATTEMPT_START };
NotifyListeners(&event);
current_attempt_trigger_ = request.trigger;
SaveCredentials save = request.persist_creds_to_disk ?
PERSIST_TO_DISK : SAVE_IN_MEMORY_ONLY;
SignIn const signin = user_settings_->
RecallSigninType(request.email, GMAIL_SIGNIN);
// We let the caller be lazy and try using the last captcha token seen by
// the gaia authenticator if they haven't provided a token but have sent
// a challenge response. Of course, if the captcha token is specified,
// we use that one instead.
std::string captcha_token(request.captcha_token);
if (!request.captcha_value.empty() && captcha_token.empty())
captcha_token = gaia_->captcha_token();
if (!request.password.empty()) {
bool authenticated = false;
if (!captcha_token.empty()) {
authenticated = gaia_->Authenticate(request.email, request.password,
save, captcha_token,
request.captcha_value, signin);
} else {
authenticated = gaia_->Authenticate(request.email, request.password,
save, signin);
}
if (authenticated) {
PersistCredentials();
DoAuthenticateWithToken(gaia_->email(), gaia_->auth_token());
} else {
ProcessGaiaAuthFailure();
}
} else if (!request.auth_token.empty()) {
DoAuthenticateWithToken(request.email, request.auth_token);
} else {
LOG(ERROR) << "Attempt to authenticate with no credentials.";
}
}
void AuthWatcher::NotifyAuthSucceeded(const string& email) {
DCHECK_EQ(MessageLoop::current(), message_loop());
LOG(INFO) << "NotifyAuthSucceeded";
AuthWatcherEvent event = { AuthWatcherEvent::AUTH_SUCCEEDED };
event.user_email = email;
NotifyListeners(&event);
}
void AuthWatcher::HandleServerConnectionEvent(
const ServerConnectionEvent& event) {
message_loop()->PostTask(FROM_HERE, NewRunnableMethod(this,
&AuthWatcher::DoHandleServerConnectionEvent, event,
scm_->auth_token()));
}
void AuthWatcher::DoHandleServerConnectionEvent(
const ServerConnectionEvent& event,
const std::string& auth_token_snapshot) {
DCHECK_EQ(MessageLoop::current(), message_loop());
if (event.server_reachable &&
// If the auth_token at the time of the event differs from the current
// one, we have authenticated since then and don't need to re-try.
(auth_token_snapshot == gaia_->auth_token()) &&
(event.connection_code == HttpResponse::SYNC_AUTH_ERROR ||
status_ == LOCALLY_AUTHENTICATED)) {
// We're either online or just got reconnected and want to try to
// authenticate. If we've got a saved token this should just work. If not
// the auth failure should trigger UI indications that we're not logged in.
// METRIC: If we get a SYNC_AUTH_ERROR, our token expired.
GaiaAuthenticator::AuthResults authresults = gaia_->results();
AuthRequest request = { authresults.email, authresults.password,
authresults.auth_token, std::string(),
std::string(),
PERSIST_TO_DISK == authresults.credentials_saved,
AuthWatcherEvent::EXPIRED_CREDENTIALS };
DoAuthenticate(request);
}
}
AuthWatcher::~AuthWatcher() {
auth_backend_thread_.Stop();
// The gaia authenticator takes a const MessageLoop* because it only uses it
// to ensure all methods are invoked on the given loop. Once our thread has
// stopped, the current message loop will be NULL, and no methods should be
// invoked on |gaia_| after this point. We could set it to NULL, but
// abstaining allows for even more sanity checking that nothing is invoked on
// it from now on.
}
void AuthWatcher::Authenticate(const string& email, const string& password,
const string& captcha_token, const string& captcha_value,
bool persist_creds_to_disk) {
LOG(INFO) << "AuthWatcher::Authenticate called";
string empty;
AuthRequest request = { FormatAsEmailAddress(email), password, empty,
captcha_token, captcha_value, persist_creds_to_disk,
AuthWatcherEvent::USER_INITIATED };
message_loop()->PostTask(FROM_HERE, NewRunnableMethod(this,
&AuthWatcher::DoAuthenticate, request));
}
void AuthWatcher::ClearAuthenticationData() {
scm_->set_auth_token(std::string());
user_settings_->ClearAllServiceTokens();
}
string AuthWatcher::email() const {
return gaia_->email();
}
void AuthWatcher::NotifyListeners(AuthWatcherEvent* event) {
event->trigger = current_attempt_trigger_;
channel_->NotifyListeners(*event);
}
} // namespace browser_sync
|