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

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

Issue 1066453002: Refactor CryptAuth component to be more testable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cryptauth_securemessage
Patch Set: rebase Created 5 years, 8 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: components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.cc
diff --git a/components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.cc b/components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.cc
new file mode 100644
index 0000000000000000000000000000000000000000..764cff386ce223d0a3f069bfb0aae1299f159f4f
--- /dev/null
+++ b/components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.cc
@@ -0,0 +1,111 @@
+// 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_access_token_fetcher.h"
+
+#include "base/callback.h"
+#include "google_apis/gaia/oauth2_token_service.h"
+
+namespace proximity_auth {
+
+namespace {
+
+// Implementation of CryptAuthAccessTokenFetcher fetching an access token for a
+// given account using the provided OAuth2TokenService.
+class CryptAuthAccessTokenFetcherImpl : public CryptAuthAccessTokenFetcher,
+ public OAuth2TokenService::Consumer {
+ public:
+ // |token_service| is not owned, and must outlive this object.
+ CryptAuthAccessTokenFetcherImpl(OAuth2TokenService* token_service,
+ const std::string& account_id);
+ ~CryptAuthAccessTokenFetcherImpl() override;
+
+ // CryptAuthAccessTokenFetcher:
+ void FetchAccessToken(const AccessTokenCallback& callback) override;
+
+ private:
+ // OAuth2TokenService::Consumer:
+ void OnGetTokenSuccess(const OAuth2TokenService::Request* request,
+ const std::string& access_token,
+ const base::Time& expiration_time) override;
+ void OnGetTokenFailure(const OAuth2TokenService::Request* request,
+ const GoogleServiceAuthError& error) override;
+
+ // System service that caches and fetches tokens for a given account.
+ // Not owned.
+ OAuth2TokenService* token_service_;
+
+ // The account id for whom to mint the token.
+ std::string account_id_;
+
+ // True if FetchAccessToken() has been called.
+ bool fetch_started_;
+
+ // Stores the request from |token_service_| to mint the token.
+ scoped_ptr<OAuth2TokenService::Request> token_request_;
+
+ // Callback to invoke when the token fetch succeeds or fails.
+ AccessTokenCallback callback_;
+
+ DISALLOW_COPY_AND_ASSIGN(CryptAuthAccessTokenFetcherImpl);
+};
Ilya Sherman 2015/04/06 18:53:33 I rather preferred having this as a separate file.
Tim Song 2015/04/06 23:21:17 Done.
+
+// 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;
+}
Ilya Sherman 2015/04/06 18:53:33 nit: Please move this either above or below the cl
Tim Song 2015/04/06 23:21:17 Moved definitions to another file.
+
+CryptAuthAccessTokenFetcherImpl::CryptAuthAccessTokenFetcherImpl(
+ 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) {
+}
+
+CryptAuthAccessTokenFetcherImpl::~CryptAuthAccessTokenFetcherImpl() {
+}
+
+void CryptAuthAccessTokenFetcherImpl::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 CryptAuthAccessTokenFetcherImpl::OnGetTokenSuccess(
+ const OAuth2TokenService::Request* request,
+ const std::string& access_token,
+ const base::Time& expiration_time) {
+ callback_.Run(access_token);
+}
+
+void CryptAuthAccessTokenFetcherImpl::OnGetTokenFailure(
+ const OAuth2TokenService::Request* request,
+ const GoogleServiceAuthError& error) {
+ callback_.Run(std::string());
+}
+
+} // namespace
+
+// static.
+scoped_ptr<CryptAuthAccessTokenFetcher>
+CryptAuthAccessTokenFetcher::CreateDefault(OAuth2TokenService* token_service,
+ const std::string& account_id) {
+ return make_scoped_ptr(
+ new CryptAuthAccessTokenFetcherImpl(token_service, account_id));
+}
+
+} // namespace proximity_auth

Powered by Google App Engine
This is Rietveld 408576698