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_
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/memory/scoped_ptr.h"
18 class DictionaryValue;
22 namespace test_server {
23 class BasicHttpResponse;
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.
34 typedef std::set<std::string> ScopeSet;
36 // Access token details used for token minting and the token info endpoint.
37 struct AccessTokenInfo {
42 std::string issued_to;
50 // Cookies and tokens for /MergeSession call seqeunce.
51 struct 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.
82 // Sets the initial value of tokens and cookies.
83 void SetMergeSessionParams(const MergeSessionParams& params);
85 // Initializes HTTP request handlers. Should be called after switches
86 // for tweaking GaiaUrls are in place.
89 // Handles a request and returns a response if the request was recognized as a
90 // GAIA request. Note that this respects the switches::kGaiaUrl and friends so
91 // that this can used with EmbeddedTestServer::RegisterRequestHandler().
92 scoped_ptr<net::test_server::HttpResponse> HandleRequest(
93 const net::test_server::HttpRequest& request);
95 // Configures an OAuth2 token that'll be returned when a client requests an
96 // access token for the given auth token, which can be a refresh token or an
97 // login-scoped access token for the token minting endpoint. Note that the
98 // scope and audience requested by the client need to match the token_info.
99 void IssueOAuthToken(const std::string& auth_token,
100 const AccessTokenInfo& token_info);
102 // Associates an account id with a SAML IdP redirect endpoint. When a
103 // /ServiceLoginAuth request comes in for that user, it will be redirected
104 // to the associated redirect endpoint.
105 void RegisterSamlUser(const std::string& account_id, const GURL& saml_idp);
107 // Extracts the parameter named |key| from |query| and places it in |value|.
108 // Returns false if no parameter is found.
109 static bool GetQueryParameter(const std::string& query,
110 const std::string& key,
113 // HTTP handler for /MergeSession.
114 virtual void HandleMergeSession(
115 const net::test_server::HttpRequest& request,
116 net::test_server::BasicHttpResponse* http_response);
119 typedef std::multimap<std::string, AccessTokenInfo> AccessTokenInfoMap;
120 typedef std::map<std::string, GURL> SamlAccountIdpMap;
122 // Formats a JSON response with the data in |response_dict|.
123 void FormatJSONResponse(const base::DictionaryValue& response_dict,
124 net::test_server::BasicHttpResponse* http_response);
126 typedef base::Callback<void(
127 const net::test_server::HttpRequest& request,
128 net::test_server::BasicHttpResponse* http_response)>
129 HttpRequestHandlerCallback;
130 typedef std::map<std::string, HttpRequestHandlerCallback> RequestHandlerMap;
132 // HTTP request handlers.
133 void HandleProgramaticAuth(
134 const net::test_server::HttpRequest& request,
135 net::test_server::BasicHttpResponse* http_response);
136 void HandleServiceLogin(const net::test_server::HttpRequest& request,
137 net::test_server::BasicHttpResponse* http_response);
138 void HandleOAuthLogin(const net::test_server::HttpRequest& request,
139 net::test_server::BasicHttpResponse* http_response);
140 void HandleServiceLoginAuth(
141 const net::test_server::HttpRequest& request,
142 net::test_server::BasicHttpResponse* http_response);
143 void HandleSSO(const net::test_server::HttpRequest& request,
144 net::test_server::BasicHttpResponse* http_response);
145 void HandleAuthToken(const net::test_server::HttpRequest& request,
146 net::test_server::BasicHttpResponse* http_response);
147 void HandleTokenInfo(const net::test_server::HttpRequest& request,
148 net::test_server::BasicHttpResponse* http_response);
149 void HandleIssueToken(const net::test_server::HttpRequest& request,
150 net::test_server::BasicHttpResponse* http_response);
151 void HandleListAccounts(const net::test_server::HttpRequest& request,
152 net::test_server::BasicHttpResponse* http_response);
154 // Returns the access token associated with |auth_token| that matches the
155 // given |client_id| and |scope_string|. If |scope_string| is empty, the first
156 // token satisfying the other criteria is returned. Returns NULL if no token
158 const AccessTokenInfo* FindAccessTokenInfo(const std::string& auth_token,
159 const std::string& client_id,
160 const std::string& scope_string)
163 MergeSessionParams merge_session_params_;
164 AccessTokenInfoMap access_token_info_map_;
165 RequestHandlerMap request_handlers_;
166 std::string service_login_response_;
167 SamlAccountIdpMap saml_account_idp_map_;
169 DISALLOW_COPY_AND_ASSIGN(FakeGaia);
172 #endif // GOOGLE_APIS_GAIA_FAKE_GAIA_H_