OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ | 5 #ifndef CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ |
6 #define CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ | 6 #define CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
| 9 #include <vector> |
9 | 10 |
10 #include "base/memory/scoped_ptr.h" | 11 #include "chrome/common/net/gaia/oauth2_api_call_flow.h" |
11 #include "base/memory/weak_ptr.h" | |
12 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" | |
13 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" | |
14 #include "chrome/common/net/gaia/oauth2_mint_token_consumer.h" | |
15 #include "chrome/common/net/gaia/oauth2_mint_token_fetcher.h" | |
16 | 12 |
17 class GoogleServiceAuthError; | 13 class GoogleServiceAuthError; |
18 class OAuth2MintTokenFlowTest; | 14 class OAuth2MintTokenFlowTest; |
19 | 15 |
| 16 namespace base { |
| 17 class DictionaryValue; |
| 18 } |
| 19 |
| 20 namespace content { |
| 21 class URLFetcher; |
| 22 } |
| 23 |
20 namespace net { | 24 namespace net { |
21 class URLRequestContextGetter; | 25 class URLRequestContextGetter; |
22 } | 26 } |
23 | 27 |
| 28 // IssueAdvice: messages to show to the user to get a user's approval. |
| 29 // The structure is as follows: |
| 30 // * Descritpion 1 |
| 31 // - Detail 1.1 |
| 32 // - Details 1.2 |
| 33 // * Description 2 |
| 34 // - Detail 2.1 |
| 35 // - Detail 2.2 |
| 36 // - Detail 2.3 |
| 37 // * Description 3 |
| 38 // - Detail 3.1 |
| 39 struct IssueAdviceInfoEntry { |
| 40 public: |
| 41 IssueAdviceInfoEntry(); |
| 42 ~IssueAdviceInfoEntry(); |
| 43 |
| 44 std::string description; |
| 45 std::vector<std::string> details; |
| 46 |
| 47 bool operator==(const IssueAdviceInfoEntry& rhs) const; |
| 48 }; |
| 49 |
| 50 typedef std::vector<IssueAdviceInfoEntry> IssueAdviceInfo; |
| 51 |
24 // This class implements the OAuth2 flow to Google to mint an OAuth2 | 52 // This class implements the OAuth2 flow to Google to mint an OAuth2 |
25 // token for the given client and the given set of scopes from the | 53 // token for the given client and the given set of scopes from the |
26 // OAuthLogin scoped "master" OAuth2 token for the user logged in to | 54 // OAuthLogin scoped "master" OAuth2 token for the user logged in to |
27 // Chrome. | 55 // Chrome. |
28 class OAuth2MintTokenFlow | 56 class OAuth2MintTokenFlow : public OAuth2ApiCallFlow { |
29 : public OAuth2AccessTokenConsumer, | |
30 public OAuth2MintTokenConsumer { | |
31 public: | 57 public: |
| 58 // There are four differnt modes when minting a token to grant |
| 59 // access to third-party app for a user. |
| 60 enum Mode { |
| 61 // Get the messages to display to the user without minting a token. |
| 62 MODE_ISSUE_ADVICE, |
| 63 // Record a grant but do not get a token back. |
| 64 MODE_RECORD_GRANT, |
| 65 // Mint a token for an existing grant. |
| 66 MODE_MINT_TOKEN_NO_FORCE, |
| 67 // Mint a token forcefully even if there is no existing grant. |
| 68 MODE_MINT_TOKEN_FORCE, |
| 69 }; |
| 70 |
| 71 // Parameters needed to mint a token. |
| 72 struct Parameters { |
| 73 public: |
| 74 Parameters(); |
| 75 Parameters(const std::string& rt, |
| 76 const std::string& eid, |
| 77 const std::string& cid, |
| 78 const std::vector<std::string>& scopes_arg, |
| 79 Mode mode_arg); |
| 80 ~Parameters(); |
| 81 |
| 82 std::string login_refresh_token; |
| 83 std::string extension_id; |
| 84 std::string client_id; |
| 85 std::vector<std::string> scopes; |
| 86 Mode mode; |
| 87 }; |
| 88 |
32 class Delegate { | 89 class Delegate { |
33 public: | 90 public: |
34 virtual void OnMintTokenSuccess(const std::string& access_token) { } | 91 Delegate() {} |
35 virtual void OnMintTokenFailure(const GoogleServiceAuthError& error) { } | 92 virtual ~Delegate() {} |
| 93 virtual void OnMintTokenSuccess(const std::string& access_token) {} |
| 94 virtual void OnIssueAdviceSuccess(const IssueAdviceInfo& issue_advice) {} |
| 95 virtual void OnMintTokenFailure(const GoogleServiceAuthError& error) {} |
36 }; | 96 }; |
37 | 97 |
38 // An interceptor for tests. | 98 // An interceptor for tests. |
39 class InterceptorForTests { | 99 class InterceptorForTests { |
40 public: | 100 public: |
41 // Returns true if the success callback should be called and false for | 101 // Returns true if the success callback should be called and false for |
42 // failures. | 102 // failures. |
43 virtual bool DoIntercept(const OAuth2MintTokenFlow* flow, | 103 virtual bool DoIntercept(const OAuth2MintTokenFlow* flow, |
44 std::string* access_token, | 104 std::string* access_token, |
45 GoogleServiceAuthError* error) = 0; | 105 GoogleServiceAuthError* error) = 0; |
46 }; | 106 }; |
47 static void SetInterceptorForTests(InterceptorForTests* interceptor); | 107 static void SetInterceptorForTests(InterceptorForTests* interceptor); |
48 | 108 |
49 OAuth2MintTokenFlow(net::URLRequestContextGetter* context, | 109 OAuth2MintTokenFlow(net::URLRequestContextGetter* context, |
50 Delegate* delegate); | 110 Delegate* delegate, |
| 111 const Parameters& parameters); |
51 virtual ~OAuth2MintTokenFlow(); | 112 virtual ~OAuth2MintTokenFlow(); |
52 | 113 |
53 // Start the process to mint a token. | 114 virtual void Start() OVERRIDE; |
54 void Start(const std::string& login_refresh_token, | |
55 const std::string& extension_id, | |
56 const std::string& client_id, | |
57 const std::vector<std::string>& scopes); | |
58 | |
59 // OAuth2AccessTokenConsumer implementation. | |
60 virtual void OnGetTokenSuccess(const std::string& access_token) OVERRIDE; | |
61 virtual void OnGetTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | |
62 // OAuth2MintTokenConsumer implementation. | |
63 virtual void OnMintTokenSuccess(const std::string& access_token) OVERRIDE; | |
64 virtual void OnMintTokenFailure(const GoogleServiceAuthError& error) OVERRIDE; | |
65 | |
66 // Getters for various members. | |
67 const std::string& extension_id() const { return extension_id_; } | |
68 const std::string& client_id() const { return client_id_; } | |
69 | 115 |
70 protected: | 116 protected: |
71 // Helper to create an instance of access token fetcher. | 117 // Implementation of template methods in OAuth2ApiCallFlow. |
72 // Caller owns the returned instance. | 118 virtual GURL CreateApiCallUrl() OVERRIDE; |
73 virtual OAuth2AccessTokenFetcher* CreateAccessTokenFetcher(); | 119 virtual std::string CreateApiCallBody() OVERRIDE; |
74 | 120 |
75 // Helper to create an instance of mint token fetcher. | 121 virtual void ProcessApiCallSuccess( |
76 // Caller owns the returned instance. | 122 const content::URLFetcher* source) OVERRIDE; |
77 virtual OAuth2MintTokenFetcher* CreateMintTokenFetcher(); | 123 virtual void ProcessApiCallFailure( |
| 124 const content::URLFetcher* source) OVERRIDE; |
| 125 virtual void ProcessNewAccessToken(const std::string& access_token) OVERRIDE; |
| 126 virtual void ProcessMintAccessTokenFailure( |
| 127 const GoogleServiceAuthError& error) OVERRIDE; |
78 | 128 |
79 private: | 129 private: |
80 // The steps this class performs are: | 130 friend class OAuth2MintTokenFlowTest; |
81 // 1. Create a login scoped access token from login scoped refresh token. | 131 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, CreateApiCallBody); |
82 // 2. Use login scoped access token to call the API to mint an access token | 132 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ParseIssueAdviceResponse); |
83 // for the app. | 133 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ParseMintTokenResponse); |
84 enum State { | 134 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ProcessApiCallSuccess); |
85 INITIAL, | 135 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ProcessApiCallFailure); |
86 FETCH_LOGIN_ACCESS_TOKEN_STARTED, | 136 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, |
87 FETCH_LOGIN_ACCESS_TOKEN_DONE, | 137 ProcessMintAccessTokenFailure); |
88 MINT_ACCESS_TOKEN_STARTED, | |
89 MINT_ACCESS_TOKEN_DONE, | |
90 ERROR_STATE | |
91 }; | |
92 | 138 |
93 enum SetupError { | 139 void ReportSuccess(const std::string& access_token); |
94 NONE, | 140 void ReportSuccess(const IssueAdviceInfo& issue_advice); |
95 AUTH_ERROR, | |
96 INTERNAL_ERROR, | |
97 USER_CANCELLED, | |
98 | |
99 // This is used for histograms, and should always be the last value. | |
100 SETUP_ERROR_BOUNDARY | |
101 }; | |
102 | |
103 friend class OAuth2MintTokenFlowTest; | |
104 | |
105 // Creates an instance of URLFetcher that does not send or save cookies. | |
106 // The URLFether's method will be GET if body is empty, POST otherwise. | |
107 // Caller owns the returned instance. | |
108 content::URLFetcher* CreateURLFetcher( | |
109 const GURL& url, const std::string& body, const std::string& auth_token); | |
110 void BeginGetLoginAccessToken(); | |
111 void EndGetLoginAccessToken(const GoogleServiceAuthError* error); | |
112 void BeginMintAccessToken(); | |
113 void EndMintAccessToken(const GoogleServiceAuthError* error); | |
114 | |
115 void ReportSuccess(); | |
116 void ReportFailure(const GoogleServiceAuthError& error); | 141 void ReportFailure(const GoogleServiceAuthError& error); |
117 | 142 |
118 static std::string GetErrorString(SetupError error); | 143 static bool ParseIssueAdviceResponse( |
| 144 const base::DictionaryValue* dict, IssueAdviceInfo* issue_advice); |
| 145 static bool ParseMintTokenResponse( |
| 146 const base::DictionaryValue* dict, std::string* access_token); |
119 | 147 |
120 net::URLRequestContextGetter* context_; | 148 net::URLRequestContextGetter* context_; |
121 Delegate* delegate_; | 149 Delegate* delegate_; |
122 State state_; | 150 Parameters parameters_; |
123 | |
124 std::string login_refresh_token_; | |
125 std::string extension_id_; | |
126 std::string client_id_; | |
127 std::vector<std::string> scopes_; | |
128 | |
129 scoped_ptr<OAuth2AccessTokenFetcher> oauth2_access_token_fetcher_; | |
130 scoped_ptr<OAuth2MintTokenFetcher> oauth2_mint_token_fetcher_; | |
131 std::string login_access_token_; | |
132 std::string app_access_token_; | |
133 | 151 |
134 DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFlow); | 152 DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFlow); |
135 }; | 153 }; |
136 | 154 |
137 #endif // CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ | 155 #endif // CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ |
OLD | NEW |