File: cryptohome_based_engine.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 (98 lines) | stat: -rw-r--r-- 3,345 bytes parent folder | download | duplicates (6)
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
// 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/engines/cryptohome_based_engine.h"

#include <utility>

#include "base/check.h"
#include "base/logging.h"
#include "chromeos/ash/components/login/auth/public/user_context.h"
#include "chromeos/ash/components/osauth/public/common_types.h"
#include "chromeos/ash/components/osauth/public/cryptohome_core.h"

namespace ash {

CryptohomeBasedEngine::CryptohomeBasedEngine(CryptohomeCore& core,
                                             AshAuthFactor factor)
    : core_(&core), factor_(factor) {}

CryptohomeBasedEngine::~CryptohomeBasedEngine() = default;

AshAuthFactor CryptohomeBasedEngine::GetFactor() const {
  return factor_;
}

void CryptohomeBasedEngine::InitializeCommon(CommonInitCallback callback) {
  core_->WaitForService(
      base::BindOnce(&CryptohomeBasedEngine::OnCryptohomeReady,
                     weak_factory_.GetWeakPtr(), std::move(callback)));
}

void CryptohomeBasedEngine::OnCryptohomeReady(CommonInitCallback callback,
                                              bool service_available) {
  if (!service_available) {
    LOG(ERROR) << "cryptohomed not started, Factor "
               << static_cast<int>(GetFactor()) << " is not available";
    return;
  }
  std::move(callback).Run(GetFactor());
}

void CryptohomeBasedEngine::ShutdownCommon(ShutdownCallback callback) {
  std::move(callback).Run(GetFactor());
}

void CryptohomeBasedEngine::StartAuthFlow(const AccountId& account,
                                          AuthPurpose purpose,
                                          FactorEngineObserver* observer) {
  observer_ = observer;
  usage_allowed_ = UsageAllowed::kDisabled;
  core_->StartAuthSession({account, purpose}, this);
}

void CryptohomeBasedEngine::OnCryptohomeAuthSessionStarted() {
  key_ref_ = LookUpFactor(*core_->GetCurrentContext());
  observer_->OnFactorPresenceChecked(GetFactor(), key_ref_.has_value());
}

void CryptohomeBasedEngine::OnAuthSessionStartFailure() {
  observer_->OnCriticalError(GetFactor());
}

void CryptohomeBasedEngine::UpdateObserver(FactorEngineObserver* observer) {
  observer_ = observer;
}

void CryptohomeBasedEngine::CleanUp(CleanupCallback callback) {
  // By default, the cleanup phase is no-op because the majority
  // of the auth factors do not need to do anything for cleaning up.
  // Simply run the callback with the factor type to indicate
  // the end of clean-up.
  std::move(callback).Run(GetFactor());
}

void CryptohomeBasedEngine::StopAuthFlow(ShutdownCallback callback) {
  CHECK(shutdown_callback_.is_null());
  shutdown_callback_ = std::move(callback);
  observer_ = nullptr;
  // Note: With FakeUserDataAuthClient next call might result in
  // shutdown callback being called synchronously (and `this` being deleted).
  core_->EndAuthSession(this);
}

AuthProofToken CryptohomeBasedEngine::StoreAuthenticationContext() {
  return core_->StoreAuthenticationContext();
}

void CryptohomeBasedEngine::OnCryptohomeAuthSessionFinished() {
  CHECK(!shutdown_callback_.is_null());
  std::move(shutdown_callback_).Run(GetFactor());
}

void CryptohomeBasedEngine::SetUsageAllowed(UsageAllowed usage) {
  usage_allowed_ = usage;
}

}  // namespace ash