OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "components/proximity_auth/cryptauth/cryptauth_access_token_fetcher.h" | |
6 | |
7 #include "base/callback.h" | |
8 #include "google_apis/gaia/oauth2_token_service.h" | |
9 | |
10 namespace proximity_auth { | |
11 | |
12 namespace { | |
13 | |
14 // Implementation of CryptAuthAccessTokenFetcher fetching an access token for a | |
15 // given account using the provided OAuth2TokenService. | |
16 class CryptAuthAccessTokenFetcherImpl : public CryptAuthAccessTokenFetcher, | |
17 public OAuth2TokenService::Consumer { | |
18 public: | |
19 // |token_service| is not owned, and must outlive this object. | |
20 CryptAuthAccessTokenFetcherImpl(OAuth2TokenService* token_service, | |
21 const std::string& account_id); | |
22 ~CryptAuthAccessTokenFetcherImpl() override; | |
23 | |
24 // CryptAuthAccessTokenFetcher: | |
25 void FetchAccessToken(const AccessTokenCallback& callback) override; | |
26 | |
27 private: | |
28 // OAuth2TokenService::Consumer: | |
29 void OnGetTokenSuccess(const OAuth2TokenService::Request* request, | |
30 const std::string& access_token, | |
31 const base::Time& expiration_time) override; | |
32 void OnGetTokenFailure(const OAuth2TokenService::Request* request, | |
33 const GoogleServiceAuthError& error) override; | |
34 | |
35 // System service that caches and fetches tokens for a given account. | |
36 // Not owned. | |
37 OAuth2TokenService* token_service_; | |
38 | |
39 // The account id for whom to mint the token. | |
40 std::string account_id_; | |
41 | |
42 // True if FetchAccessToken() has been called. | |
43 bool fetch_started_; | |
44 | |
45 // Stores the request from |token_service_| to mint the token. | |
46 scoped_ptr<OAuth2TokenService::Request> token_request_; | |
47 | |
48 // Callback to invoke when the token fetch succeeds or fails. | |
49 AccessTokenCallback callback_; | |
50 | |
51 DISALLOW_COPY_AND_ASSIGN(CryptAuthAccessTokenFetcherImpl); | |
52 }; | |
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.
| |
53 | |
54 // Returns the set of OAuth2 scopes that CryptAuth uses. | |
55 OAuth2TokenService::ScopeSet GetScopes() { | |
56 OAuth2TokenService::ScopeSet scopes; | |
57 scopes.insert("https://www.googleapis.com/auth/cryptauth"); | |
58 return scopes; | |
59 } | |
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.
| |
60 | |
61 CryptAuthAccessTokenFetcherImpl::CryptAuthAccessTokenFetcherImpl( | |
62 OAuth2TokenService* token_service, | |
63 const std::string& account_id) | |
64 : OAuth2TokenService::Consumer("cryptauth_account_token_fetcher"), | |
65 token_service_(token_service), | |
66 account_id_(account_id), | |
67 fetch_started_(false) { | |
68 } | |
69 | |
70 CryptAuthAccessTokenFetcherImpl::~CryptAuthAccessTokenFetcherImpl() { | |
71 } | |
72 | |
73 void CryptAuthAccessTokenFetcherImpl::FetchAccessToken( | |
74 const AccessTokenCallback& callback) { | |
75 if (fetch_started_) { | |
76 LOG(WARNING) << "Create an instance for each token fetched. Do not reuse."; | |
77 callback.Run(std::string()); | |
78 return; | |
79 } | |
80 | |
81 fetch_started_ = true; | |
82 callback_ = callback; | |
83 // This request will return a cached result if it is available, saving a | |
84 // network round trip every time we fetch the access token. | |
85 token_request_ = token_service_->StartRequest(account_id_, GetScopes(), this); | |
86 } | |
87 | |
88 void CryptAuthAccessTokenFetcherImpl::OnGetTokenSuccess( | |
89 const OAuth2TokenService::Request* request, | |
90 const std::string& access_token, | |
91 const base::Time& expiration_time) { | |
92 callback_.Run(access_token); | |
93 } | |
94 | |
95 void CryptAuthAccessTokenFetcherImpl::OnGetTokenFailure( | |
96 const OAuth2TokenService::Request* request, | |
97 const GoogleServiceAuthError& error) { | |
98 callback_.Run(std::string()); | |
99 } | |
100 | |
101 } // namespace | |
102 | |
103 // static. | |
104 scoped_ptr<CryptAuthAccessTokenFetcher> | |
105 CryptAuthAccessTokenFetcher::CreateDefault(OAuth2TokenService* token_service, | |
106 const std::string& account_id) { | |
107 return make_scoped_ptr( | |
108 new CryptAuthAccessTokenFetcherImpl(token_service, account_id)); | |
109 } | |
110 | |
111 } // namespace proximity_auth | |
OLD | NEW |