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.
5 #include "components/proximity_auth/cryptauth/cryptauth_account_token_fetcher.h"
7 namespace proximity_auth
{
11 // Returns the set of OAuth2 scopes that CryptAuth uses.
12 OAuth2TokenService::ScopeSet
GetScopes() {
13 OAuth2TokenService::ScopeSet scopes
;
14 scopes
.insert("https://www.googleapis.com/auth/cryptauth");
20 CryptAuthAccountTokenFetcher::CryptAuthAccountTokenFetcher(
21 OAuth2TokenService
* token_service
,
22 const std::string
& account_id
)
23 : OAuth2TokenService::Consumer("cryptauth_account_token_fetcher"),
24 token_service_(token_service
),
25 account_id_(account_id
),
26 fetch_started_(false) {
29 CryptAuthAccountTokenFetcher::~CryptAuthAccountTokenFetcher() {
32 void CryptAuthAccountTokenFetcher::FetchAccessToken(
33 const AccessTokenCallback
& callback
) {
35 LOG(WARNING
) << "Create an instance for each token fetched. Do not reuse.";
36 callback
.Run(std::string());
40 fetch_started_
= true;
42 // This request will return a cached result if it is available, saving a
43 // network round trip every time we fetch the access token.
44 token_request_
= token_service_
->StartRequest(account_id_
, GetScopes(), this);
47 void CryptAuthAccountTokenFetcher::OnGetTokenSuccess(
48 const OAuth2TokenService::Request
* request
,
49 const std::string
& access_token
,
50 const base::Time
& expiration_time
) {
51 callback_
.Run(access_token
);
54 void CryptAuthAccountTokenFetcher::OnGetTokenFailure(
55 const OAuth2TokenService::Request
* request
,
56 const GoogleServiceAuthError
& error
) {
57 callback_
.Run(std::string());
60 } // namespace proximity_auth