[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 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 | |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 10 | #include "base/callback.h" |
avi | c5960f3 | 2015-12-22 22:49:48 | [diff] [blame] | 11 | #include "base/macros.h" |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 12 | #include "base/memory/weak_ptr.h" |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 13 | #include "net/ssl/client_cert_identity.h" |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 14 | #include "net/url_request/url_request.h" |
| 15 | #include "net/url_request/url_request_context_getter.h" |
lukasza | 0d40d8a | 2015-03-03 18:36:28 | [diff] [blame] | 16 | #include "remoting/host/third_party_auth_config.h" |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 17 | #include "remoting/protocol/token_validator.h" |
| 18 | #include "url/gurl.h" |
| 19 | |
| 20 | namespace net { |
| 21 | class ClientCertStore; |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | namespace remoting { |
| 25 | |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 26 | class TokenValidatorBase |
| 27 | : public net::URLRequest::Delegate, |
| 28 | public protocol::TokenValidator { |
| 29 | public: |
| 30 | TokenValidatorBase( |
| 31 | const ThirdPartyAuthConfig& third_party_auth_config, |
| 32 | const std::string& token_scope, |
| 33 | scoped_refptr<net::URLRequestContextGetter> request_context_getter); |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 34 | ~TokenValidatorBase() override; |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 35 | |
| 36 | // TokenValidator interface. |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 37 | void ValidateThirdPartyToken( |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 38 | const std::string& token, |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 39 | const base::Callback<void(const std::string& shared_secret)>& |
| 40 | on_token_validated) override; |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 41 | |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 42 | const GURL& token_url() const override; |
| 43 | const std::string& token_scope() const override; |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 44 | |
| 45 | // URLRequest::Delegate interface. |
maksim.sisov | c023fa2 | 2016-09-22 04:16:32 | [diff] [blame] | 46 | void OnResponseStarted(net::URLRequest* source, int net_result) override; |
| 47 | void OnReadCompleted(net::URLRequest* source, int net_result) override; |
lambroslambrou | f43816ad | 2015-12-16 03:50:14 | [diff] [blame] | 48 | void OnReceivedRedirect(net::URLRequest* request, |
| 49 | const net::RedirectInfo& redirect_info, |
| 50 | bool* defer_redirect) override; |
dcheng | 562aba5 | 2014-10-21 12:30:14 | [diff] [blame] | 51 | void OnCertificateRequested( |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 52 | net::URLRequest* source, |
mostynb | 11d989c | 2014-10-08 16:58:09 | [diff] [blame] | 53 | net::SSLCertRequestInfo* cert_request_info) override; |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 54 | |
| 55 | protected: |
mattm | 7ed243f | 2017-04-28 05:28:58 | [diff] [blame] | 56 | void OnCertificatesSelected(net::ClientCertStore* unused, |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 57 | net::ClientCertIdentityList selected_certs); |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 58 | |
| 59 | virtual void StartValidateRequest(const std::string& token) = 0; |
mattm | 436ccfe | 2017-06-19 20:24:08 | [diff] [blame] | 60 | virtual void ContinueWithCertificate( |
| 61 | scoped_refptr<net::X509Certificate> client_cert, |
| 62 | scoped_refptr<net::SSLPrivateKey> client_private_key); |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 63 | virtual bool IsValidScope(const std::string& token_scope); |
maksim.sisov | c023fa2 | 2016-09-22 04:16:32 | [diff] [blame] | 64 | std::string ProcessResponse(int net_result); |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 65 | |
| 66 | // Constructor parameters. |
| 67 | ThirdPartyAuthConfig third_party_auth_config_; |
| 68 | std::string token_scope_; |
| 69 | scoped_refptr<net::URLRequestContextGetter> request_context_getter_; |
| 70 | |
| 71 | // URLRequest related fields. |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 72 | std::unique_ptr<net::URLRequest> request_; |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 73 | scoped_refptr<net::IOBuffer> buffer_; |
| 74 | std::string data_; |
| 75 | |
lambroslambrou | f43816ad | 2015-12-16 03:50:14 | [diff] [blame] | 76 | // This is set by OnReceivedRedirect() if the token validation request is |
| 77 | // being re-submitted as a POST request. This can happen if the authentication |
| 78 | // cookie has not yet been set, and a login handler redirection causes the |
| 79 | // POST request to be turned into a GET operation, losing the POST data. In |
| 80 | // this case, an immediate retry (with the same cookie jar) is expected to |
| 81 | // succeeed. |
| 82 | bool retrying_request_ = false; |
| 83 | |
| 84 | // Stores the most recently requested token, in case the validation request |
| 85 | // needs to be retried. |
| 86 | std::string token_; |
| 87 | |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 88 | base::Callback<void(const std::string& shared_secret)> on_token_validated_; |
| 89 | |
Jeremy Roman | 7c5cfabd | 2019-08-12 15:45:27 | [diff] [blame] | 90 | base::WeakPtrFactory<TokenValidatorBase> weak_factory_{this}; |
[email protected] | d95ee26 | 2014-02-26 06:30:31 | [diff] [blame] | 91 | |
| 92 | DISALLOW_COPY_AND_ASSIGN(TokenValidatorBase); |
| 93 | }; |
| 94 | |
| 95 | } // namespace remoting |
| 96 | |
| 97 | #endif // REMOTING_HOST_TOKEN_VALIDATOR_BASE_H |