OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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 #ifndef CHROME_BROWSER_NET_GAIA_OAUTH2_LOGIN_TOKEN_FETCHER_H_ |
| 6 #define CHROME_BROWSER_NET_GAIA_OAUTH2_LOGIN_TOKEN_FETCHER_H_ |
| 7 #pragma once |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "base/gtest_prod_util.h" |
| 12 #include "base/memory/scoped_ptr.h" |
| 13 #include "chrome/browser/net/gaia/oauth2_login_token_consumer.h" |
| 14 #include "content/public/common/url_fetcher.h" |
| 15 #include "content/public/common/url_fetcher_delegate.h" |
| 16 #include "googleurl/src/gurl.h" |
| 17 |
| 18 class OAuth2LoginTokenFetcherTest; |
| 19 |
| 20 namespace net { |
| 21 class URLRequestContextGetter; |
| 22 class URLRequestStatus; |
| 23 } |
| 24 |
| 25 // Abstracts the details to get OAuth2 login scoped token from |
| 26 // ClientLogin credentails. |
| 27 // |
| 28 // This class should be used on a single thread, but it can be whichever thread |
| 29 // that you like. |
| 30 // Also, do not reuse the same instance. Once Start() is called, the instance |
| 31 // should not be reused. |
| 32 // |
| 33 // Usage: |
| 34 // * Create an instance with a consumer. |
| 35 // * Call Start() |
| 36 // * The consumer passed in the constructor will be called on the same |
| 37 // thread Start was called with the results. |
| 38 // |
| 39 // This class can handle one request at a time. To parallelize requests, |
| 40 // create multiple instances. |
| 41 class OAuth2LoginTokenFetcher : public content::URLFetcherDelegate { |
| 42 public: |
| 43 OAuth2LoginTokenFetcher(OAuth2LoginTokenConsumer* consumer, |
| 44 net::URLRequestContextGetter* getter, |
| 45 const std::string& source); |
| 46 virtual ~OAuth2LoginTokenFetcher(); |
| 47 |
| 48 void Start(const std::string& auth_token); |
| 49 |
| 50 void CancelRequest(); |
| 51 |
| 52 // Implementation of content::URLFetcherDelegate |
| 53 virtual void OnURLFetchComplete(const content::URLFetcher* source); |
| 54 |
| 55 private: |
| 56 // There are two steps to getting a tokeen pair: |
| 57 // 1. Get authorization code. |
| 58 // 2. Exchange authorization code for a token pair. |
| 59 enum State { |
| 60 INITIAL, |
| 61 GET_AUTH_CODE_STARTED, |
| 62 GET_AUTH_CODE_DONE, |
| 63 GET_TOKEN_PAIR_STARTED, |
| 64 GET_TOKEN_PAIR_DONE, |
| 65 ERROR_STATE, |
| 66 }; |
| 67 |
| 68 // Helper methods for the flow. |
| 69 void StartGetAuthCode(); |
| 70 void EndGetAuthCode(const content::URLFetcher* source); |
| 71 void StartGetTokenPair(); |
| 72 void EndGetTokenPair(const content::URLFetcher* source); |
| 73 |
| 74 // Helper mehtods for reporting back results. |
| 75 void ReportSuccess(const std::string& refresh_token, |
| 76 const std::string& access_token); |
| 77 void ReportFailure(GoogleServiceAuthError error); |
| 78 |
| 79 // Other helpers. |
| 80 static GURL MakeGetAuthCodeUrl(); |
| 81 static std::string MakeGetAuthCodeHeader(const std::string& auth_token); |
| 82 static std::string MakeGetAuthCodeBody(); |
| 83 static bool ParseGetAuthCodeResponse(const content::URLFetcher* source, |
| 84 std::string* auth_code); |
| 85 |
| 86 static GURL MakeGetTokenPairUrl(); |
| 87 static std::string MakeGetTokenPairBody(const std::string& auth_code); |
| 88 static bool ParseGetTokenPairResponse(const content::URLFetcher* source, |
| 89 std::string* refresh_token, |
| 90 std::string* access_token); |
| 91 |
| 92 // State that is set during construction. |
| 93 OAuth2LoginTokenConsumer* const consumer_; |
| 94 net::URLRequestContextGetter* const getter_; |
| 95 std::string source_; |
| 96 State state_; |
| 97 |
| 98 // While a fetch is in progress. |
| 99 scoped_ptr<content::URLFetcher> fetcher_; |
| 100 std::string auth_token_; |
| 101 std::string auth_code_; |
| 102 |
| 103 friend class OAuth2LoginTokenFetcherTest; |
| 104 FRIEND_TEST_ALL_PREFIXES(OAuth2LoginTokenFetcherTest, |
| 105 ParseGetAuthCodeResponse); |
| 106 FRIEND_TEST_ALL_PREFIXES(OAuth2LoginTokenFetcherTest, |
| 107 ParseGetTokenPairResponse); |
| 108 |
| 109 DISALLOW_COPY_AND_ASSIGN(OAuth2LoginTokenFetcher); |
| 110 }; |
| 111 |
| 112 #endif // CHROME_BROWSER_NET_GAIA_OAUTH2_LOGIN_TOKEN_FETCHER_H_ |
OLD | NEW |