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 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.
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 // Extracts the parameter named |key| from |query| and places it in |value|.
117 // Returns false if no parameter is found.
118 static bool GetQueryParameter(const std::string
& query
,
119 const std::string
& key
,
122 // HTTP handler for /MergeSession.
123 virtual void HandleMergeSession(
124 const net::test_server::HttpRequest
& request
,
125 net::test_server::BasicHttpResponse
* http_response
);
128 typedef std::multimap
<std::string
, AccessTokenInfo
> AccessTokenInfoMap
;
129 typedef std::map
<std::string
, std::string
> EmailToGaiaIdMap
;
130 typedef std::map
<std::string
, GURL
> SamlAccountIdpMap
;
132 std::string
GetGaiaIdOfEmail(const std::string
& email
) const;
134 void AddGoogleAccountsSigninHeader(
135 net::test_server::BasicHttpResponse
* http_response
,
136 const std::string
& email
) const;
138 // Formats a JSON response with the data in |response_dict|.
139 void FormatJSONResponse(const base::DictionaryValue
& response_dict
,
140 net::test_server::BasicHttpResponse
* http_response
);
142 typedef base::Callback
<void(
143 const net::test_server::HttpRequest
& request
,
144 net::test_server::BasicHttpResponse
* http_response
)>
145 HttpRequestHandlerCallback
;
146 typedef std::map
<std::string
, HttpRequestHandlerCallback
> RequestHandlerMap
;
148 // HTTP request handlers.
149 void HandleProgramaticAuth(
150 const net::test_server::HttpRequest
& request
,
151 net::test_server::BasicHttpResponse
* http_response
);
152 void HandleServiceLogin(const net::test_server::HttpRequest
& request
,
153 net::test_server::BasicHttpResponse
* http_response
);
154 void HandleOAuthLogin(const net::test_server::HttpRequest
& request
,
155 net::test_server::BasicHttpResponse
* http_response
);
156 void HandleServiceLoginAuth(
157 const net::test_server::HttpRequest
& request
,
158 net::test_server::BasicHttpResponse
* http_response
);
159 void HandleSSO(const net::test_server::HttpRequest
& request
,
160 net::test_server::BasicHttpResponse
* http_response
);
161 void HandleAuthToken(const net::test_server::HttpRequest
& request
,
162 net::test_server::BasicHttpResponse
* http_response
);
163 void HandleTokenInfo(const net::test_server::HttpRequest
& request
,
164 net::test_server::BasicHttpResponse
* http_response
);
165 void HandleIssueToken(const net::test_server::HttpRequest
& request
,
166 net::test_server::BasicHttpResponse
* http_response
);
167 void HandleListAccounts(const net::test_server::HttpRequest
& request
,
168 net::test_server::BasicHttpResponse
* http_response
);
169 void HandlePeopleGet(const net::test_server::HttpRequest
& request
,
170 net::test_server::BasicHttpResponse
* http_response
);
171 void HandleGetUserInfo(const net::test_server::HttpRequest
& request
,
172 net::test_server::BasicHttpResponse
* http_response
);
174 // Returns the access token associated with |auth_token| that matches the
175 // given |client_id| and |scope_string|. If |scope_string| is empty, the first
176 // token satisfying the other criteria is returned. Returns NULL if no token
178 const AccessTokenInfo
* FindAccessTokenInfo(const std::string
& auth_token
,
179 const std::string
& client_id
,
180 const std::string
& scope_string
)
183 MergeSessionParams merge_session_params_
;
184 EmailToGaiaIdMap email_to_gaia_id_map_
;
185 AccessTokenInfoMap access_token_info_map_
;
186 RequestHandlerMap request_handlers_
;
187 std::string service_login_response_
;
188 SamlAccountIdpMap saml_account_idp_map_
;
190 DISALLOW_COPY_AND_ASSIGN(FakeGaia
);
193 #endif // GOOGLE_APIS_GAIA_FAKE_GAIA_H_