1a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Copyright 2014 The Chromium Authors. All rights reserved.
2a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
3a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)// found in the LICENSE file.
4a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
5a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#ifndef REMOTING_HOST_TOKEN_VALIDATOR_BASE_H_
6a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#define REMOTING_HOST_TOKEN_VALIDATOR_BASE_H_
7a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
8a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/callback.h"
9a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "base/memory/weak_ptr.h"
10a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/url_request/url_request.h"
11a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "net/url_request/url_request_context_getter.h"
12a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "remoting/protocol/token_validator.h"
13a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#include "url/gurl.h"
14a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
15a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace net {
16a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class ClientCertStore;
17a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)typedef std::vector<scoped_refptr<X509Certificate> > CertificateList;
18a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}
19a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
20a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)namespace remoting {
21a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
22a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)struct ThirdPartyAuthConfig {
23a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  inline bool is_empty() const {
24a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return token_url.is_empty() && token_validation_url.is_empty();
25a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
26a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
27a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  inline bool is_valid() const {
28a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    return token_url.is_valid() && token_validation_url.is_valid();
29a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  }
30a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
31a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL token_url;
32a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  GURL token_validation_url;
33a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string token_validation_cert_issuer;
34a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
35a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
36a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)class TokenValidatorBase
37a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)    : public net::URLRequest::Delegate,
38a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      public protocol::TokenValidator {
39a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) public:
40a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  TokenValidatorBase(
41a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const ThirdPartyAuthConfig& third_party_auth_config,
42a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::string& token_scope,
43a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      scoped_refptr<net::URLRequestContextGetter> request_context_getter);
44a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual ~TokenValidatorBase();
45a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
46a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // TokenValidator interface.
47a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void ValidateThirdPartyToken(
48a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const std::string& token,
49a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      const base::Callback<void(
50a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)          const std::string& shared_secret)>& on_token_validated) OVERRIDE;
51a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
52a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual const GURL& token_url() const OVERRIDE;
53a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual const std::string& token_scope() const OVERRIDE;
54a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
55a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // URLRequest::Delegate interface.
56a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void OnResponseStarted(net::URLRequest* source) OVERRIDE;
57a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void OnReadCompleted(net::URLRequest* source,
58a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                               int bytes_read) OVERRIDE;
59a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void OnCertificateRequested(
60a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::URLRequest* source,
61a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)      net::SSLCertRequestInfo* cert_request_info) OVERRIDE;
62a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
63a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) protected:
64a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  void OnCertificatesSelected(net::CertificateList* selected_certs,
65a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)                              net::ClientCertStore* unused);
66a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
67a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void StartValidateRequest(const std::string& token) = 0;
68a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual bool IsValidScope(const std::string& token_scope);
69a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string ProcessResponse();
70a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
71a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Constructor parameters.
72a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  ThirdPartyAuthConfig third_party_auth_config_;
73a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string token_scope_;
74a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_refptr<net::URLRequestContextGetter> request_context_getter_;
75a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
76a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // URLRequest related fields.
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_ptr<net::URLRequest> request_;
78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  scoped_refptr<net::IOBuffer> buffer_;
79a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  std::string data_;
80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::Callback<void(const std::string& shared_secret)> on_token_validated_;
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  base::WeakPtrFactory<TokenValidatorBase> weak_factory_;
84a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
85a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(TokenValidatorBase);
86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)};
87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
88a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)}  // namespace remoting
89a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
90a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)#endif  // REMOTING_HOST_TOKEN_VALIDATOR_BASE_H
91