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 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
|
// 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 "chrome/browser/signin/android_profile_oauth2_token_service.h"
#include "base/android/jni_android.h"
#include "base/android/jni_array.h"
#include "base/android/jni_string.h"
#include "base/bind.h"
#include "base/logging.h"
#include "chrome/browser/profiles/profile_android.h"
#include "chrome/browser/signin/profile_oauth2_token_service_factory.h"
#include "chrome/browser/sync/profile_sync_service_android.h"
#include "content/public/browser/browser_thread.h"
#include "google_apis/gaia/gaia_auth_util.h"
#include "google_apis/gaia/oauth2_access_token_fetcher.h"
#include "jni/OAuth2TokenService_jni.h"
using base::android::AttachCurrentThread;
using base::android::ConvertJavaStringToUTF8;
using base::android::ConvertUTF8ToJavaString;
using base::android::ScopedJavaLocalRef;
using content::BrowserThread;
namespace {
// Callback from FetchOAuth2TokenWithUsername().
// Arguments:
// - the error, or NONE if the token fetch was successful.
// - the OAuth2 access token.
// - the expiry time of the token (may be null, indicating that the expiry
// time is unknown.
typedef base::Callback<void(
const GoogleServiceAuthError&, const std::string&, const base::Time&)>
FetchOAuth2TokenCallback;
class AndroidAccessTokenFetcher : public OAuth2AccessTokenFetcher {
public:
AndroidAccessTokenFetcher(OAuth2AccessTokenConsumer* consumer,
const std::string& account_id);
virtual ~AndroidAccessTokenFetcher();
// Overrides from OAuth2AccessTokenFetcher:
virtual void Start(const std::string& client_id,
const std::string& client_secret,
const std::vector<std::string>& scopes) override;
virtual void CancelRequest() override;
// Handles an access token response.
void OnAccessTokenResponse(const GoogleServiceAuthError& error,
const std::string& access_token,
const base::Time& expiration_time);
private:
std::string CombineScopes(const std::vector<std::string>& scopes);
base::WeakPtrFactory<AndroidAccessTokenFetcher> weak_factory_;
std::string account_id_;
bool request_was_cancelled_;
DISALLOW_COPY_AND_ASSIGN(AndroidAccessTokenFetcher);
};
AndroidAccessTokenFetcher::AndroidAccessTokenFetcher(
OAuth2AccessTokenConsumer* consumer,
const std::string& account_id)
: OAuth2AccessTokenFetcher(consumer),
weak_factory_(this),
account_id_(account_id),
request_was_cancelled_(false) {
}
AndroidAccessTokenFetcher::~AndroidAccessTokenFetcher() {}
void AndroidAccessTokenFetcher::Start(const std::string& client_id,
const std::string& client_secret,
const std::vector<std::string>& scopes) {
JNIEnv* env = AttachCurrentThread();
std::string scope = CombineScopes(scopes);
ScopedJavaLocalRef<jstring> j_username =
ConvertUTF8ToJavaString(env, account_id_);
ScopedJavaLocalRef<jstring> j_scope =
ConvertUTF8ToJavaString(env, scope);
scoped_ptr<FetchOAuth2TokenCallback> heap_callback(
new FetchOAuth2TokenCallback(
base::Bind(&AndroidAccessTokenFetcher::OnAccessTokenResponse,
weak_factory_.GetWeakPtr())));
// Call into Java to get a new token.
Java_OAuth2TokenService_getOAuth2AuthToken(
env, base::android::GetApplicationContext(),
j_username.obj(),
j_scope.obj(),
reinterpret_cast<intptr_t>(heap_callback.release()));
}
void AndroidAccessTokenFetcher::CancelRequest() {
request_was_cancelled_ = true;
}
void AndroidAccessTokenFetcher::OnAccessTokenResponse(
const GoogleServiceAuthError& error,
const std::string& access_token,
const base::Time& expiration_time) {
if (request_was_cancelled_) {
// Ignore the callback if the request was cancelled.
return;
}
if (error.state() == GoogleServiceAuthError::NONE) {
FireOnGetTokenSuccess(access_token, expiration_time);
} else {
FireOnGetTokenFailure(error);
}
}
// static
std::string AndroidAccessTokenFetcher::CombineScopes(
const std::vector<std::string>& scopes) {
// The Android AccountManager supports multiple scopes separated by a space:
// https://code.google.com/p/google-api-java-client/wiki/OAuth2#Android
std::string scope;
for (std::vector<std::string>::const_iterator it = scopes.begin();
it != scopes.end(); ++it) {
if (!scope.empty())
scope += " ";
scope += *it;
}
return scope;
}
} // namespace
bool AndroidProfileOAuth2TokenService::is_testing_profile_ = false;
AndroidProfileOAuth2TokenService::AndroidProfileOAuth2TokenService() {
DVLOG(1) << "AndroidProfileOAuth2TokenService::ctor";
JNIEnv* env = AttachCurrentThread();
base::android::ScopedJavaLocalRef<jobject> local_java_ref =
Java_OAuth2TokenService_create(env, reinterpret_cast<intptr_t>(this));
java_ref_.Reset(env, local_java_ref.obj());
}
AndroidProfileOAuth2TokenService::~AndroidProfileOAuth2TokenService() {}
// static
jobject AndroidProfileOAuth2TokenService::GetForProfile(
JNIEnv* env, jclass clazz, jobject j_profile_android) {
Profile* profile = ProfileAndroid::FromProfileAndroid(j_profile_android);
AndroidProfileOAuth2TokenService* service =
ProfileOAuth2TokenServiceFactory::GetPlatformSpecificForProfile(profile);
return service->java_ref_.obj();
}
static jobject GetForProfile(JNIEnv* env,
jclass clazz,
jobject j_profile_android) {
return AndroidProfileOAuth2TokenService::GetForProfile(
env, clazz, j_profile_android);
}
void AndroidProfileOAuth2TokenService::Initialize(
SigninClient* client,
SigninErrorController* signin_error_controller) {
DVLOG(1) << "AndroidProfileOAuth2TokenService::Initialize";
ProfileOAuth2TokenService::Initialize(client, signin_error_controller);
if (!is_testing_profile_) {
Java_OAuth2TokenService_validateAccounts(
AttachCurrentThread(), java_ref_.obj(),
base::android::GetApplicationContext(), JNI_TRUE);
}
}
bool AndroidProfileOAuth2TokenService::RefreshTokenIsAvailable(
const std::string& account_id) const {
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> j_account_id =
ConvertUTF8ToJavaString(env, account_id);
jboolean refresh_token_is_available =
Java_OAuth2TokenService_hasOAuth2RefreshToken(
env, base::android::GetApplicationContext(),
j_account_id.obj());
return refresh_token_is_available == JNI_TRUE;
}
void AndroidProfileOAuth2TokenService::UpdateAuthError(
const std::string& account_id,
const GoogleServiceAuthError& error) {
// TODO(rogerta): do we need to update anything, or does the system handle it?
}
std::vector<std::string> AndroidProfileOAuth2TokenService::GetAccounts() {
std::vector<std::string> accounts;
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobjectArray> j_accounts =
Java_OAuth2TokenService_getAccounts(
env, base::android::GetApplicationContext());
// TODO(fgorski): We may decide to filter out some of the accounts.
base::android::AppendJavaStringArrayToStringVector(env,
j_accounts.obj(),
&accounts);
return accounts;
}
std::vector<std::string> AndroidProfileOAuth2TokenService::GetSystemAccounts() {
std::vector<std::string> accounts;
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobjectArray> j_accounts =
Java_OAuth2TokenService_getSystemAccounts(
env, base::android::GetApplicationContext());
base::android::AppendJavaStringArrayToStringVector(env,
j_accounts.obj(),
&accounts);
return accounts;
}
OAuth2AccessTokenFetcher*
AndroidProfileOAuth2TokenService::CreateAccessTokenFetcher(
const std::string& account_id,
net::URLRequestContextGetter* getter,
OAuth2AccessTokenConsumer* consumer) {
ValidateAccountId(account_id);
return new AndroidAccessTokenFetcher(consumer, account_id);
}
void AndroidProfileOAuth2TokenService::InvalidateOAuth2Token(
const std::string& account_id,
const std::string& client_id,
const ScopeSet& scopes,
const std::string& access_token) {
ValidateAccountId(account_id);
OAuth2TokenService::InvalidateOAuth2Token(account_id,
client_id,
scopes,
access_token);
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> j_access_token =
ConvertUTF8ToJavaString(env, access_token);
Java_OAuth2TokenService_invalidateOAuth2AuthToken(
env, base::android::GetApplicationContext(),
j_access_token.obj());
}
void AndroidProfileOAuth2TokenService::ValidateAccounts(
JNIEnv* env,
jobject obj,
jstring j_current_acc,
jboolean j_force_notifications) {
DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts from java";
std::string signed_in_account;
if (j_current_acc)
signed_in_account = ConvertJavaStringToUTF8(env, j_current_acc);
if (!signed_in_account.empty())
signed_in_account = gaia::CanonicalizeEmail(signed_in_account);
ValidateAccounts(signed_in_account, j_force_notifications != JNI_FALSE);
}
void AndroidProfileOAuth2TokenService::ValidateAccounts(
const std::string& signed_in_account,
bool force_notifications) {
std::vector<std::string> prev_ids = GetAccounts();
std::vector<std::string> curr_ids = GetSystemAccounts();
std::vector<std::string> refreshed_ids;
std::vector<std::string> revoked_ids;
// Canonicalize system accounts. |prev_ids| is already done.
for (size_t i = 0; i < curr_ids.size(); ++i)
curr_ids[i] = gaia::CanonicalizeEmail(curr_ids[i]);
for (size_t i = 0; i < prev_ids.size(); ++i)
ValidateAccountId(prev_ids[i]);
DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:"
<< " sigined_in_account=" << signed_in_account
<< " prev_ids=" << prev_ids.size()
<< " curr_ids=" << curr_ids.size()
<< " force=" << (force_notifications ? "true" : "false");
if (!ValidateAccounts(signed_in_account, prev_ids, curr_ids, refreshed_ids,
revoked_ids, force_notifications)) {
curr_ids.clear();
}
ScopedBatchChange batch(this);
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobjectArray> java_accounts(
base::android::ToJavaArrayOfStrings(env, curr_ids));
Java_OAuth2TokenService_saveStoredAccounts(
env, base::android::GetApplicationContext(), java_accounts.obj());
for (std::vector<std::string>::iterator it = refreshed_ids.begin();
it != refreshed_ids.end(); it++) {
FireRefreshTokenAvailable(*it);
}
for (std::vector<std::string>::iterator it = revoked_ids.begin();
it != revoked_ids.end(); it++) {
FireRefreshTokenRevoked(*it);
}
}
bool AndroidProfileOAuth2TokenService::ValidateAccounts(
const std::string& signed_in_account,
const std::vector<std::string>& prev_account_ids,
const std::vector<std::string>& curr_account_ids,
std::vector<std::string>& refreshed_ids,
std::vector<std::string>& revoked_ids,
bool force_notifications) {
if (std::find(curr_account_ids.begin(),
curr_account_ids.end(),
signed_in_account) != curr_account_ids.end()) {
// Test to see if an account is removed from the Android AccountManager.
// If so, invoke FireRefreshTokenRevoked to notify the reconcilor.
for (std::vector<std::string>::const_iterator it = prev_account_ids.begin();
it != prev_account_ids.end(); it++) {
if (*it == signed_in_account)
continue;
if (std::find(curr_account_ids.begin(),
curr_account_ids.end(),
*it) == curr_account_ids.end()) {
DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:"
<< "revoked=" << *it;
revoked_ids.push_back(*it);
}
}
if (force_notifications ||
std::find(prev_account_ids.begin(), prev_account_ids.end(),
signed_in_account) == prev_account_ids.end()) {
// Always fire the primary signed in account first.
DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:"
<< "refreshed=" << signed_in_account;
refreshed_ids.push_back(signed_in_account);
}
for (std::vector<std::string>::const_iterator it = curr_account_ids.begin();
it != curr_account_ids.end(); it++) {
if (*it != signed_in_account) {
if (force_notifications ||
std::find(prev_account_ids.begin(),
prev_account_ids.end(),
*it) == prev_account_ids.end()) {
DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:"
<< "refreshed=" << *it;
refreshed_ids.push_back(*it);
}
}
}
return true;
} else {
// Currently signed in account does not any longer exist among accounts on
// system together with all other accounts.
if (std::find(prev_account_ids.begin(), prev_account_ids.end(),
signed_in_account) != prev_account_ids.end()) {
DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:"
<< "revoked=" << signed_in_account;
revoked_ids.push_back(signed_in_account);
}
for (std::vector<std::string>::const_iterator it = prev_account_ids.begin();
it != prev_account_ids.end(); it++) {
if (*it == signed_in_account)
continue;
DVLOG(1) << "AndroidProfileOAuth2TokenService::ValidateAccounts:"
<< "revoked=" << *it;
revoked_ids.push_back(*it);
}
return false;
}
}
void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailableFromJava(
JNIEnv* env,
jobject obj,
const jstring account_name) {
std::string account_id = ConvertJavaStringToUTF8(env, account_name);
AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable(account_id);
}
void AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable(
const std::string& account_id) {
DVLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokenAvailable id="
<< account_id;
// Notify native observers.
OAuth2TokenService::FireRefreshTokenAvailable(account_id);
// Notify Java observers.
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> account_name =
ConvertUTF8ToJavaString(env, account_id);
Java_OAuth2TokenService_notifyRefreshTokenAvailable(
env, java_ref_.obj(), account_name.obj());
}
void AndroidProfileOAuth2TokenService::FireRefreshTokenRevokedFromJava(
JNIEnv* env,
jobject obj,
const jstring account_name) {
std::string account_id = ConvertJavaStringToUTF8(env, account_name);
AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked(account_id);
}
void AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked(
const std::string& account_id) {
DVLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokenRevoked id="
<< account_id;
// Notify native observers.
OAuth2TokenService::FireRefreshTokenRevoked(account_id);
// Notify Java observers.
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jstring> account_name =
ConvertUTF8ToJavaString(env, account_id);
Java_OAuth2TokenService_notifyRefreshTokenRevoked(
env, java_ref_.obj(), account_name.obj());
}
void AndroidProfileOAuth2TokenService::FireRefreshTokensLoadedFromJava(
JNIEnv* env,
jobject obj) {
AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded();
}
void AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded() {
DVLOG(1) << "AndroidProfileOAuth2TokenService::FireRefreshTokensLoaded";
// Notify native observers.
OAuth2TokenService::FireRefreshTokensLoaded();
// Notify Java observers.
JNIEnv* env = AttachCurrentThread();
Java_OAuth2TokenService_notifyRefreshTokensLoaded(
env, java_ref_.obj());
}
void AndroidProfileOAuth2TokenService::RevokeAllCredentials() {
DVLOG(1) << "AndroidProfileOAuth2TokenService::RevokeAllCredentials";
ScopedBatchChange batch(this);
std::vector<std::string> accounts = GetAccounts();
for (std::vector<std::string>::iterator it = accounts.begin();
it != accounts.end(); it++) {
FireRefreshTokenRevoked(*it);
}
// Clear everything on the Java side as well.
std::vector<std::string> empty;
JNIEnv* env = AttachCurrentThread();
ScopedJavaLocalRef<jobjectArray> java_accounts(
base::android::ToJavaArrayOfStrings(env, empty));
Java_OAuth2TokenService_saveStoredAccounts(
env, base::android::GetApplicationContext(), java_accounts.obj());
}
// Called from Java when fetching of an OAuth2 token is finished. The
// |authToken| param is only valid when |result| is true.
void OAuth2TokenFetched(
JNIEnv* env,
jclass clazz,
jstring authToken,
jboolean result,
jlong nativeCallback) {
std::string token;
if (authToken)
token = ConvertJavaStringToUTF8(env, authToken);
scoped_ptr<FetchOAuth2TokenCallback> heap_callback(
reinterpret_cast<FetchOAuth2TokenCallback*>(nativeCallback));
// Android does not provide enough information to know if the credentials are
// wrong, so assume any error is transient by using CONNECTION_FAILED.
GoogleServiceAuthError err(result ?
GoogleServiceAuthError::NONE :
GoogleServiceAuthError::CONNECTION_FAILED);
heap_callback->Run(err, token, base::Time());
}
// static
bool AndroidProfileOAuth2TokenService::Register(JNIEnv* env) {
return RegisterNativesImpl(env);
}
|