Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3948)

Unified Diff: chrome/browser/chromeos/login/signin/token_handler_util.cc

Issue 1025663002: Implement oauth token external handler checking. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Add comments Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/chromeos/login/signin/token_handler_util.cc
diff --git a/chrome/browser/chromeos/login/signin/token_handler_util.cc b/chrome/browser/chromeos/login/signin/token_handler_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..efeabd2987d4da6e6424926708e6584c3518d583
--- /dev/null
+++ b/chrome/browser/chromeos/login/signin/token_handler_util.cc
@@ -0,0 +1,115 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
Nikita (slow) 2015/03/20 18:06:49 nit: 2015
Denis Kuznetsov (DE-MUC) 2015/03/20 19:50:29 Done.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/chromeos/login/signin/token_handler_util.h"
+
+#include "base/memory/weak_ptr.h"
+#include "base/values.h"
+#include "chrome/browser/chromeos/profiles/profile_helper.h"
+#include "chrome/browser/profiles/profile.h"
+#include "components/user_manager/user_id.h"
+#include "components/user_manager/user_manager.h"
+#include "google_apis/gaia/gaia_oauth_client.h"
+
+namespace {
+
+const char kTokenHandlePref[] = "PasswordTokenHandle";
+static const int kMaxRetries = 3;
+}
+
+TokenHandlerUtil::TokenHandlerUtil(user_manager::UserManager* user_manager)
+ : user_manager_(user_manager), weak_factory_(this) {
+}
+
+TokenHandlerUtil::~TokenHandlerUtil() {
+ weak_factory_.InvalidateWeakPtrs();
+ gaia_client_.reset();
+}
+
+bool TokenHandlerUtil::HasToken(const user_manager::UserID& user_id) {
+ const base::DictionaryValue* dict = nullptr;
+ std::string token;
+ if (!user_manager_->FindKnowUserPrefs(user_id, &dict))
+ return false;
+ if (!dict->GetString(kTokenHandlePref, &token))
+ return false;
+ return !token.empty();
+}
+
+void TokenHandlerUtil::DeleteToken(const user_manager::UserID& user_id) {
+ const base::DictionaryValue* dict = nullptr;
+ if (!user_manager_->FindKnowUserPrefs(user_id, &dict))
+ return;
+ scoped_ptr<base::DictionaryValue> dict_copy(dict->DeepCopy());
+ if (!dict_copy->Remove(kTokenHandlePref, nullptr))
+ return;
+ user_manager_->UpdateKnowUserPrefs(user_id, *dict_copy.get(), true);
Nikita (slow) 2015/03/20 18:06:49 nit: comment for true
Denis Kuznetsov (DE-MUC) 2015/03/20 19:50:29 Done.
+}
+
+void TokenHandlerUtil::CheckToken(const user_manager::UserID& user_id,
+ const TokenValidationCallback& callback) {
+ const base::DictionaryValue* dict = nullptr;
+ std::string token;
+ if (!user_manager_->FindKnowUserPrefs(user_id, &dict)) {
+ callback.Run(user_id, UNKNOWN);
+ return;
+ }
+ if (!dict->GetString(kTokenHandlePref, &token)) {
+ callback.Run(user_id, UNKNOWN);
+ return;
+ }
+
+ if (!gaia_client_.get()) {
+ auto request_context =
+ chromeos::ProfileHelper::Get()->GetSigninProfile()->GetRequestContext();
+ gaia_client_.reset(new gaia::GaiaOAuthClient(request_context));
+ }
+
+ validation_delegates_.set(
+ token, scoped_ptr<TokenValidationDelegate>(new TokenValidationDelegate(
+ weak_factory_.GetWeakPtr(), user_id, token, callback)));
+ gaia_client_->GetTokenHandleInfo(token, kMaxRetries,
+ validation_delegates_.get(token));
+}
+
+void TokenHandlerUtil::OnValidationComplete(const std::string& token) {
+ validation_delegates_.erase(token);
+}
+
+TokenHandlerUtil::TokenValidationDelegate::TokenValidationDelegate(
+ const base::WeakPtr<TokenHandlerUtil>& owner,
+ const user_manager::UserID& user_id,
+ const std::string& token,
+ const TokenValidationCallback& callback)
+ : owner_(owner), user_id_(user_id), token_(token), callback_(callback) {
+}
+
+TokenHandlerUtil::TokenValidationDelegate::~TokenValidationDelegate() {
+}
+
+void TokenHandlerUtil::TokenValidationDelegate::OnOAuthError() {
+ callback_.Run(user_id_, INVALID);
+ if (owner_)
+ owner_->OnValidationComplete(token_);
+}
+
+void TokenHandlerUtil::TokenValidationDelegate::OnNetworkError(
+ int response_code) {
+ callback_.Run(user_id_, UNKNOWN);
+ if (owner_)
+ owner_->OnValidationComplete(token_);
+}
+
+void TokenHandlerUtil::TokenValidationDelegate::OnGetTokenInfoResponse(
+ scoped_ptr<base::DictionaryValue> token_info) {
+ TokenHandleStatus outcome = UNKNOWN;
+ if (!token_info->HasKey("error")) {
+ int expires_in = 0;
+ if (token_info->GetInteger("expires_in", &expires_in))
+ outcome = (expires_in < 0) ? INVALID : VALID;
+ }
+ callback_.Run(user_id_, outcome);
+ if (owner_)
+ owner_->OnValidationComplete(token_);
+}

Powered by Google App Engine
This is Rietveld 408576698