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 17 namespace base { 18 class DictionaryValue; 19 } 20 21 namespace net { 22 namespace test_server { 23 class BasicHttpResponse; 24 struct HttpRequest; 25 class 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. 32 class 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 void SetFakeMergeSessionParams(const std::string& email, 83 const std::string& auth_sid_cookie, 84 const std::string& auth_lsid_cookie); 85 86 // Sets the initial value of tokens and cookies. 87 void SetMergeSessionParams(const MergeSessionParams& params); 88 89 // Initializes HTTP request handlers. Should be called after switches 90 // for tweaking GaiaUrls are in place. 91 void Initialize(); 92 93 // Handles a request and returns a response if the request was recognized as a 94 // GAIA request. Note that this respects the switches::kGaiaUrl and friends so 95 // that this can used with EmbeddedTestServer::RegisterRequestHandler(). 96 scoped_ptr<net::test_server::HttpResponse> HandleRequest( 97 const net::test_server::HttpRequest& request); 98 99 // Configures an OAuth2 token that'll be returned when a client requests an 100 // access token for the given auth token, which can be a refresh token or an 101 // login-scoped access token for the token minting endpoint. Note that the 102 // scope and audience requested by the client need to match the token_info. 103 void IssueOAuthToken(const std::string& auth_token, 104 const AccessTokenInfo& token_info); 105 106 // Associates an account id with a SAML IdP redirect endpoint. When a 107 // /ServiceLoginAuth request comes in for that user, it will be redirected 108 // to the associated redirect endpoint. 109 void RegisterSamlUser(const std::string& account_id, const GURL& saml_idp); 110 111 // Extracts the parameter named |key| from |query| and places it in |value|. 112 // Returns false if no parameter is found. 113 static bool GetQueryParameter(const std::string& query, 114 const std::string& key, 115 std::string* value); 116 protected: 117 // HTTP handler for /MergeSession. 118 virtual void HandleMergeSession( 119 const net::test_server::HttpRequest& request, 120 net::test_server::BasicHttpResponse* http_response); 121 122 private: 123 typedef std::multimap<std::string, AccessTokenInfo> AccessTokenInfoMap; 124 typedef std::map<std::string, GURL> SamlAccountIdpMap; 125 126 // Formats a JSON response with the data in |response_dict|. 127 void FormatJSONResponse(const base::DictionaryValue& response_dict, 128 net::test_server::BasicHttpResponse* http_response); 129 130 typedef base::Callback<void( 131 const net::test_server::HttpRequest& request, 132 net::test_server::BasicHttpResponse* http_response)> 133 HttpRequestHandlerCallback; 134 typedef std::map<std::string, HttpRequestHandlerCallback> RequestHandlerMap; 135 136 // HTTP request handlers. 137 void HandleProgramaticAuth( 138 const net::test_server::HttpRequest& request, 139 net::test_server::BasicHttpResponse* http_response); 140 void HandleServiceLogin(const net::test_server::HttpRequest& request, 141 net::test_server::BasicHttpResponse* http_response); 142 void HandleOAuthLogin(const net::test_server::HttpRequest& request, 143 net::test_server::BasicHttpResponse* http_response); 144 void HandleServiceLoginAuth( 145 const net::test_server::HttpRequest& request, 146 net::test_server::BasicHttpResponse* http_response); 147 void HandleSSO(const net::test_server::HttpRequest& request, 148 net::test_server::BasicHttpResponse* http_response); 149 void HandleAuthToken(const net::test_server::HttpRequest& request, 150 net::test_server::BasicHttpResponse* http_response); 151 void HandleTokenInfo(const net::test_server::HttpRequest& request, 152 net::test_server::BasicHttpResponse* http_response); 153 void HandleIssueToken(const net::test_server::HttpRequest& request, 154 net::test_server::BasicHttpResponse* http_response); 155 void HandleListAccounts(const net::test_server::HttpRequest& request, 156 net::test_server::BasicHttpResponse* http_response); 157 void HandlePeopleGet(const net::test_server::HttpRequest& request, 158 net::test_server::BasicHttpResponse* http_response); 159 void HandleGetUserInfo(const net::test_server::HttpRequest& request, 160 net::test_server::BasicHttpResponse* http_response); 161 162 // Returns the access token associated with |auth_token| that matches the 163 // given |client_id| and |scope_string|. If |scope_string| is empty, the first 164 // token satisfying the other criteria is returned. Returns NULL if no token 165 // matches. 166 const AccessTokenInfo* FindAccessTokenInfo(const std::string& auth_token, 167 const std::string& client_id, 168 const std::string& scope_string) 169 const; 170 171 MergeSessionParams merge_session_params_; 172 AccessTokenInfoMap access_token_info_map_; 173 RequestHandlerMap request_handlers_; 174 std::string service_login_response_; 175 SamlAccountIdpMap saml_account_idp_map_; 176 177 DISALLOW_COPY_AND_ASSIGN(FakeGaia); 178 }; 179 180 #endif // GOOGLE_APIS_GAIA_FAKE_GAIA_H_ 181