blob: bb642b860f6adc324e93bfd7f6cd47f1580a9402 [file] [log] [blame]
Louis Collardf59aa942019-02-25 17:50:14 +08001// Copyright 2019 The Chromium OS 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 U2FD_USER_STATE_H_
6#define U2FD_USER_STATE_H_
7
8#include <string>
9#include <vector>
10
11#include <base/files/file_util.h>
12#include <base/optional.h>
13#include <brillo/secure_blob.h>
14#include <dbus/bus.h>
15#include <session_manager/dbus-proxies.h>
16
17namespace u2f {
18
19// Encapsulates access to user-specific U2F state. This class is not
20// thread-safe.
21class UserState {
22 public:
Louis Collard243acd62019-04-25 15:42:57 +080023 // Constructs a new UserState object using the specified dbus object.
24 // The counter values returned by this object will be >= counter_min.
Ben Chanb50bc5d2019-09-19 12:07:32 -070025 UserState(org::chromium::SessionManagerInterfaceProxy* sm_proxy,
26 uint32_t counter_min);
Louis Collardf59aa942019-02-25 17:50:14 +080027
Louis Collardaef4b482019-08-30 14:57:20 +080028 virtual ~UserState() = default;
29
Louis Collardf59aa942019-02-25 17:50:14 +080030 // Get*() methods return base::nullopt if user state is currently
31 // unavailable.
32
33 // Get the user secret.
Louis Collardaef4b482019-08-30 14:57:20 +080034 virtual base::Optional<brillo::SecureBlob> GetUserSecret();
Louis Collardf59aa942019-02-25 17:50:14 +080035
Louis Collarda98adda2019-08-01 17:08:29 +080036 // Returns the current counter value. The returned value must not be
37 // returned externally until the counter has succesfully been
38 // incremented (and persisted to disk).
Louis Collardaef4b482019-08-30 14:57:20 +080039 virtual base::Optional<std::vector<uint8_t>> GetCounter();
Louis Collardf59aa942019-02-25 17:50:14 +080040
Louis Collarda98adda2019-08-01 17:08:29 +080041 // Increments the counter value, which is subsequently immediately
42 // flushed to disk. Returns true on success, false if the counter
43 // could not be persisted to disk.
Louis Collardaef4b482019-08-30 14:57:20 +080044 virtual bool IncrementCounter();
45
Yicheng Li30b6abc2020-11-13 14:51:15 -080046 // Sets a callback that is invoked when a primary session started, with the
47 // username.
Yicheng Li9e902962020-11-01 11:07:11 -080048 virtual void SetSessionStartedCallback(
49 base::RepeatingCallback<void(const std::string&)> callback);
Yicheng Li30b6abc2020-11-13 14:51:15 -080050 // Sets a callback that is invoked when the user session stopped.
Yicheng Li9e902962020-11-01 11:07:11 -080051 virtual void SetSessionStoppedCallback(
52 base::RepeatingCallback<void()> callback);
53
Yicheng Li30b6abc2020-11-13 14:51:15 -080054 // Returns if there is a known primary session username.
55 virtual bool HasUser();
Yicheng Li67abd182020-11-18 15:31:41 -080056 // Returns the known primary session username.
57 virtual base::Optional<std::string> GetUser();
58 // Returns the sanitized username.
Yicheng Li1090c902020-11-10 11:31:43 -080059 virtual base::Optional<std::string> GetSanitizedUser();
60
Louis Collardaef4b482019-08-30 14:57:20 +080061 protected:
62 // Constructor for use by mock objects.
63 UserState();
Louis Collarda98adda2019-08-01 17:08:29 +080064
Louis Collardf59aa942019-02-25 17:50:14 +080065 private:
66 // Handler for the SessionStateChanged signal.
67 void OnSessionStateChanged(const std::string& state);
68
69 // Fetches the sanitized username for the primary session.
70 void UpdatePrimarySessionSanitizedUser();
71
72 // Attempts to load state for the current primary session.
73 void LoadState();
74
75 // The following methods assume that a sanitized_user_ is currently available.
76
77 // Loads an existing user secret if available, or creates a new secret if not.
78 void LoadOrCreateUserSecret();
79
80 // Loads an existing user secret from disk.
81 void LoadUserSecret(const base::FilePath& path);
82
83 // Creates a new user secret and stores it on disk, blocks until the secret
84 // has been flushed to disk.
85 void CreateUserSecret(const base::FilePath& path);
86
87 // Loads the counter from disk.
88 void LoadCounter();
89
90 // Persists the counter to disk, blocks until the counter has been flushed to
91 // disk.
92 bool PersistCounter();
93
Yicheng Li9e902962020-11-01 11:07:11 -080094 // Current username, if any.
95 base::Optional<std::string> user_;
96
Louis Collardf59aa942019-02-25 17:50:14 +080097 // Current sanitized username, if any.
98 base::Optional<std::string> sanitized_user_;
99
100 base::Optional<brillo::SecureBlob> user_secret_;
101 base::Optional<uint32_t> counter_;
102
Louis Collardd0c23612019-05-24 10:04:48 +0800103 org::chromium::SessionManagerInterfaceProxy* sm_proxy_;
Louis Collardf59aa942019-02-25 17:50:14 +0800104 base::WeakPtrFactory<UserState> weak_ptr_factory_;
Louis Collard243acd62019-04-25 15:42:57 +0800105
Yicheng Li9e902962020-11-01 11:07:11 -0800106 base::RepeatingCallback<void(const std::string&)> session_started_callback_;
107 base::RepeatingCallback<void()> session_stopped_callback_;
108
Louis Collard243acd62019-04-25 15:42:57 +0800109 const uint32_t counter_min_;
Louis Collardf59aa942019-02-25 17:50:14 +0800110};
111
112} // namespace u2f
113
114#endif // U2FD_USER_STATE_H_