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

Unified Diff: chrome/browser/chromeos/arc/arc_auth_fetcher.cc

Issue 1618193003: arc: Pass auth token from Chrome to ARC instance. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Refactored. Added LSO UI Created 4 years, 11 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/arc/arc_auth_fetcher.cc
diff --git a/chrome/browser/chromeos/arc/arc_auth_fetcher.cc b/chrome/browser/chromeos/arc/arc_auth_fetcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..15e123c6c63ad345fc303be1f7f8df23b2ea04be
--- /dev/null
+++ b/chrome/browser/chromeos/arc/arc_auth_fetcher.cc
@@ -0,0 +1,113 @@
+// Copyright 2016 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/chromeos/arc/arc_auth_fetcher.h"
+
+#include "base/strings/string_split.h"
+#include "base/strings/string_util.h"
+#include "base/strings/stringprintf.h"
+#include "chrome/browser/profiles/profile.h"
+#include "google_apis/gaia/gaia_constants.h"
+#include "google_apis/gaia/gaia_urls.h"
+#include "net/http/http_status_code.h"
+#include "net/url_request/url_fetcher.h"
+
+namespace arc {
+
+namespace {
+
+const char kGMSCoreClientId[] =
+ "1070009224336-sdh77n7uot3oc99ais00jmuft6sk2fg9.apps.googleusercontent.com";
+const char kCookiePartSecure[] = "secure";
+const char kCookiePartHttpOnly[] = "httponly";
+const char kCookiePartCodePrefix[] = "oauth_code=";
+const int kCookiePartCodePrefixLength = arraysize(kCookiePartCodePrefix) - 1;
+
+static bool CookiePartsContains(const std::vector<std::string>& parts,
+ const char* part) {
+ for (std::vector<std::string>::const_iterator it = parts.begin();
+ it != parts.end(); ++it) {
+ if (base::LowerCaseEqualsASCII(*it, part))
+ return true;
+ }
+ return false;
+}
+
+} // namespace
+
+ArcAuthFetcher::ArcAuthFetcher(Profile* profile, Delegate* delegate)
+ : profile_(profile), delegate_(delegate) {
+ FetchAuthCode();
+}
+
+ArcAuthFetcher::~ArcAuthFetcher() {}
+
+// static
+GURL ArcAuthFetcher::CreateURL(Profile* profile) {
+ DCHECK(profile != nullptr);
+
+ std::string query_string = base::StringPrintf(
+ "?scope=%s&client_id=%s&email=%s", GaiaConstants::kOAuth1LoginScope,
+ kGMSCoreClientId, profile->GetProfileUserName().c_str());
+
+ return GaiaUrls::GetInstance()->client_login_to_oauth2_url().Resolve(
+ query_string);
+}
+
+void ArcAuthFetcher::FetchAuthCode() {
+ auth_fetcher_ =
+ net::URLFetcher::Create(CreateURL(profile_), net::URLFetcher::GET, this);
+ auth_fetcher_->SetRequestContext(profile_->GetRequestContext());
xiyuan 2016/01/26 23:37:05 Looks like the class does not need the whole |prof
khmel 2016/01/27 22:36:20 Good point! Thanks
+ // Executed asynchronously.
+ auth_fetcher_->Start();
+}
+
+// static
+bool ArcAuthFetcher::ParseAuthCode(const net::URLFetcher* source,
+ std::string* code) {
+ DCHECK(source != nullptr && code != nullptr);
+ net::ResponseCookies::const_iterator iter;
+ const net::ResponseCookies& cookies = source->GetCookies();
+ for (iter = cookies.begin(); iter != cookies.end(); ++iter) {
+ std::vector<std::string> parts = base::SplitString(
+ *iter, ";", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
+ // Per documentation, the cookie should have Secure and HttpOnly.
+ if (!CookiePartsContains(parts, kCookiePartSecure) ||
+ !CookiePartsContains(parts, kCookiePartHttpOnly)) {
+ continue;
+ }
+
+ std::vector<std::string>::const_iterator iter;
+ for (iter = parts.begin(); iter != parts.end(); ++iter) {
+ const std::string& part = *iter;
+ if (base::StartsWith(part, kCookiePartCodePrefix,
+ base::CompareCase::INSENSITIVE_ASCII)) {
+ *code = part.substr(kCookiePartCodePrefixLength);
+ return true;
+ }
+ }
+ }
+
+ return false;
+}
+
+void ArcAuthFetcher::OnURLFetchComplete(const net::URLFetcher* source) {
+ if (!source->GetStatus().is_success() ||
+ source->GetResponseCode() != net::HTTP_OK) {
+ VLOG(2) << "Failed to receive auth response. Response code: "
+ << source->GetResponseCode() << ".";
+ delegate_->OnAuthCodeFailed();
+ return;
+ }
+
+ std::string auth_code;
+ if (!ParseAuthCode(source, &auth_code)) {
+ delegate_->OnAuthCodeNeedUI();
+ return;
+ }
+
+ delegate_->OnAuthCodeFetched(auth_code);
+}
+
+} // namespace arc

Powered by Google App Engine
This is Rietveld 408576698