igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 1 | // Copyright 2016 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 | #include "chrome/browser/chromeos/certificate_provider/pin_dialog_manager.h" |
| 6 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 7 | #include "base/bind.h" |
| 8 | #include "base/logging.h" |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 9 | |
| 10 | namespace chromeos { |
| 11 | |
| 12 | // Define timeout for issued sign_request_id. |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 13 | constexpr base::TimeDelta kSignRequestIdTimeout = |
| 14 | base::TimeDelta::FromMinutes(10); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 15 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 16 | PinDialogManager::PinDialogManager() = default; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 17 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 18 | PinDialogManager::~PinDialogManager() = default; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 19 | |
| 20 | void PinDialogManager::AddSignRequestId(const std::string& extension_id, |
| 21 | int sign_request_id) { |
| 22 | ExtensionNameRequestIdPair key(extension_id, sign_request_id); |
| 23 | // Cache the ID with current timestamp. |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 24 | sign_request_times_[key] = base::Time::Now(); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 25 | } |
| 26 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 27 | PinDialogManager::RequestPinResult PinDialogManager::RequestPin( |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 28 | const std::string& extension_id, |
| 29 | const std::string& extension_name, |
| 30 | int sign_request_id, |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 31 | PinCodeType code_type, |
| 32 | PinErrorLabel error_label, |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 33 | int attempts_left, |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 34 | RequestPinCallback callback) { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 35 | DCHECK_GE(attempts_left, -1); |
| 36 | const bool accept_input = (attempts_left != 0); |
| 37 | |
| 38 | // Start from sanity checks, as the extension might have issued this call |
| 39 | // incorrectly. |
| 40 | if (active_dialog_state_) { |
| 41 | // The active dialog exists already, so we need to make sure it belongs to |
| 42 | // the same extension and the user submitted some input. |
| 43 | if (extension_id != active_dialog_state_->extension_id) |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 44 | return RequestPinResult::kOtherFlowInProgress; |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 45 | if (active_dialog_state_->request_pin_callback || |
| 46 | active_dialog_state_->stop_pin_request_callback) { |
| 47 | // Extension requests a PIN without having received any input from its |
| 48 | // previous request. Reject the new request. |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 49 | return RequestPinResult::kDialogDisplayedAlready; |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 50 | } |
| 51 | } else { |
| 52 | // Check the validity of sign_request_id |
| 53 | const ExtensionNameRequestIdPair key(extension_id, sign_request_id); |
| 54 | if (sign_request_times_.find(key) == sign_request_times_.end()) |
| 55 | return RequestPinResult::kInvalidId; |
| 56 | const base::Time current_time = base::Time::Now(); |
| 57 | if (current_time - sign_request_times_[key] > kSignRequestIdTimeout) |
| 58 | return RequestPinResult::kInvalidId; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 59 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 60 | // A new dialog will be opened, so initialize the related internal state. |
| 61 | active_dialog_state_.emplace(&default_dialog_host_, extension_id, |
| 62 | extension_name, code_type); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 63 | } |
| 64 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 65 | active_dialog_state_->request_pin_callback = std::move(callback); |
| 66 | active_dialog_state_->host->ShowSecurityTokenPinDialog( |
| 67 | extension_name, code_type, accept_input, error_label, attempts_left, |
| 68 | base::BindOnce(&PinDialogManager::OnPinEntered, |
| 69 | weak_factory_.GetWeakPtr()), |
| 70 | base::BindOnce(&PinDialogManager::OnPinDialogClosed, |
| 71 | weak_factory_.GetWeakPtr())); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 72 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 73 | return RequestPinResult::kSuccess; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 74 | } |
| 75 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 76 | PinDialogManager::StopPinRequestResult |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 77 | PinDialogManager::StopPinRequestWithError(const std::string& extension_id, |
| 78 | PinErrorLabel error_label, |
| 79 | StopPinRequestCallback callback) { |
| 80 | DCHECK_NE(error_label, PinErrorLabel::kNone); |
| 81 | |
| 82 | // Perform sanity checks, as the extension might have issued this call |
| 83 | // incorrectly. |
| 84 | if (!active_dialog_state_ || |
| 85 | active_dialog_state_->extension_id != extension_id) { |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 86 | return StopPinRequestResult::kNoActiveDialog; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 87 | } |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 88 | if (active_dialog_state_->request_pin_callback || |
| 89 | active_dialog_state_->stop_pin_request_callback) { |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 90 | return StopPinRequestResult::kNoUserInput; |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 91 | } |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 92 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 93 | active_dialog_state_->stop_pin_request_callback = std::move(callback); |
| 94 | active_dialog_state_->host->ShowSecurityTokenPinDialog( |
| 95 | active_dialog_state_->extension_name, active_dialog_state_->code_type, |
| 96 | /*enable_user_input=*/false, error_label, |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 97 | /*attempts_left=*/-1, |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 98 | base::BindOnce(&PinDialogManager::OnPinEntered, |
| 99 | weak_factory_.GetWeakPtr()), |
| 100 | base::BindOnce(&PinDialogManager::OnPinDialogClosed, |
| 101 | weak_factory_.GetWeakPtr())); |
| 102 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 103 | return StopPinRequestResult::kSuccess; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 104 | } |
| 105 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 106 | bool PinDialogManager::LastPinDialogClosed( |
| 107 | const std::string& extension_id) const { |
| 108 | auto iter = last_response_closed_.find(extension_id); |
| 109 | return iter != last_response_closed_.end() && iter->second; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | bool PinDialogManager::CloseDialog(const std::string& extension_id) { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 113 | // Perform sanity checks, as the extension might have issued this call |
| 114 | // incorrectly. |
| 115 | if (!active_dialog_state_ || |
| 116 | extension_id != active_dialog_state_->extension_id) { |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 117 | LOG(ERROR) << "StopPinRequest called by unexpected extension: " |
| 118 | << extension_id; |
| 119 | return false; |
| 120 | } |
| 121 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 122 | active_dialog_state_->host->CloseSecurityTokenPinDialog(); |
| 123 | |
| 124 | // The active dialog state should have been cleared by OnPinDialogClosed(). |
| 125 | DCHECK(!active_dialog_state_); |
| 126 | |
| 127 | last_response_closed_[extension_id] = true; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 128 | return true; |
| 129 | } |
| 130 | |
| 131 | void PinDialogManager::ExtensionUnloaded(const std::string& extension_id) { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 132 | if (active_dialog_state_ && |
| 133 | active_dialog_state_->extension_id == extension_id) { |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 134 | CloseDialog(extension_id); |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 135 | } |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 136 | |
| 137 | last_response_closed_[extension_id] = false; |
| 138 | |
| 139 | for (auto it = sign_request_times_.cbegin(); |
| 140 | it != sign_request_times_.cend();) { |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 141 | if (it->first.first == extension_id) |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 142 | sign_request_times_.erase(it++); |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 143 | else |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 144 | ++it; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 148 | PinDialogManager::ActiveDialogState::ActiveDialogState( |
| 149 | SecurityTokenPinDialogHost* host, |
| 150 | const std::string& extension_id, |
| 151 | const std::string& extension_name, |
| 152 | PinCodeType code_type) |
| 153 | : host(host), |
| 154 | extension_id(extension_id), |
| 155 | extension_name(extension_name), |
| 156 | code_type(code_type) {} |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 157 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 158 | PinDialogManager::ActiveDialogState::~ActiveDialogState() = default; |
| 159 | |
| 160 | void PinDialogManager::OnPinEntered(const std::string& user_input) { |
| 161 | DCHECK(!active_dialog_state_->stop_pin_request_callback); |
| 162 | last_response_closed_[active_dialog_state_->extension_id] = false; |
| 163 | if (active_dialog_state_->request_pin_callback) |
| 164 | std::move(active_dialog_state_->request_pin_callback).Run(user_input); |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | void PinDialogManager::OnPinDialogClosed() { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 168 | DCHECK(!active_dialog_state_->request_pin_callback || |
| 169 | !active_dialog_state_->stop_pin_request_callback); |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 170 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame^] | 171 | last_response_closed_[active_dialog_state_->extension_id] = true; |
| 172 | if (active_dialog_state_->request_pin_callback) { |
| 173 | std::move(active_dialog_state_->request_pin_callback) |
| 174 | .Run(/*user_input=*/std::string()); |
| 175 | } |
| 176 | if (active_dialog_state_->stop_pin_request_callback) |
| 177 | std::move(active_dialog_state_->stop_pin_request_callback).Run(); |
| 178 | active_dialog_state_.reset(); |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 179 | } |
| 180 | |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 181 | } // namespace chromeos |