File: auth_hub_attempt_handler.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (325 lines) | stat: -rw-r--r-- 11,266 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/ash/components/osauth/impl/auth_hub_attempt_handler.h"

#include <optional>
#include <utility>

#include "base/check.h"
#include "base/containers/enum_set.h"
#include "base/functional/callback_forward.h"
#include "base/memory/raw_ptr.h"
#include "chromeos/ash/components/osauth/impl/auth_hub_common.h"
#include "chromeos/ash/components/osauth/public/auth_factor_engine.h"
#include "chromeos/ash/components/osauth/public/auth_factor_status_consumer.h"
#include "chromeos/ash/components/osauth/public/common_types.h"

namespace ash {

AuthHubAttemptHandler::AuthHubAttemptHandler(
    AuthHubAttemptHandler::Owner* owner,
    const AuthAttemptVector& attempt,
    const AuthEnginesMap& engines,
    AuthFactorsSet expected_factors)
    : owner_(owner),
      attempt_(attempt),
      engines_(engines),
      initial_factors_(expected_factors) {}

AuthHubAttemptHandler::~AuthHubAttemptHandler() = default;

AuthHubAttemptHandler::Owner::~Owner() = default;

AuthHubConnector* AuthHubAttemptHandler::GetConnector() {
  return this;
}

void AuthHubAttemptHandler::SetConsumer(
    raw_ptr<AuthFactorStatusConsumer> consumer) {
  status_consumer_ = std::move(consumer);
  status_consumer_->InitializeUi(initial_factors_, this);
}

bool AuthHubAttemptHandler::HasOngoingAttempt() const {
  // It is safe to proceed with shutdown if we have
  // successfully authenticated.
  if (authenticated_) {
    return false;
  }
  return ongoing_attempt_factor_.has_value();
}

void AuthHubAttemptHandler::PrepareForShutdown(base::OnceClosure callback) {
  CHECK(!callback.is_null());
  if (shutting_down_) {
    shutdown_callbacks_.AddUnsafe(std::move(callback));
    return;
  }
  if (!ongoing_attempt_factor_.has_value() || authenticated_) {
    shutting_down_ = true;
    UpdateAllFactorStates();
    std::move(callback).Run();
    return;
  }
  shutting_down_ = true;
  shutdown_callbacks_.AddUnsafe(std::move(callback));
}

void AuthHubAttemptHandler::OnFactorsChecked(AuthFactorsSet available_factors,
                                             AuthFactorsSet failed_factors) {
  DCHECK(Intersection(available_factors, failed_factors).empty());
  // TODO(b/286814076): Refine this strategy.
  // All factors can be split into 4 groups, according to initial_factors_ and
  // two parameters passed:
  //  * New factors, that are in `available_factors` but were not listed in
  //  `initial_factors_`;
  //  * Available factors, that present both  in `available_factors` and
  //  `initial_factors_`;
  //  * Removed factors, that were listed in `initial_factors_` but do not
  //  present neither in `available_factors` nor in `failed_factors`;
  //  * Failed factors;

  AuthFactorsSet potentially_present = Union(available_factors, failed_factors);
  AuthFactorsSet new_factors = Difference(available_factors, initial_factors_);
  AuthFactorsSet removed_factors =
      Difference(initial_factors_, potentially_present);

  // If some factor engines failed, but there are no new/removed factors,
  // report those factors in error state. Otherwise, consider them removed.

  AuthFactorsSet failed_initial =
      Intersection(initial_factors_, failed_factors);
  bool same_factor_list = new_factors.empty() && removed_factors.empty();

  if (same_factor_list) {
    for (AshAuthFactor f : failed_initial) {
      // Create entry, but mark as failed.
      factor_state_[f].engine_failed = true;
      CalculateFactorState(f, factor_state_[f]);
    }
  }

  // Retain only necessary engines, fill status for them:
  for (auto it = engines_.begin(); it != engines_.end();) {
    if (!available_factors.Has(it->first)) {
      it = engines_.erase(it);
      continue;
    }
    FillAllStatusValues(it->first, factor_state_[it->first]);
    it++;
  }

  FactorsStatusMap update;
  for (auto& state : factor_state_) {
    update[state.first] = state.second.internal_state;
    state.second.reported_state = state.second.internal_state;
  }

  if (same_factor_list) {
    status_consumer_->OnFactorStatusesChanged(update);
  } else {
    owner_->UpdateFactorUiCache(attempt_, available_factors);
    status_consumer_->OnFactorListChanged(update);
  }
  PropagateEnginesEnabledStatus();
}

void AuthHubAttemptHandler::PropagateStatusUpdates() {
  FactorsStatusMap update;
  for (auto& state : factor_state_) {
    if (state.second.internal_state != state.second.reported_state) {
      update[state.first] = state.second.internal_state;
      state.second.reported_state = state.second.internal_state;
    }
  }
  if (!update.empty()) {
    status_consumer_->OnFactorStatusesChanged(update);
  }
  PropagateEnginesEnabledStatus();
}

void AuthHubAttemptHandler::PropagateEnginesEnabledStatus() {
  for (auto& state : factor_state_) {
    if (state.second.intended_usage != state.second.engine_usage) {
      state.second.engine_usage = state.second.intended_usage;
      engines_[state.first]->SetUsageAllowed(state.second.engine_usage);
    }
  }
}

void AuthHubAttemptHandler::OnFactorPresenceChecked(AshAuthFactor factor,
                                                    bool factor_present) {
  // No-op, this method is implemented and handled by AuthHubVectorLifecycle.
  // Result would be provided to this class via `OnFactorsChecked` call.
}

void AuthHubAttemptHandler::OnFactorAttempt(AshAuthFactor factor) {
  ongoing_attempt_factor_ = factor;
  UpdateAllFactorStates();
}

void AuthHubAttemptHandler::UpdateAllFactorStates() {
  for (auto& state : factor_state_) {
    CalculateFactorState(state.first, state.second);
  }
  PropagateStatusUpdates();
}

void AuthHubAttemptHandler::OnFactorAttemptResult(AshAuthFactor factor,
                                                  bool success) {
  CHECK(ongoing_attempt_factor_.has_value());
  CHECK(factor == *ongoing_attempt_factor_);

  if (shutting_down_) {
    shutdown_callbacks_.Notify();
    return;
  }

  if (success) {
    status_consumer_->OnFactorAuthSuccess(factor);
    authenticated_ = true;
    // Keep an `ongoing_attempt_factor_` to prevent
    // factors from being re-enabled.
    status_consumer_->OnEndAuthentication();

    // Calling `OnEndAuthentication` signals the end of interaction with UI for
    // this particular attempt, which would eventually destroy UI, so we reset
    // the pointer here to avoid calling into a danling pointer.
    status_consumer_ = nullptr;

    // Signal the successful auth to every auth engine.
    for (const auto& [unused, engine] : engines_) {
      engine->OnSuccessfulAuthentiation();
    }

    owner_->OnAuthenticationSuccess(attempt_, factor);
    return;
  } else {
    status_consumer_->OnFactorAuthFailure(factor);
    owner_->OnFactorAttemptFailed(attempt_, factor);
    ongoing_attempt_factor_.reset();
  }
  UpdateAllFactorStates();
}

void AuthHubAttemptHandler::OnPolicyChanged(AshAuthFactor factor) {
  CHECK(factor_state_.contains(factor));
  auto& state = factor_state_[factor];
  if (state.engine_failed) {
    return;
  }
  CHECK(engines_.contains(factor));
  auto* engine = engines_[factor].get();
  state.disabled_by_policy = engine->IsDisabledByPolicy();
  CalculateFactorState(factor, state);
  PropagateStatusUpdates();
}

void AuthHubAttemptHandler::OnLockoutChanged(AshAuthFactor factor) {
  CHECK(factor_state_.contains(factor));
  auto& state = factor_state_[factor];
  if (state.engine_failed) {
    return;
  }
  CHECK(engines_.contains(factor));
  auto* engine = engines_[factor].get();
  state.locked_out = engine->IsLockedOut();
  CalculateFactorState(factor, state);
  PropagateStatusUpdates();
}

void AuthHubAttemptHandler::OnFactorSpecificRestrictionsChanged(
    AshAuthFactor factor) {
  CHECK(factor_state_.contains(factor));
  auto& state = factor_state_[factor];
  if (state.engine_failed) {
    return;
  }
  CHECK(engines_.contains(factor));
  auto* engine = engines_[factor].get();
  state.factor_specific_restricted = engine->IsFactorSpecificRestricted();
  CalculateFactorState(factor, state);
  PropagateStatusUpdates();
}

void AuthHubAttemptHandler::OnCriticalError(AshAuthFactor factor) {
  CHECK(factor_state_.contains(factor));
  factor_state_[factor].engine_failed = true;
  CalculateFactorState(factor, factor_state_[factor]);
  PropagateStatusUpdates();
}

void AuthHubAttemptHandler::OnFactorCustomSignal(AshAuthFactor factor) {
  CHECK(engines_.contains(factor));
  status_consumer_->OnFactorCustomSignal(factor);
}

void AuthHubAttemptHandler::FillAllStatusValues(AshAuthFactor factor,
                                                FactorAttemptState& state) {
  CHECK(engines_.contains(factor));
  auto* engine = engines_[factor].get();
  state.disabled_by_policy = engine->IsDisabledByPolicy();
  state.locked_out = engine->IsLockedOut();
  state.factor_specific_restricted = engine->IsFactorSpecificRestricted();
  CalculateFactorState(factor, state);
}

void AuthHubAttemptHandler::CalculateFactorState(AshAuthFactor factor,
                                                 FactorAttemptState& state) {
  state.internal_state = AuthFactorState::kFactorReady;
  if (state.engine_failed) {
    state.internal_state = AuthFactorState::kEngineError;
    // Factor is marked as Failed if it's engine did not start.
    // We do not modify `intended_usage` here as we can not propagate
    // it to the engine.
    return;
  }
  if (shutting_down_) {
    // We need to set some disabled state here to prevent factors from
    // being used, does not matter which one.
    state.internal_state = AuthFactorState::kDisabledParallelAttempt;
    // As code is in the shutdown sequence, engine will not be re-enabled
    // again, so use kDisabled instead of kDisabledParallelAttempt here,
    // to prevent engine from queueing any attempts.
    state.intended_usage = AuthFactorEngine::UsageAllowed::kDisabled;
    return;
  }
  if (state.disabled_by_policy) {
    state.internal_state = AuthFactorState::kDisabledByPolicy;
    state.intended_usage = AuthFactorEngine::UsageAllowed::kDisabled;
    return;
  }
  if (state.factor_specific_restricted) {
    state.internal_state = AuthFactorState::kDisabledFactorSpecific;
    state.intended_usage = AuthFactorEngine::UsageAllowed::kDisabled;
    return;
  }
  if (state.locked_out) {
    state.internal_state = AuthFactorState::kLockedOutIndefinite;
    state.intended_usage = AuthFactorEngine::UsageAllowed::kDisabled;
    return;
  }
  if (ongoing_attempt_factor_.has_value()) {
    if (*ongoing_attempt_factor_ == factor) {
      state.internal_state = AuthFactorState::kOngoingAttempt;
    } else {
      state.internal_state = AuthFactorState::kDisabledParallelAttempt;
    }
    // While there is an ongoing attempt, keep all factors disabled to
    // prevent double authentication.
    state.intended_usage =
        AuthFactorEngine::UsageAllowed::kDisabledParallelAttempt;
    return;
  }
  state.internal_state = AuthFactorState::kFactorReady;
  state.intended_usage = AuthFactorEngine::UsageAllowed::kEnabled;
}

AuthFactorEngine* AuthHubAttemptHandler::GetEngine(AshAuthFactor factor) {
  CHECK(engines_.contains(factor));
  return engines_[factor];
}

}  // namespace ash