Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / google_apis / gaia / fake_gaia.h
blob1f1a3eff9c5c58353c8d56a7d61dafecbacf5a5d
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 using ScopeSet = std::set<std::string>;
35 using RefreshTokenToDeviceIdMap = std::map<std::string, std::string>;
37 // Access token details used for token minting and the token info endpoint.
38 struct AccessTokenInfo {
39 AccessTokenInfo();
40 ~AccessTokenInfo();
42 std::string token;
43 std::string issued_to;
44 std::string audience;
45 std::string user_id;
46 ScopeSet scopes;
47 int expires_in;
48 std::string email;
51 // Cookies and tokens for /MergeSession call seqeunce.
52 struct MergeSessionParams {
53 MergeSessionParams();
54 ~MergeSessionParams();
56 // Updates params with non-empty values from |params|.
57 void Update(const MergeSessionParams& params);
59 // Values of SID and LSID cookie that are set by /ServiceLoginAuth or its
60 // equivalent at the end of the SAML login flow.
61 std::string auth_sid_cookie;
62 std::string auth_lsid_cookie;
64 // auth_code cookie value response for /o/oauth2/programmatic_auth call.
65 std::string auth_code;
67 // OAuth2 refresh and access token generated by /o/oauth2/token call
68 // with "...&grant_type=authorization_code".
69 std::string refresh_token;
70 std::string access_token;
72 // Uber token response from /OAuthLogin call.
73 std::string gaia_uber_token;
75 // Values of SID and LSID cookie generated from /MergeSession call.
76 std::string session_sid_cookie;
77 std::string session_lsid_cookie;
79 // The e-mail address returned by /ListAccounts.
80 std::string email;
83 FakeGaia();
84 virtual ~FakeGaia();
86 void SetFakeMergeSessionParams(const std::string& email,
87 const std::string& auth_sid_cookie,
88 const std::string& auth_lsid_cookie);
90 // Sets the initial value of tokens and cookies.
91 void SetMergeSessionParams(const MergeSessionParams& params);
93 // Updates various params with non-empty values from |params|.
94 void UpdateMergeSessionParams(const MergeSessionParams& params);
96 // Sets the specified |gaia_id| as corresponding to the given |email|
97 // address when setting GAIA response headers. If no mapping is given for
98 // an email address, a default GAIA Id is used.
99 void MapEmailToGaiaId(const std::string& email, const std::string& gaia_id);
101 // Initializes HTTP request handlers. Should be called after switches
102 // for tweaking GaiaUrls are in place.
103 void Initialize();
105 // Handles a request and returns a response if the request was recognized as a
106 // GAIA request. Note that this respects the switches::kGaiaUrl and friends so
107 // that this can used with EmbeddedTestServer::RegisterRequestHandler().
108 scoped_ptr<net::test_server::HttpResponse> HandleRequest(
109 const net::test_server::HttpRequest& request);
111 // Configures an OAuth2 token that'll be returned when a client requests an
112 // access token for the given auth token, which can be a refresh token or an
113 // login-scoped access token for the token minting endpoint. Note that the
114 // scope and audience requested by the client need to match the token_info.
115 void IssueOAuthToken(const std::string& auth_token,
116 const AccessTokenInfo& token_info);
118 // Associates an account id with a SAML IdP redirect endpoint. When a
119 // /ServiceLoginAuth request comes in for that user, it will be redirected
120 // to the associated redirect endpoint.
121 void RegisterSamlUser(const std::string& account_id, const GURL& saml_idp);
123 void set_issue_oauth_code_cookie(bool value) {
124 issue_oauth_code_cookie_ = value;
127 // Extracts the parameter named |key| from |query| and places it in |value|.
128 // Returns false if no parameter is found.
129 static bool GetQueryParameter(const std::string& query,
130 const std::string& key,
131 std::string* value);
133 // Returns a device ID associated with a given |refresh_token|.
134 std::string GetDeviceIdByRefreshToken(const std::string& refresh_token) const;
136 void SetRefreshTokenToDeviceIdMap(
137 const RefreshTokenToDeviceIdMap& refresh_token_to_device_id_map);
139 const RefreshTokenToDeviceIdMap& refresh_token_to_device_id_map() const {
140 return refresh_token_to_device_id_map_;
143 protected:
144 // HTTP handler for /MergeSession.
145 virtual void HandleMergeSession(
146 const net::test_server::HttpRequest& request,
147 net::test_server::BasicHttpResponse* http_response);
149 private:
150 typedef std::multimap<std::string, AccessTokenInfo> AccessTokenInfoMap;
151 typedef std::map<std::string, std::string> EmailToGaiaIdMap;
152 typedef std::map<std::string, GURL> SamlAccountIdpMap;
154 std::string GetGaiaIdOfEmail(const std::string& email) const;
156 void AddGoogleAccountsSigninHeader(
157 net::test_server::BasicHttpResponse* http_response,
158 const std::string& email) const;
160 void SetOAuthCodeCookie(
161 net::test_server::BasicHttpResponse* http_response) const;
163 // Formats a JSON response with the data in |response_dict|.
164 void FormatJSONResponse(const base::DictionaryValue& response_dict,
165 net::test_server::BasicHttpResponse* http_response);
167 typedef base::Callback<void(
168 const net::test_server::HttpRequest& request,
169 net::test_server::BasicHttpResponse* http_response)>
170 HttpRequestHandlerCallback;
171 typedef std::map<std::string, HttpRequestHandlerCallback> RequestHandlerMap;
173 // HTTP request handlers.
174 void HandleProgramaticAuth(
175 const net::test_server::HttpRequest& request,
176 net::test_server::BasicHttpResponse* http_response);
177 void HandleServiceLogin(const net::test_server::HttpRequest& request,
178 net::test_server::BasicHttpResponse* http_response);
179 void HandleEmbeddedSetupChromeos(
180 const net::test_server::HttpRequest& request,
181 net::test_server::BasicHttpResponse* http_response);
182 void HandleOAuthLogin(const net::test_server::HttpRequest& request,
183 net::test_server::BasicHttpResponse* http_response);
184 void HandleServiceLoginAuth(
185 const net::test_server::HttpRequest& request,
186 net::test_server::BasicHttpResponse* http_response);
187 void HandleEmbeddedLookupAccountLookup(
188 const net::test_server::HttpRequest& request,
189 net::test_server::BasicHttpResponse* http_response);
190 void HandleEmbeddedSigninChallenge(
191 const net::test_server::HttpRequest& request,
192 net::test_server::BasicHttpResponse* http_response);
193 void HandleSSO(const net::test_server::HttpRequest& request,
194 net::test_server::BasicHttpResponse* http_response);
195 void HandleAuthToken(const net::test_server::HttpRequest& request,
196 net::test_server::BasicHttpResponse* http_response);
197 void HandleTokenInfo(const net::test_server::HttpRequest& request,
198 net::test_server::BasicHttpResponse* http_response);
199 void HandleIssueToken(const net::test_server::HttpRequest& request,
200 net::test_server::BasicHttpResponse* http_response);
201 void HandleListAccounts(const net::test_server::HttpRequest& request,
202 net::test_server::BasicHttpResponse* http_response);
203 void HandlePeopleGet(const net::test_server::HttpRequest& request,
204 net::test_server::BasicHttpResponse* http_response);
205 void HandleGetUserInfo(const net::test_server::HttpRequest& request,
206 net::test_server::BasicHttpResponse* http_response);
207 void HandleOAuthUserInfo(const net::test_server::HttpRequest& request,
208 net::test_server::BasicHttpResponse* http_response);
210 // Returns the access token associated with |auth_token| that matches the
211 // given |client_id| and |scope_string|. If |scope_string| is empty, the first
212 // token satisfying the other criteria is returned. Returns NULL if no token
213 // matches.
214 const AccessTokenInfo* FindAccessTokenInfo(
215 const std::string& auth_token,
216 const std::string& client_id,
217 const std::string& scope_string) const;
219 // Returns the access token identified by |access_token| or NULL if not found.
220 const AccessTokenInfo* GetAccessTokenInfo(
221 const std::string& access_token) const;
223 MergeSessionParams merge_session_params_;
224 EmailToGaiaIdMap email_to_gaia_id_map_;
225 AccessTokenInfoMap access_token_info_map_;
226 RequestHandlerMap request_handlers_;
227 std::string service_login_response_;
228 std::string embedded_setup_chromeos_response_;
229 SamlAccountIdpMap saml_account_idp_map_;
230 bool issue_oauth_code_cookie_;
231 RefreshTokenToDeviceIdMap refresh_token_to_device_id_map_;
233 DISALLOW_COPY_AND_ASSIGN(FakeGaia);
236 #endif // GOOGLE_APIS_GAIA_FAKE_GAIA_H_