fake_gaia.h revision a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7
1d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// found in the LICENSE file.
4d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
5d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#ifndef GOOGLE_APIS_GAIA_FAKE_GAIA_H_
6d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#define GOOGLE_APIS_GAIA_FAKE_GAIA_H_
7d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
8d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <map>
9d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <set>
10d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <string>
11d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
12d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/basictypes.h"
13d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include "base/memory/scoped_ptr.h"
14d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
15d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)namespace base {
16d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class DictionaryValue;
17d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
18d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
19d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)namespace net {
20d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)namespace test_server {
21d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class BasicHttpResponse;
22d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)struct HttpRequest;
23d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class HttpResponse;
24d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
25d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)}
26d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
27d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// This is a test helper that implements a fake GAIA service for use in browser
28d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// tests. It's mainly intended for use with EmbeddedTestServer, for which it can
29d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)// be registered as an additional request handler.
30d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)class FakeGaia {
31d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) public:
32d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  typedef std::set<std::string> ScopeSet;
33d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
34d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Access token details used for token minting and the token info endpoint.
35d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  struct AccessTokenInfo {
36d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    AccessTokenInfo();
37d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ~AccessTokenInfo();
38d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
39d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    std::string token;
40d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    std::string issued_to;
41d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    std::string audience;
42d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    std::string user_id;
43d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    ScopeSet scopes;
44d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    int expires_in;
45d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)    std::string email;
46d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  };
47d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
48d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  FakeGaia();
49d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  ~FakeGaia();
50d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
51d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Handles a request and returns a response if the request was recognized as a
52d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // GAIA request. Note that this respects the switches::kGaiaUrl and friends so
53d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // that this can used with EmbeddedTestServer::RegisterRequestHandler().
54d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  scoped_ptr<net::test_server::HttpResponse> HandleRequest(
55d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)      const net::test_server::HttpRequest& request);
56d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
57d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Configures an OAuth2 token that'll be returned when a client requests an
58a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // access token for the given auth token, which can be a refresh token or an
59a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // login-scoped access token for the token minting endpoint. Note that the
60a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // scope and audience requested by the client need to match the token_info.
61a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  void IssueOAuthToken(const std::string& auth_token,
62d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                       const AccessTokenInfo& token_info);
63d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
64d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) private:
65a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  typedef std::multimap<std::string, AccessTokenInfo> AccessTokenInfoMap;
66d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
67d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Formats a JSON response with the data in |response_dict|.
68d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  void FormatJSONResponse(const base::DictionaryValue& response_dict,
69d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                          net::test_server::BasicHttpResponse* http_response);
70d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
71a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // Returns the access token associated with |auth_token| that matches the
72a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // given |client_id| and |scope_string|. If |scope_string| is empty, the first
73a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // token satisfying the other criteria is returned. Returns NULL if no token
74a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  // matches.
75a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  const AccessTokenInfo* GetAccessTokenInfo(const std::string& auth_token,
76a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                            const std::string& client_id,
77a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)                                            const std::string& scope_string)
78a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const;
79d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
80d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Extracts the parameter named |key| from |query| and places it in |value|.
81d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // Returns false if no parameter is found.
82d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  static bool GetQueryParameter(const std::string& query,
83d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                const std::string& key,
84d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)                                std::string* value);
85d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
86d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  AccessTokenInfoMap access_token_info_map_;
87d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  std::string service_login_response_;
88d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
89d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(FakeGaia);
90d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)};
91d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
92d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#endif  // GOOGLE_APIS_GAIA_FAKE_GAIA_H_
93