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" |
Maksim Ivanov | 5fd57903 | 2019-08-02 01:15:15 | [diff] [blame] | 9 | #include "base/stl_util.h" |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 10 | |
| 11 | namespace chromeos { |
| 12 | |
| 13 | // Define timeout for issued sign_request_id. |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 14 | constexpr base::TimeDelta kSignRequestIdTimeout = |
| 15 | base::TimeDelta::FromMinutes(10); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 16 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 17 | PinDialogManager::PinDialogManager() = default; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 18 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 19 | PinDialogManager::~PinDialogManager() = default; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 20 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 21 | void PinDialogManager::AddSignRequestId( |
| 22 | const std::string& extension_id, |
| 23 | int sign_request_id, |
| 24 | const base::Optional<AccountId>& authenticating_user_account_id) { |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 25 | ExtensionNameRequestIdPair key(extension_id, sign_request_id); |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 26 | sign_requests_.insert( |
| 27 | std::make_pair(key, SignRequestState(/*begin_time=*/base::Time::Now(), |
| 28 | authenticating_user_account_id))); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 29 | } |
| 30 | |
Maksim Ivanov | 7652d0d | 2019-09-04 19:12:19 | [diff] [blame] | 31 | void PinDialogManager::AbortSignRequest(const std::string& extension_id, |
| 32 | int sign_request_id) { |
| 33 | if (active_dialog_state_ && |
| 34 | active_dialog_state_->extension_id == extension_id && |
| 35 | active_dialog_state_->sign_request_id == sign_request_id) { |
| 36 | CloseActiveDialog(); |
| 37 | } |
| 38 | |
| 39 | ExtensionNameRequestIdPair key(extension_id, sign_request_id); |
| 40 | sign_requests_.erase(key); |
| 41 | } |
| 42 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 43 | PinDialogManager::RequestPinResult PinDialogManager::RequestPin( |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 44 | const std::string& extension_id, |
| 45 | const std::string& extension_name, |
| 46 | int sign_request_id, |
Fabian Sommer | 195d1f3 | 2020-02-27 01:51:02 | [diff] [blame^] | 47 | security_token_pin::CodeType code_type, |
| 48 | security_token_pin::ErrorLabel error_label, |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 49 | int attempts_left, |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 50 | RequestPinCallback callback) { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 51 | DCHECK_GE(attempts_left, -1); |
| 52 | const bool accept_input = (attempts_left != 0); |
| 53 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 54 | // Check the validity of sign_request_id. |
| 55 | const SignRequestState* const sign_request_state = |
| 56 | FindSignRequestState(extension_id, sign_request_id); |
| 57 | if (!sign_request_state) |
| 58 | return RequestPinResult::kInvalidId; |
| 59 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 60 | // Start from sanity checks, as the extension might have issued this call |
| 61 | // incorrectly. |
| 62 | if (active_dialog_state_) { |
| 63 | // The active dialog exists already, so we need to make sure it belongs to |
| 64 | // the same extension and the user submitted some input. |
| 65 | if (extension_id != active_dialog_state_->extension_id) |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 66 | return RequestPinResult::kOtherFlowInProgress; |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 67 | if (active_dialog_state_->request_pin_callback || |
| 68 | active_dialog_state_->stop_pin_request_callback) { |
| 69 | // Extension requests a PIN without having received any input from its |
| 70 | // previous request. Reject the new request. |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 71 | return RequestPinResult::kDialogDisplayedAlready; |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 72 | } |
| 73 | } else { |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 74 | // Check that the sign request hasn't timed out yet. |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 75 | const base::Time current_time = base::Time::Now(); |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 76 | if (current_time - sign_request_state->begin_time > kSignRequestIdTimeout) |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 77 | return RequestPinResult::kInvalidId; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 78 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 79 | // A new dialog will be opened, so initialize the related internal state. |
Maksim Ivanov | 5fd57903 | 2019-08-02 01:15:15 | [diff] [blame] | 80 | active_dialog_state_.emplace(GetHostForNewDialog(), extension_id, |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 81 | extension_name, sign_request_id, code_type); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 82 | } |
| 83 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 84 | active_dialog_state_->request_pin_callback = std::move(callback); |
| 85 | active_dialog_state_->host->ShowSecurityTokenPinDialog( |
| 86 | extension_name, code_type, accept_input, error_label, attempts_left, |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 87 | sign_request_state->authenticating_user_account_id, |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 88 | base::BindOnce(&PinDialogManager::OnPinEntered, |
| 89 | weak_factory_.GetWeakPtr()), |
| 90 | base::BindOnce(&PinDialogManager::OnPinDialogClosed, |
| 91 | weak_factory_.GetWeakPtr())); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 92 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 93 | return RequestPinResult::kSuccess; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 94 | } |
| 95 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 96 | PinDialogManager::StopPinRequestResult |
Maksim Ivanov | b2985b1 | 2019-08-08 15:23:58 | [diff] [blame] | 97 | PinDialogManager::StopPinRequestWithError( |
| 98 | const std::string& extension_id, |
Fabian Sommer | 195d1f3 | 2020-02-27 01:51:02 | [diff] [blame^] | 99 | security_token_pin::ErrorLabel error_label, |
Maksim Ivanov | b2985b1 | 2019-08-08 15:23:58 | [diff] [blame] | 100 | StopPinRequestCallback callback) { |
Fabian Sommer | 195d1f3 | 2020-02-27 01:51:02 | [diff] [blame^] | 101 | DCHECK_NE(error_label, security_token_pin::ErrorLabel::kNone); |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 102 | |
| 103 | // Perform sanity checks, as the extension might have issued this call |
| 104 | // incorrectly. |
| 105 | if (!active_dialog_state_ || |
| 106 | active_dialog_state_->extension_id != extension_id) { |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 107 | return StopPinRequestResult::kNoActiveDialog; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 108 | } |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 109 | if (active_dialog_state_->request_pin_callback || |
| 110 | active_dialog_state_->stop_pin_request_callback) { |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 111 | return StopPinRequestResult::kNoUserInput; |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 112 | } |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 113 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 114 | const SignRequestState* const sign_request_state = |
| 115 | FindSignRequestState(extension_id, active_dialog_state_->sign_request_id); |
| 116 | if (!sign_request_state) |
| 117 | return StopPinRequestResult::kNoActiveDialog; |
| 118 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 119 | active_dialog_state_->stop_pin_request_callback = std::move(callback); |
| 120 | active_dialog_state_->host->ShowSecurityTokenPinDialog( |
| 121 | active_dialog_state_->extension_name, active_dialog_state_->code_type, |
| 122 | /*enable_user_input=*/false, error_label, |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 123 | /*attempts_left=*/-1, sign_request_state->authenticating_user_account_id, |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 124 | base::BindOnce(&PinDialogManager::OnPinEntered, |
| 125 | weak_factory_.GetWeakPtr()), |
| 126 | base::BindOnce(&PinDialogManager::OnPinDialogClosed, |
| 127 | weak_factory_.GetWeakPtr())); |
| 128 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 129 | return StopPinRequestResult::kSuccess; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 130 | } |
| 131 | |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 132 | bool PinDialogManager::LastPinDialogClosed( |
| 133 | const std::string& extension_id) const { |
| 134 | auto iter = last_response_closed_.find(extension_id); |
| 135 | return iter != last_response_closed_.end() && iter->second; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | bool PinDialogManager::CloseDialog(const std::string& extension_id) { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 139 | // Perform sanity checks, as the extension might have issued this call |
| 140 | // incorrectly. |
| 141 | if (!active_dialog_state_ || |
| 142 | extension_id != active_dialog_state_->extension_id) { |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 143 | LOG(ERROR) << "StopPinRequest called by unexpected extension: " |
| 144 | << extension_id; |
| 145 | return false; |
| 146 | } |
| 147 | |
Maksim Ivanov | 5ccb96c | 2019-08-08 13:25:41 | [diff] [blame] | 148 | CloseActiveDialog(); |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | |
| 152 | void PinDialogManager::ExtensionUnloaded(const std::string& extension_id) { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 153 | if (active_dialog_state_ && |
| 154 | active_dialog_state_->extension_id == extension_id) { |
Maksim Ivanov | 5ccb96c | 2019-08-08 13:25:41 | [diff] [blame] | 155 | CloseActiveDialog(); |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 156 | } |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 157 | |
| 158 | last_response_closed_[extension_id] = false; |
| 159 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 160 | for (auto it = sign_requests_.cbegin(); it != sign_requests_.cend();) { |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 161 | if (it->first.first == extension_id) |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 162 | sign_requests_.erase(it++); |
Maksim Ivanov | 86866b7 | 2019-07-25 02:15:06 | [diff] [blame] | 163 | else |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 164 | ++it; |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
Maksim Ivanov | 5fd57903 | 2019-08-02 01:15:15 | [diff] [blame] | 168 | void PinDialogManager::AddPinDialogHost( |
| 169 | SecurityTokenPinDialogHost* pin_dialog_host) { |
| 170 | DCHECK(!base::Contains(added_dialog_hosts_, pin_dialog_host)); |
| 171 | added_dialog_hosts_.push_back(pin_dialog_host); |
| 172 | } |
| 173 | |
| 174 | void PinDialogManager::RemovePinDialogHost( |
| 175 | SecurityTokenPinDialogHost* pin_dialog_host) { |
Maksim Ivanov | 5ccb96c | 2019-08-08 13:25:41 | [diff] [blame] | 176 | if (active_dialog_state_ && active_dialog_state_->host == pin_dialog_host) |
| 177 | CloseActiveDialog(); |
Maksim Ivanov | 5fd57903 | 2019-08-02 01:15:15 | [diff] [blame] | 178 | DCHECK(base::Contains(added_dialog_hosts_, pin_dialog_host)); |
| 179 | base::Erase(added_dialog_hosts_, pin_dialog_host); |
| 180 | } |
| 181 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 182 | PinDialogManager::SignRequestState::SignRequestState( |
| 183 | base::Time begin_time, |
| 184 | const base::Optional<AccountId>& authenticating_user_account_id) |
| 185 | : begin_time(begin_time), |
| 186 | authenticating_user_account_id(authenticating_user_account_id) {} |
| 187 | |
| 188 | PinDialogManager::SignRequestState::SignRequestState(const SignRequestState&) = |
| 189 | default; |
| 190 | PinDialogManager::SignRequestState& PinDialogManager::SignRequestState:: |
| 191 | operator=(const SignRequestState&) = default; |
| 192 | |
| 193 | PinDialogManager::SignRequestState::~SignRequestState() = default; |
| 194 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 195 | PinDialogManager::ActiveDialogState::ActiveDialogState( |
| 196 | SecurityTokenPinDialogHost* host, |
| 197 | const std::string& extension_id, |
| 198 | const std::string& extension_name, |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 199 | int sign_request_id, |
Fabian Sommer | 195d1f3 | 2020-02-27 01:51:02 | [diff] [blame^] | 200 | security_token_pin::CodeType code_type) |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 201 | : host(host), |
| 202 | extension_id(extension_id), |
| 203 | extension_name(extension_name), |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 204 | sign_request_id(sign_request_id), |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 205 | code_type(code_type) {} |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 206 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 207 | PinDialogManager::ActiveDialogState::~ActiveDialogState() = default; |
| 208 | |
Maksim Ivanov | 2b8136f | 2019-08-09 14:03:59 | [diff] [blame] | 209 | PinDialogManager::SignRequestState* PinDialogManager::FindSignRequestState( |
| 210 | const std::string& extension_id, |
| 211 | int sign_request_id) { |
| 212 | const ExtensionNameRequestIdPair key(extension_id, sign_request_id); |
| 213 | const auto sign_request_iter = sign_requests_.find(key); |
| 214 | if (sign_request_iter == sign_requests_.end()) |
| 215 | return nullptr; |
| 216 | return &sign_request_iter->second; |
| 217 | } |
| 218 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 219 | void PinDialogManager::OnPinEntered(const std::string& user_input) { |
| 220 | DCHECK(!active_dialog_state_->stop_pin_request_callback); |
| 221 | last_response_closed_[active_dialog_state_->extension_id] = false; |
| 222 | if (active_dialog_state_->request_pin_callback) |
| 223 | std::move(active_dialog_state_->request_pin_callback).Run(user_input); |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | void PinDialogManager::OnPinDialogClosed() { |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 227 | DCHECK(!active_dialog_state_->request_pin_callback || |
| 228 | !active_dialog_state_->stop_pin_request_callback); |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 229 | |
Maksim Ivanov | 73dfec3 | 2019-08-01 22:43:12 | [diff] [blame] | 230 | last_response_closed_[active_dialog_state_->extension_id] = true; |
| 231 | if (active_dialog_state_->request_pin_callback) { |
| 232 | std::move(active_dialog_state_->request_pin_callback) |
| 233 | .Run(/*user_input=*/std::string()); |
| 234 | } |
| 235 | if (active_dialog_state_->stop_pin_request_callback) |
| 236 | std::move(active_dialog_state_->stop_pin_request_callback).Run(); |
| 237 | active_dialog_state_.reset(); |
Maksim Ivanov | ab8e858 | 2019-08-01 21:49:13 | [diff] [blame] | 238 | } |
| 239 | |
Maksim Ivanov | 5fd57903 | 2019-08-02 01:15:15 | [diff] [blame] | 240 | SecurityTokenPinDialogHost* PinDialogManager::GetHostForNewDialog() { |
| 241 | if (added_dialog_hosts_.empty()) |
| 242 | return &default_dialog_host_; |
| 243 | return added_dialog_hosts_.back(); |
| 244 | } |
| 245 | |
Maksim Ivanov | 5ccb96c | 2019-08-08 13:25:41 | [diff] [blame] | 246 | void PinDialogManager::CloseActiveDialog() { |
| 247 | if (!active_dialog_state_) |
| 248 | return; |
| 249 | |
| 250 | // Ignore any further callbacks from the host. Instead of relying on the host |
| 251 | // to call the closing callback, run OnPinDialogClosed() below explicitly. |
| 252 | weak_factory_.InvalidateWeakPtrs(); |
| 253 | |
| 254 | active_dialog_state_->host->CloseSecurityTokenPinDialog(); |
| 255 | OnPinDialogClosed(); |
| 256 | DCHECK(!active_dialog_state_); |
| 257 | } |
| 258 | |
igorcov | bb898fb | 2016-12-01 16:07:56 | [diff] [blame] | 259 | } // namespace chromeos |