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..ef25b7a0897a9db765c0217c9987b832264bc78c |
--- /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 { |
+ |
+OAuth2TokenService::ScopeSet GetScopes() { |
Ilya Sherman
2014/12/11 02:53:21
nit: Please add a brief docstring.
Tim Song
2014/12/12 20:48:44
Done.
|
+ OAuth2TokenService::ScopeSet scopes; |
+ scopes.insert("https://www.googleapis.com/auth/cryptauth"); |
+ return scopes; |
+} |
+ |
+} // namespace |
+ |
+// Inherits from OAuth2TokenService for the token caching. |
Ilya Sherman
2014/12/11 02:53:21
I'm not following what this comment means -- do yo
Tim Song
2014/12/12 20:48:44
Sorry, forgot to delete this comment.
|
+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; |
+ // The service will cache the access token, so we do not need to make a |
+ // network roundtrip for each token request. |
Ilya Sherman
2014/12/11 02:53:21
I don't understand this comment -- where do we tak
Tim Song
2014/12/12 20:48:44
Done.
|
+ 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 |