1 // Copyright 2015 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 #ifndef REMOTING_TEST_ACCESS_TOKEN_FETCHER_H_
6 #define REMOTING_TEST_ACCESS_TOKEN_FETCHER_H_
8 #include "base/callback.h"
9 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "google_apis/gaia/gaia_oauth_client.h"
16 // Supplied by the client for each request to GAIA and returns valid tokens on
17 // success or empty tokens on failure.
18 typedef base::Callback
<void(
19 const std::string
& access_token
,
20 const std::string
& refresh_token
)> AccessTokenCallback
;
22 // Retrieves an access token from either an authorization code or a refresh
23 // token. Destroying the AccessTokenFetcher while a request is outstanding will
24 // cancel the request. It is safe to delete the fetcher from within a completion
25 // callback. Must be used from a thread running an IO message loop.
26 // The public methods are virtual to allow for mocking and fakes.
27 class AccessTokenFetcher
: public gaia::GaiaOAuthClient::Delegate
{
30 ~AccessTokenFetcher() override
;
32 // Retrieve an access token from a one time use authorization code.
33 virtual void GetAccessTokenFromAuthCode(const std::string
& auth_code
,
34 const AccessTokenCallback
& callback
);
36 // Retrieve an access token from a refresh token.
37 virtual void GetAccessTokenFromRefreshToken(
38 const std::string
& refresh_token
,
39 const AccessTokenCallback
& callback
);
42 // gaia::GaiaOAuthClient::Delegate Interface.
43 void OnGetTokensResponse(const std::string
& refresh_token
,
44 const std::string
& access_token
,
45 int expires_in_seconds
) override
;
46 void OnRefreshTokenResponse(const std::string
& access_token
,
47 int expires_in_seconds
) override
;
48 void OnGetUserEmailResponse(const std::string
& user_email
) override
;
49 void OnGetUserIdResponse(const std::string
& user_id
) override
;
50 void OnGetUserInfoResponse(
51 scoped_ptr
<base::DictionaryValue
> user_info
) override
;
52 void OnGetTokenInfoResponse(
53 scoped_ptr
<base::DictionaryValue
> token_info
) override
;
54 void OnOAuthError() override
;
55 void OnNetworkError(int response_code
) override
;
57 // Updates |auth_client_| with a new GaiaOAuthClient instance.
58 void CreateNewGaiaOAuthClientInstance();
60 // Validates the retrieved access token.
61 void ValidateAccessToken();
63 // Caller-supplied callback used to return valid tokens on success or empty
65 AccessTokenCallback access_token_callback_
;
67 // Retrieved based on the |refresh_token_|.
68 std::string access_token_
;
70 // Supplied by the caller or retrieved from a caller-supplied auth token.
71 std::string refresh_token_
;
73 // Holds the client id, secret, and redirect url used to make
74 // the Gaia service request.
75 gaia::OAuthClientInfo oauth_client_info_
;
77 // Used to make token requests to GAIA.
78 scoped_ptr
<gaia::GaiaOAuthClient
> auth_client_
;
80 DISALLOW_COPY_AND_ASSIGN(AccessTokenFetcher
);
84 } // namespace remoting
86 #endif // REMOTING_TEST_ACCESS_TOKEN_FETCHER_H_