OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ | |
6 #define CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ | |
7 | |
8 #include <string> | |
9 #include <vector> | |
10 | |
11 #include "base/memory/weak_ptr.h" | |
12 #include "base/string16.h" | |
13 #include "chrome/common/net/gaia/oauth2_api_call_flow.h" | |
14 | |
15 class GoogleServiceAuthError; | |
16 class OAuth2MintTokenFlowTest; | |
17 | |
18 namespace base { | |
19 class DictionaryValue; | |
20 } | |
21 | |
22 namespace content { | |
23 class URLFetcher; | |
24 } | |
25 | |
26 namespace net { | |
27 class URLRequestContextGetter; | |
28 } | |
29 | |
30 // IssueAdvice: messages to show to the user to get a user's approval. | |
31 // The structure is as follows: | |
32 // * Description 1 | |
33 // - Detail 1.1 | |
34 // - Details 1.2 | |
35 // * Description 2 | |
36 // - Detail 2.1 | |
37 // - Detail 2.2 | |
38 // - Detail 2.3 | |
39 // * Description 3 | |
40 // - Detail 3.1 | |
41 struct IssueAdviceInfoEntry { | |
42 public: | |
43 IssueAdviceInfoEntry(); | |
44 ~IssueAdviceInfoEntry(); | |
45 | |
46 string16 description; | |
47 std::vector<string16> details; | |
48 | |
49 bool operator==(const IssueAdviceInfoEntry& rhs) const; | |
50 }; | |
51 | |
52 typedef std::vector<IssueAdviceInfoEntry> IssueAdviceInfo; | |
53 | |
54 // This class implements the OAuth2 flow to Google to mint an OAuth2 | |
55 // token for the given client and the given set of scopes from the | |
56 // OAuthLogin scoped "master" OAuth2 token for the user logged in to | |
57 // Chrome. | |
58 class OAuth2MintTokenFlow : public OAuth2ApiCallFlow { | |
59 public: | |
60 // There are four differnt modes when minting a token to grant | |
61 // access to third-party app for a user. | |
62 enum Mode { | |
63 // Get the messages to display to the user without minting a token. | |
64 MODE_ISSUE_ADVICE, | |
65 // Record a grant but do not get a token back. | |
66 MODE_RECORD_GRANT, | |
67 // Mint a token for an existing grant. | |
68 MODE_MINT_TOKEN_NO_FORCE, | |
69 // Mint a token forcefully even if there is no existing grant. | |
70 MODE_MINT_TOKEN_FORCE, | |
71 }; | |
72 | |
73 // Parameters needed to mint a token. | |
74 struct Parameters { | |
75 public: | |
76 Parameters(); | |
77 Parameters(const std::string& rt, | |
78 const std::string& eid, | |
79 const std::string& cid, | |
80 const std::vector<std::string>& scopes_arg, | |
81 Mode mode_arg); | |
82 ~Parameters(); | |
83 | |
84 std::string login_refresh_token; | |
85 std::string extension_id; | |
86 std::string client_id; | |
87 std::vector<std::string> scopes; | |
88 Mode mode; | |
89 }; | |
90 | |
91 class Delegate { | |
92 public: | |
93 virtual void OnMintTokenSuccess(const std::string& access_token) {} | |
94 virtual void OnIssueAdviceSuccess(const IssueAdviceInfo& issue_advice) {} | |
95 virtual void OnMintTokenFailure(const GoogleServiceAuthError& error) {} | |
96 | |
97 protected: | |
98 virtual ~Delegate() {} | |
99 }; | |
100 | |
101 OAuth2MintTokenFlow(net::URLRequestContextGetter* context, | |
102 Delegate* delegate, | |
103 const Parameters& parameters); | |
104 virtual ~OAuth2MintTokenFlow(); | |
105 | |
106 // Starts the flow, and deletes |this| when done. Useful when the caller | |
107 // does not care about the response (|delegate_| is NULL). | |
108 void FireAndForget(); | |
109 | |
110 protected: | |
111 // Implementation of template methods in OAuth2ApiCallFlow. | |
112 virtual GURL CreateApiCallUrl() OVERRIDE; | |
113 virtual std::string CreateApiCallBody() OVERRIDE; | |
114 | |
115 virtual void ProcessApiCallSuccess( | |
116 const net::URLFetcher* source) OVERRIDE; | |
117 virtual void ProcessApiCallFailure( | |
118 const net::URLFetcher* source) OVERRIDE; | |
119 virtual void ProcessNewAccessToken(const std::string& access_token) OVERRIDE; | |
120 virtual void ProcessMintAccessTokenFailure( | |
121 const GoogleServiceAuthError& error) OVERRIDE; | |
122 | |
123 private: | |
124 friend class OAuth2MintTokenFlowTest; | |
125 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, CreateApiCallBody); | |
126 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ParseIssueAdviceResponse); | |
127 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ParseMintTokenResponse); | |
128 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ProcessApiCallSuccess); | |
129 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, ProcessApiCallFailure); | |
130 FRIEND_TEST_ALL_PREFIXES(OAuth2MintTokenFlowTest, | |
131 ProcessMintAccessTokenFailure); | |
132 | |
133 void ReportSuccess(const std::string& access_token); | |
134 void ReportIssueAdviceSuccess(const IssueAdviceInfo& issue_advice); | |
135 void ReportFailure(const GoogleServiceAuthError& error); | |
136 | |
137 static bool ParseIssueAdviceResponse( | |
138 const base::DictionaryValue* dict, IssueAdviceInfo* issue_advice); | |
139 static bool ParseMintTokenResponse( | |
140 const base::DictionaryValue* dict, std::string* access_token); | |
141 | |
142 net::URLRequestContextGetter* context_; | |
143 Delegate* delegate_; | |
144 Parameters parameters_; | |
145 // If true, |this| owns itself and will delete itself after reporting | |
146 // success or failure. | |
147 bool delete_when_done_; | |
148 base::WeakPtrFactory<OAuth2MintTokenFlow> weak_factory_; | |
149 | |
150 DISALLOW_COPY_AND_ASSIGN(OAuth2MintTokenFlow); | |
151 }; | |
152 | |
153 #endif // CHROME_COMMON_NET_GAIA_OAUTH2_MINT_TOKEN_FLOW_H_ | |
OLD | NEW |