fake_gaia.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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.
  4. #ifndef GOOGLE_APIS_GAIA_FAKE_GAIA_H_
  5. #define GOOGLE_APIS_GAIA_FAKE_GAIA_H_
  6. #include <memory>
  7. #include <set>
  8. #include <string>
  9. #include "base/callback.h"
  10. #include "base/containers/flat_map.h"
  11. #include "google_apis/gaia/gaia_auth_consumer.h"
  12. #include "net/http/http_status_code.h"
  13. #include "url/gurl.h"
  14. namespace base {
  15. class Value;
  16. }
  17. namespace net {
  18. namespace test_server {
  19. class BasicHttpResponse;
  20. struct HttpRequest;
  21. class HttpResponse;
  22. }
  23. }
  24. // This is a test helper that implements a fake GAIA service for use in browser
  25. // tests. It's mainly intended for use with EmbeddedTestServer, for which it can
  26. // be registered as an additional request handler.
  27. class FakeGaia {
  28. public:
  29. using ScopeSet = std::set<std::string>;
  30. using RefreshTokenToDeviceIdMap = std::map<std::string, std::string>;
  31. // Access token details used for token minting and the token info endpoint.
  32. struct AccessTokenInfo {
  33. AccessTokenInfo();
  34. AccessTokenInfo(const AccessTokenInfo& other);
  35. ~AccessTokenInfo();
  36. std::string token;
  37. std::string issued_to;
  38. std::string audience;
  39. std::string user_id;
  40. ScopeSet scopes;
  41. int expires_in = 3600;
  42. std::string email;
  43. // When set to true, any scope set for issue token request matches |this|.
  44. bool any_scope = false;
  45. std::string id_token;
  46. };
  47. // Cookies and tokens for /MergeSession call seqeunce.
  48. struct MergeSessionParams {
  49. MergeSessionParams();
  50. ~MergeSessionParams();
  51. // Updates params with non-empty values from |params|.
  52. void Update(const MergeSessionParams& params);
  53. // Values of SID and LSID cookie that are set by /ServiceLoginAuth or its
  54. // equivalent at the end of the SAML login flow.
  55. std::string auth_sid_cookie;
  56. std::string auth_lsid_cookie;
  57. // auth_code cookie value response for /o/oauth2/programmatic_auth call.
  58. std::string auth_code;
  59. // OAuth2 refresh access and id token generated by /oauth2/v4/token call
  60. // with "...&grant_type=authorization_code".
  61. std::string refresh_token;
  62. std::string access_token;
  63. std::string id_token;
  64. // Uber token response from /OAuthLogin call.
  65. std::string gaia_uber_token;
  66. // Values of SID and LSID cookie generated from /MergeSession call.
  67. std::string session_sid_cookie;
  68. std::string session_lsid_cookie;
  69. // The e-mail address returned by /ListAccounts.
  70. std::string email;
  71. // List of signed out gaia IDs returned by /ListAccounts.
  72. std::vector<std::string> signed_out_gaia_ids;
  73. };
  74. struct SyncTrustedVaultKeys {
  75. SyncTrustedVaultKeys();
  76. ~SyncTrustedVaultKeys();
  77. std::vector<uint8_t> encryption_key;
  78. int encryption_key_version = 0;
  79. std::vector<std::vector<uint8_t>> trusted_public_keys;
  80. };
  81. FakeGaia();
  82. FakeGaia(const FakeGaia&) = delete;
  83. FakeGaia& operator=(const FakeGaia&) = delete;
  84. virtual ~FakeGaia();
  85. void SetFakeMergeSessionParams(const std::string& email,
  86. const std::string& auth_sid_cookie,
  87. const std::string& auth_lsid_cookie);
  88. // Sets the initial value of tokens and cookies.
  89. void SetMergeSessionParams(const MergeSessionParams& params);
  90. // Updates various params with non-empty values from |params|.
  91. void UpdateMergeSessionParams(const MergeSessionParams& params);
  92. // Sets the specified |gaia_id| as corresponding to the given |email|
  93. // address when setting GAIA response headers. If no mapping is given for
  94. // an email address, a default GAIA Id is used.
  95. void MapEmailToGaiaId(const std::string& email, const std::string& gaia_id);
  96. // Adds sync trusted vault keys for |email|.
  97. void SetSyncTrustedVaultKeys(
  98. const std::string& email,
  99. const SyncTrustedVaultKeys& sync_trusted_vault_keys);
  100. // Initializes HTTP request handlers. Should be called after switches
  101. // for tweaking GaiaUrls are in place.
  102. void Initialize();
  103. // Handles a request and returns a response if the request was recognized as a
  104. // GAIA request. Note that this respects the switches::kGaiaUrl and friends so
  105. // that this can used with EmbeddedTestServer::RegisterRequestHandler().
  106. std::unique_ptr<net::test_server::HttpResponse> HandleRequest(
  107. const net::test_server::HttpRequest& request);
  108. // Configures an OAuth2 token that'll be returned when a client requests an
  109. // access token for the given auth token, which can be a refresh token or an
  110. // login-scoped access token for the token minting endpoint. Note that the
  111. // scope and audience requested by the client need to match the token_info.
  112. void IssueOAuthToken(const std::string& auth_token,
  113. const AccessTokenInfo& token_info);
  114. // Associates an account id with a SAML IdP redirect endpoint. When a
  115. // /ServiceLoginAuth request comes in for that user, it will be redirected
  116. // to the associated redirect endpoint.
  117. void RegisterSamlUser(const std::string& account_id, const GURL& saml_idp);
  118. // Associates an SAML |domain| with a SAML IdP redirect endpoint. When a
  119. // /samlredirect request comes in for this domain, it will be redirected to
  120. // this endpoint.
  121. void RegisterSamlDomainRedirectUrl(const std::string& domain,
  122. const GURL& saml_redirect_url);
  123. void set_issue_oauth_code_cookie(bool value) {
  124. issue_oauth_code_cookie_ = value;
  125. }
  126. // Extracts the parameter named |key| from |query| and places it in |value|.
  127. // Returns false if no parameter is found.
  128. static bool GetQueryParameter(const std::string& query,
  129. const std::string& key,
  130. std::string* value);
  131. // Returns a device ID associated with a given |refresh_token|.
  132. std::string GetDeviceIdByRefreshToken(const std::string& refresh_token) const;
  133. void SetRefreshTokenToDeviceIdMap(
  134. const RefreshTokenToDeviceIdMap& refresh_token_to_device_id_map);
  135. const RefreshTokenToDeviceIdMap& refresh_token_to_device_id_map() const {
  136. return refresh_token_to_device_id_map_;
  137. }
  138. // Returns an email that is filled into the the Email field (if any).
  139. const std::string& prefilled_email() { return prefilled_email_; }
  140. void SetNextReAuthStatus(
  141. GaiaAuthConsumer::ReAuthProofTokenStatus next_status) {
  142. next_reauth_status_ = next_status;
  143. }
  144. // If set, HandleEmbeddedSetupChromeos will serve a hidden iframe that points
  145. // to |frame_src_url|.
  146. void SetIframeOnEmbeddedSetupChromeosUrl(const GURL& frame_src_url) {
  147. embedded_setup_chromeos_iframe_url_ = frame_src_url;
  148. }
  149. // Configures FakeGaia to answer with HTTP status code |http_status_code| and
  150. // an empty body when |gaia_url| is requeqsted. Only |gaia_url|.path() is
  151. // relevant for the URL match.
  152. // To reset, pass |http_status_code| = net::HTTP_OK.
  153. void SetErrorResponse(const GURL& gaia_url,
  154. net::HttpStatusCode http_status_code);
  155. // Returns the is_supervised param from the reauth URL if any.
  156. const std::string& is_supervised() { return is_supervised_; }
  157. // Returns the is_device_owner param from the reauth URL if any.
  158. const std::string& is_device_owner() { return is_device_owner_; }
  159. // Returns the rart param from the embedded setup URL if any.
  160. const std::string& reauth_request_token() { return reauth_request_token_; }
  161. // Returns the fake server's URL that browser tests can visit to trigger a
  162. // RemoveLocalAccount event.
  163. GURL GetFakeRemoveLocalAccountURL(const std::string& gaia_id) const;
  164. void SetFakeSamlContinueResponse(
  165. const std::string& fake_saml_continue_response) {
  166. fake_saml_continue_response_ = fake_saml_continue_response;
  167. }
  168. protected:
  169. // HTTP handler for /MergeSession.
  170. virtual void HandleMergeSession(
  171. const net::test_server::HttpRequest& request,
  172. net::test_server::BasicHttpResponse* http_response);
  173. private:
  174. using AccessTokenInfoMap = std::multimap<std::string, AccessTokenInfo>;
  175. using EmailToGaiaIdMap = std::map<std::string, std::string>;
  176. using SamlAccountIdpMap = std::map<std::string, GURL>;
  177. using SamlDomainRedirectUrlMap = std::map<std::string, GURL>;
  178. using EmailToSyncTrustedVaultKeysMap =
  179. std::map<std::string, SyncTrustedVaultKeys>;
  180. std::string GetGaiaIdOfEmail(const std::string& email) const;
  181. std::string GetEmailOfGaiaId(const std::string& email) const;
  182. void AddGoogleAccountsSigninHeader(
  183. net::test_server::BasicHttpResponse* http_response,
  184. const std::string& email) const;
  185. void SetOAuthCodeCookie(
  186. net::test_server::BasicHttpResponse* http_response) const;
  187. void AddSyncTrustedKeysHeader(
  188. net::test_server::BasicHttpResponse* http_response,
  189. const std::string& email) const;
  190. // Formats a JSON response with the data in |value|, setting the http status
  191. // to |status|.
  192. void FormatJSONResponse(const base::Value& value,
  193. net::HttpStatusCode status,
  194. net::test_server::BasicHttpResponse* http_response);
  195. // Formats a JSON response with the data in |value|, setting the http status
  196. // to net::HTTP_OK.
  197. void FormatOkJSONResponse(const base::Value& value,
  198. net::test_server::BasicHttpResponse* http_response);
  199. using HttpRequestHandlerCallback = base::RepeatingCallback<void(
  200. const net::test_server::HttpRequest& request,
  201. net::test_server::BasicHttpResponse* http_response)>;
  202. using RequestHandlerMap =
  203. base::flat_map<std::string, HttpRequestHandlerCallback>;
  204. using ErrorResponseMap = base::flat_map<std::string, net::HttpStatusCode>;
  205. // Finds the handler for the specified |request_path| by prefix.
  206. // Used as a backup for situations where an exact match doesn't
  207. // find a match.
  208. RequestHandlerMap::iterator FindHandlerByPathPrefix(
  209. const std::string& request_path);
  210. // HTTP request handlers.
  211. void HandleProgramaticAuth(
  212. const net::test_server::HttpRequest& request,
  213. net::test_server::BasicHttpResponse* http_response);
  214. void HandleServiceLogin(const net::test_server::HttpRequest& request,
  215. net::test_server::BasicHttpResponse* http_response);
  216. void HandleEmbeddedSetupChromeos(
  217. const net::test_server::HttpRequest& request,
  218. net::test_server::BasicHttpResponse* http_response);
  219. void HandleEmbeddedReauthChromeos(
  220. const net::test_server::HttpRequest& request,
  221. net::test_server::BasicHttpResponse* http_response);
  222. void HandleOAuthLogin(const net::test_server::HttpRequest& request,
  223. net::test_server::BasicHttpResponse* http_response);
  224. void HandleServiceLoginAuth(
  225. const net::test_server::HttpRequest& request,
  226. net::test_server::BasicHttpResponse* http_response);
  227. void HandleEmbeddedLookupAccountLookup(
  228. const net::test_server::HttpRequest& request,
  229. net::test_server::BasicHttpResponse* http_response);
  230. void HandleEmbeddedSigninChallenge(
  231. const net::test_server::HttpRequest& request,
  232. net::test_server::BasicHttpResponse* http_response);
  233. void HandleSSO(const net::test_server::HttpRequest& request,
  234. net::test_server::BasicHttpResponse* http_response);
  235. void HandleFakeSAMLContinue(
  236. const net::test_server::HttpRequest& request,
  237. net::test_server::BasicHttpResponse* http_response);
  238. void HandleAuthToken(const net::test_server::HttpRequest& request,
  239. net::test_server::BasicHttpResponse* http_response);
  240. void HandleTokenInfo(const net::test_server::HttpRequest& request,
  241. net::test_server::BasicHttpResponse* http_response);
  242. void HandleIssueToken(const net::test_server::HttpRequest& request,
  243. net::test_server::BasicHttpResponse* http_response);
  244. void HandleListAccounts(const net::test_server::HttpRequest& request,
  245. net::test_server::BasicHttpResponse* http_response);
  246. void HandlePeopleGet(const net::test_server::HttpRequest& request,
  247. net::test_server::BasicHttpResponse* http_response);
  248. void HandleGetUserInfo(const net::test_server::HttpRequest& request,
  249. net::test_server::BasicHttpResponse* http_response);
  250. void HandleOAuthUserInfo(const net::test_server::HttpRequest& request,
  251. net::test_server::BasicHttpResponse* http_response);
  252. void HandleSAMLRedirect(const net::test_server::HttpRequest& request,
  253. net::test_server::BasicHttpResponse* http_response);
  254. void HandleGetCheckConnectionInfo(
  255. const net::test_server::HttpRequest& request,
  256. net::test_server::BasicHttpResponse* http_response);
  257. void HandleGetReAuthProofToken(
  258. const net::test_server::HttpRequest& request,
  259. net::test_server::BasicHttpResponse* http_response);
  260. // HTTP handler for /OAuth/Multilogin.
  261. void HandleMultilogin(const net::test_server::HttpRequest& request,
  262. net::test_server::BasicHttpResponse* http_response);
  263. void HandleFakeRemoveLocalAccount(
  264. const net::test_server::HttpRequest& request,
  265. net::test_server::BasicHttpResponse* http_response);
  266. // Returns the access token associated with |auth_token| that matches the
  267. // given |client_id| and |scope_string|. If |scope_string| is empty, the first
  268. // token satisfying the other criteria is returned. Returns NULL if no token
  269. // matches.
  270. const AccessTokenInfo* FindAccessTokenInfo(
  271. const std::string& auth_token,
  272. const std::string& client_id,
  273. const std::string& scope_string) const;
  274. // Returns the access token identified by |access_token| or NULL if not found.
  275. const AccessTokenInfo* GetAccessTokenInfo(
  276. const std::string& access_token) const;
  277. // Returns the response content for HandleEmbeddedSetupChromeos, taking into
  278. // account |embedded_setup_chromeos_iframe_url_| if set.
  279. std::string GetEmbeddedSetupChromeosResponseContent() const;
  280. MergeSessionParams merge_session_params_;
  281. EmailToGaiaIdMap email_to_gaia_id_map_;
  282. AccessTokenInfoMap access_token_info_map_;
  283. RequestHandlerMap request_handlers_;
  284. ErrorResponseMap error_responses_;
  285. std::string embedded_setup_chromeos_response_;
  286. std::string fake_saml_continue_response_;
  287. SamlAccountIdpMap saml_account_idp_map_;
  288. SamlDomainRedirectUrlMap saml_domain_url_map_;
  289. bool issue_oauth_code_cookie_;
  290. RefreshTokenToDeviceIdMap refresh_token_to_device_id_map_;
  291. EmailToSyncTrustedVaultKeysMap email_to_sync_trusted_vault_keys_map_;
  292. std::string prefilled_email_;
  293. std::string is_supervised_;
  294. std::string is_device_owner_;
  295. std::string reauth_request_token_;
  296. GaiaAuthConsumer::ReAuthProofTokenStatus next_reauth_status_ =
  297. GaiaAuthConsumer::ReAuthProofTokenStatus::kSuccess;
  298. GURL embedded_setup_chromeos_iframe_url_;
  299. };
  300. #endif // GOOGLE_APIS_GAIA_FAKE_GAIA_H_