1 // Copyright (c) 2012 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 GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FETCHER_H_
6 #define GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FETCHER_H_
11 #include "base/gtest_prod_util.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "google_apis/gaia/oauth2_mint_token_consumer.h"
14 #include "googleurl/src/gurl.h"
15 #include "net/url_request/url_fetcher_delegate.h"
17 class OAuth2MintTokenFetcherTest
;
21 class URLRequestContextGetter
;
22 class URLRequestStatus
;
25 // Abstracts the details to mint new OAuth2 tokens from OAuth2 login scoped
28 // This class should be used on a single thread, but it can be whichever thread
30 // Also, do not reuse the same instance. Once Start() is called, the instance
31 // should not be reused.
34 // * Create an instance with a consumer.
36 // * The consumer passed in the constructor will be called on the same
37 // thread Start was called with the results.
39 // This class can handle one request at a time. To parallelize requests,
40 // create multiple instances.
41 class OAuth2MintTokenFetcher
: public net::URLFetcherDelegate
{
43 OAuth2MintTokenFetcher(OAuth2MintTokenConsumer
* consumer
,
44 net::URLRequestContextGetter
* getter
,
45 const std::string
& source
);
46 virtual ~OAuth2MintTokenFetcher();
49 virtual void Start(const std::string
& oauth_login_access_token
,
50 const std::string
& client_id
,
51 const std::vector
<std::string
>& scopes
,
52 const std::string
& origin
);
56 // Implementation of net::URLFetcherDelegate
57 virtual void OnURLFetchComplete(const net::URLFetcher
* source
) OVERRIDE
;
67 // Helper methods for the flow.
68 void StartMintToken();
69 void EndMintToken(const net::URLFetcher
* source
);
71 // Helper methods for reporting back results.
72 void OnMintTokenSuccess(const std::string
& access_token
);
73 void OnMintTokenFailure(const GoogleServiceAuthError
& error
);
76 static GURL
MakeMintTokenUrl();
77 static std::string
MakeMintTokenHeader(const std::string
& access_token
);
78 static std::string
MakeMintTokenBody(const std::string
& client_id
,
79 const std::vector
<std::string
>& scopes
,
80 const std::string
& origin
);
81 static bool ParseMintTokenResponse(const net::URLFetcher
* source
,
82 std::string
* access_token
);
84 // State that is set during construction.
85 OAuth2MintTokenConsumer
* const consumer_
;
86 net::URLRequestContextGetter
* const getter_
;
90 // While a fetch is in progress.
91 scoped_ptr
<net::URLFetcher
> fetcher_
;
92 std::string oauth_login_access_token_
;
93 std::string client_id_
;
94 std::vector
<std::string
> scopes_
;
97 friend class OAuth2MintTokenFetcherTest
;
98 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFetcherTest
,
99 ParseMintTokenResponse
);
101 DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFetcher
);
104 #endif // GOOGLE_APIS_GAIA_OAUTH2_MINT_TOKEN_FETCHER_H_