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

Unified Diff: components/proximity_auth/cryptauth/cryptauth_account_token_fetcher.cc

Issue 792193003: Implement CryptAuthAccessTokenFetcher. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixes Created 6 years 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: components/proximity_auth/cryptauth/cryptauth_account_token_fetcher.cc
diff --git a/components/proximity_auth/cryptauth/cryptauth_account_token_fetcher.cc b/components/proximity_auth/cryptauth/cryptauth_account_token_fetcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..f02ee681fa35325cdb4223d0f02a02a661dd4f04
--- /dev/null
+++ b/components/proximity_auth/cryptauth/cryptauth_account_token_fetcher.cc
@@ -0,0 +1,60 @@
+// Copyright 2014 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 "components/proximity_auth/cryptauth/cryptauth_account_token_fetcher.h"
+
+namespace proximity_auth {
+
+namespace {
+
+// Returns the set of OAuth2 scopes that CryptAuth uses.
+OAuth2TokenService::ScopeSet GetScopes() {
+ OAuth2TokenService::ScopeSet scopes;
+ scopes.insert("https://www.googleapis.com/auth/cryptauth");
+ return scopes;
+}
+
+} // namespace
+
+CryptAuthAccountTokenFetcher::CryptAuthAccountTokenFetcher(
+ OAuth2TokenService* token_service,
+ const std::string& account_id)
+ : OAuth2TokenService::Consumer("cryptauth_account_token_fetcher"),
+ token_service_(token_service),
+ account_id_(account_id),
+ fetch_started_(false) {
+}
+
+CryptAuthAccountTokenFetcher::~CryptAuthAccountTokenFetcher() {
+}
+
+void CryptAuthAccountTokenFetcher::FetchAccessToken(
+ const AccessTokenCallback& callback) {
+ if (fetch_started_) {
+ LOG(WARNING) << "Create an instance for each token fetched. Do not reuse.";
+ callback.Run(std::string());
+ return;
+ }
+
+ fetch_started_ = true;
+ callback_ = callback;
+ // This request will return a cached result if it is available, saving a
+ // network round trip every time we fetch the access token.
+ token_request_ = token_service_->StartRequest(account_id_, GetScopes(), this);
+}
+
+void CryptAuthAccountTokenFetcher::OnGetTokenSuccess(
+ const OAuth2TokenService::Request* request,
+ const std::string& access_token,
+ const base::Time& expiration_time) {
+ callback_.Run(access_token);
+}
+
+void CryptAuthAccountTokenFetcher::OnGetTokenFailure(
+ const OAuth2TokenService::Request* request,
+ const GoogleServiceAuthError& error) {
+ callback_.Run(std::string());
+}
+
+} // namespace proximity_auth

Powered by Google App Engine
This is Rietveld 408576698