Roll src/third_party/WebKit d26421b:4b1dbe3 (svn 194839:194840)
[chromium-blink-merge.git] / google_apis / gaia / fake_gaia.h
blobe50b7cb37bbaceddbcd7521f794eeb5953da788b
1 // Copyright (c) 2013 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_FAKE_GAIA_H_
6 #define GOOGLE_APIS_GAIA_FAKE_GAIA_H_
8 #include <map>
9 #include <set>
10 #include <string>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "url/gurl.h"
17 namespace base {
18 class DictionaryValue;
21 namespace net {
22 namespace test_server {
23 class BasicHttpResponse;
24 struct HttpRequest;
25 class HttpResponse;
29 // This is a test helper that implements a fake GAIA service for use in browser
30 // tests. It's mainly intended for use with EmbeddedTestServer, for which it can
31 // be registered as an additional request handler.
32 class FakeGaia {
33 public:
34 typedef std::set<std::string> ScopeSet;
36 // Access token details used for token minting and the token info endpoint.
37 struct AccessTokenInfo {
38 AccessTokenInfo();
39 ~AccessTokenInfo();
41 std::string token;
42 std::string issued_to;
43 std::string audience;
44 std::string user_id;
45 ScopeSet scopes;
46 int expires_in;
47 std::string email;
50 // Cookies and tokens for /MergeSession call seqeunce.
51 struct MergeSessionParams {
52 MergeSessionParams();
53 ~MergeSessionParams();
55 // Values of SID and LSID cookie that are set by /ServiceLoginAuth or its
56 // equivalent at the end of the SAML login flow.
57 std::string auth_sid_cookie;
58 std::string auth_lsid_cookie;
60 // auth_code cookie value response for /o/oauth2/programmatic_auth call.
61 std::string auth_code;
63 // OAuth2 refresh and access token generated by /o/oauth2/token call
64 // with "...&grant_type=authorization_code".
65 std::string refresh_token;
66 std::string access_token;
68 // Uber token response from /OAuthLogin call.
69 std::string gaia_uber_token;
71 // Values of SID and LSID cookie generated from /MergeSession call.
72 std::string session_sid_cookie;
73 std::string session_lsid_cookie;
75 // The e-mail address returned by /ListAccounts.
76 std::string email;
79 FakeGaia();
80 virtual ~FakeGaia();
82 void SetFakeMergeSessionParams(const std::string& email,
83 const std::string& auth_sid_cookie,
84 const std::string& auth_lsid_cookie);
86 // Sets the initial value of tokens and cookies.
87 void SetMergeSessionParams(const MergeSessionParams& params);
89 // Sets the specified |gaia_id| as corresponding to the given |email|
90 // address when setting GAIA response headers. If no mapping is given for
91 // an email address, a default GAIA Id is used.
92 void MapEmailToGaiaId(const std::string& email, const std::string& gaia_id);
94 // Initializes HTTP request handlers. Should be called after switches
95 // for tweaking GaiaUrls are in place.
96 void Initialize();
98 // Handles a request and returns a response if the request was recognized as a
99 // GAIA request. Note that this respects the switches::kGaiaUrl and friends so
100 // that this can used with EmbeddedTestServer::RegisterRequestHandler().
101 scoped_ptr<net::test_server::HttpResponse> HandleRequest(
102 const net::test_server::HttpRequest& request);
104 // Configures an OAuth2 token that'll be returned when a client requests an
105 // access token for the given auth token, which can be a refresh token or an
106 // login-scoped access token for the token minting endpoint. Note that the
107 // scope and audience requested by the client need to match the token_info.
108 void IssueOAuthToken(const std::string& auth_token,
109 const AccessTokenInfo& token_info);
111 // Associates an account id with a SAML IdP redirect endpoint. When a
112 // /ServiceLoginAuth request comes in for that user, it will be redirected
113 // to the associated redirect endpoint.
114 void RegisterSamlUser(const std::string& account_id, const GURL& saml_idp);
116 void set_issue_oauth_code_cookie(bool value) {
117 issue_oauth_code_cookie_ = value;
120 // Extracts the parameter named |key| from |query| and places it in |value|.
121 // Returns false if no parameter is found.
122 static bool GetQueryParameter(const std::string& query,
123 const std::string& key,
124 std::string* value);
125 protected:
126 // HTTP handler for /MergeSession.
127 virtual void HandleMergeSession(
128 const net::test_server::HttpRequest& request,
129 net::test_server::BasicHttpResponse* http_response);
131 private:
132 typedef std::multimap<std::string, AccessTokenInfo> AccessTokenInfoMap;
133 typedef std::map<std::string, std::string> EmailToGaiaIdMap;
134 typedef std::map<std::string, GURL> SamlAccountIdpMap;
136 std::string GetGaiaIdOfEmail(const std::string& email) const;
138 void AddGoogleAccountsSigninHeader(
139 net::test_server::BasicHttpResponse* http_response,
140 const std::string& email) const;
142 void SetOAuthCodeCookie(
143 net::test_server::BasicHttpResponse* http_response) const;
145 // Formats a JSON response with the data in |response_dict|.
146 void FormatJSONResponse(const base::DictionaryValue& response_dict,
147 net::test_server::BasicHttpResponse* http_response);
149 typedef base::Callback<void(
150 const net::test_server::HttpRequest& request,
151 net::test_server::BasicHttpResponse* http_response)>
152 HttpRequestHandlerCallback;
153 typedef std::map<std::string, HttpRequestHandlerCallback> RequestHandlerMap;
155 // HTTP request handlers.
156 void HandleProgramaticAuth(
157 const net::test_server::HttpRequest& request,
158 net::test_server::BasicHttpResponse* http_response);
159 void HandleServiceLogin(const net::test_server::HttpRequest& request,
160 net::test_server::BasicHttpResponse* http_response);
161 void HandleEmbeddedSetupChromeos(
162 const net::test_server::HttpRequest& request,
163 net::test_server::BasicHttpResponse* http_response);
164 void HandleOAuthLogin(const net::test_server::HttpRequest& request,
165 net::test_server::BasicHttpResponse* http_response);
166 void HandleServiceLoginAuth(
167 const net::test_server::HttpRequest& request,
168 net::test_server::BasicHttpResponse* http_response);
169 void HandleEmbeddedLookupAccountLookup(
170 const net::test_server::HttpRequest& request,
171 net::test_server::BasicHttpResponse* http_response);
172 void HandleEmbeddedSigninChallenge(
173 const net::test_server::HttpRequest& request,
174 net::test_server::BasicHttpResponse* http_response);
175 void HandleSSO(const net::test_server::HttpRequest& request,
176 net::test_server::BasicHttpResponse* http_response);
177 void HandleAuthToken(const net::test_server::HttpRequest& request,
178 net::test_server::BasicHttpResponse* http_response);
179 void HandleTokenInfo(const net::test_server::HttpRequest& request,
180 net::test_server::BasicHttpResponse* http_response);
181 void HandleIssueToken(const net::test_server::HttpRequest& request,
182 net::test_server::BasicHttpResponse* http_response);
183 void HandleListAccounts(const net::test_server::HttpRequest& request,
184 net::test_server::BasicHttpResponse* http_response);
185 void HandlePeopleGet(const net::test_server::HttpRequest& request,
186 net::test_server::BasicHttpResponse* http_response);
187 void HandleGetUserInfo(const net::test_server::HttpRequest& request,
188 net::test_server::BasicHttpResponse* http_response);
189 void HandleOAuthUserInfo(const net::test_server::HttpRequest& request,
190 net::test_server::BasicHttpResponse* http_response);
192 // Returns the access token associated with |auth_token| that matches the
193 // given |client_id| and |scope_string|. If |scope_string| is empty, the first
194 // token satisfying the other criteria is returned. Returns NULL if no token
195 // matches.
196 const AccessTokenInfo* FindAccessTokenInfo(
197 const std::string& auth_token,
198 const std::string& client_id,
199 const std::string& scope_string) const;
201 // Returns the access token identified by |access_token| or NULL if not found.
202 const AccessTokenInfo* GetAccessTokenInfo(
203 const std::string& access_token) const;
205 MergeSessionParams merge_session_params_;
206 EmailToGaiaIdMap email_to_gaia_id_map_;
207 AccessTokenInfoMap access_token_info_map_;
208 RequestHandlerMap request_handlers_;
209 std::string service_login_response_;
210 std::string embedded_setup_chromeos_response_;
211 SamlAccountIdpMap saml_account_idp_map_;
212 bool issue_oauth_code_cookie_;
214 DISALLOW_COPY_AND_ASSIGN(FakeGaia);
217 #endif // GOOGLE_APIS_GAIA_FAKE_GAIA_H_