Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / remoting / host / token_validator_base.h
1 // Copyright 2014 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 REMOTING_HOST_TOKEN_VALIDATOR_BASE_H_
6 #define REMOTING_HOST_TOKEN_VALIDATOR_BASE_H_
7
8 #include "base/callback.h"
9 #include "base/memory/weak_ptr.h"
10 #include "net/url_request/url_request.h"
11 #include "net/url_request/url_request_context_getter.h"
12 #include "remoting/protocol/token_validator.h"
13 #include "url/gurl.h"
14
15 namespace net {
16 class ClientCertStore;
17 typedef std::vector<scoped_refptr<X509Certificate> > CertificateList;
18 }
19
20 namespace remoting {
21
22 struct ThirdPartyAuthConfig {
23   inline bool is_empty() const {
24     return token_url.is_empty() && token_validation_url.is_empty();
25   }
26
27   inline bool is_valid() const {
28     return token_url.is_valid() && token_validation_url.is_valid();
29   }
30
31   GURL token_url;
32   GURL token_validation_url;
33   std::string token_validation_cert_issuer;
34 };
35
36 class TokenValidatorBase
37     : public net::URLRequest::Delegate,
38       public protocol::TokenValidator {
39  public:
40   TokenValidatorBase(
41       const ThirdPartyAuthConfig& third_party_auth_config,
42       const std::string& token_scope,
43       scoped_refptr<net::URLRequestContextGetter> request_context_getter);
44   ~TokenValidatorBase() override;
45
46   // TokenValidator interface.
47   void ValidateThirdPartyToken(
48       const std::string& token,
49       const base::Callback<void(const std::string& shared_secret)>&
50           on_token_validated) override;
51
52   const GURL& token_url() const override;
53   const std::string& token_scope() const override;
54
55   // URLRequest::Delegate interface.
56   void OnResponseStarted(net::URLRequest* source) override;
57   void OnReadCompleted(net::URLRequest* source, int bytes_read) override;
58   void OnCertificateRequested(
59       net::URLRequest* source,
60       net::SSLCertRequestInfo* cert_request_info) override;
61
62  protected:
63   void OnCertificatesSelected(net::CertificateList* selected_certs,
64                               net::ClientCertStore* unused);
65
66   virtual void StartValidateRequest(const std::string& token) = 0;
67   virtual bool IsValidScope(const std::string& token_scope);
68   std::string ProcessResponse();
69
70   // Constructor parameters.
71   ThirdPartyAuthConfig third_party_auth_config_;
72   std::string token_scope_;
73   scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
74
75   // URLRequest related fields.
76   scoped_ptr<net::URLRequest> request_;
77   scoped_refptr<net::IOBuffer> buffer_;
78   std::string data_;
79
80   base::Callback<void(const std::string& shared_secret)> on_token_validated_;
81
82   base::WeakPtrFactory<TokenValidatorBase> weak_factory_;
83
84   DISALLOW_COPY_AND_ASSIGN(TokenValidatorBase);
85 };
86
87 }  // namespace remoting
88
89 #endif  // REMOTING_HOST_TOKEN_VALIDATOR_BASE_H