fake_gaia.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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
5#ifndef GOOGLE_APIS_GAIA_FAKE_GAIA_H_
6#define GOOGLE_APIS_GAIA_FAKE_GAIA_H_
7
8#include <map>
9#include <set>
10#include <string>
11
12#include "base/basictypes.h"
13#include "base/callback.h"
14#include "base/memory/scoped_ptr.h"
15#include "url/gurl.h"
16
17namespace base {
18class DictionaryValue;
19}
20
21namespace net {
22namespace test_server {
23class BasicHttpResponse;
24struct HttpRequest;
25class HttpResponse;
26}
27}
28
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.
32class FakeGaia {
33 public:
34  typedef std::set<std::string> ScopeSet;
35
36  // Access token details used for token minting and the token info endpoint.
37  struct AccessTokenInfo {
38    AccessTokenInfo();
39    ~AccessTokenInfo();
40
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;
48  };
49
50  // Cookies and tokens for /MergeSession call seqeunce.
51  struct MergeSessionParams {
52    MergeSessionParams();
53    ~MergeSessionParams();
54
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;
59
60    // auth_code cookie value response for /o/oauth2/programmatic_auth call.
61    std::string auth_code;
62
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;
67
68    // Uber token response from /OAuthLogin call.
69    std::string gaia_uber_token;
70
71    // Values of SID and LSID cookie generated from /MergeSession call.
72    std::string session_sid_cookie;
73    std::string session_lsid_cookie;
74
75    // The e-mail address returned by /ListAccounts.
76    std::string email;
77  };
78
79  FakeGaia();
80  virtual ~FakeGaia();
81
82  // Sets the initial value of tokens and cookies.
83  void SetMergeSessionParams(const MergeSessionParams& params);
84
85  // Initializes HTTP request handlers. Should be called after switches
86  // for tweaking GaiaUrls are in place.
87  void Initialize();
88
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);
94
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);
101
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);
106
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,
111                                std::string* value);
112 protected:
113  // HTTP handler for /MergeSession.
114  virtual void HandleMergeSession(
115      const net::test_server::HttpRequest& request,
116      net::test_server::BasicHttpResponse* http_response);
117
118 private:
119  typedef std::multimap<std::string, AccessTokenInfo> AccessTokenInfoMap;
120  typedef std::map<std::string, GURL> SamlAccountIdpMap;
121
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);
125
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;
131
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);
153
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
157  // matches.
158  const AccessTokenInfo* FindAccessTokenInfo(const std::string& auth_token,
159                                             const std::string& client_id,
160                                             const std::string& scope_string)
161      const;
162
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_;
168
169  DISALLOW_COPY_AND_ASSIGN(FakeGaia);
170};
171
172#endif  // GOOGLE_APIS_GAIA_FAKE_GAIA_H_
173