/
identity_model.dart
235 lines (199 loc) · 8.22 KB
/
identity_model.dart
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
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:safe_change_notifier/safe_change_notifier.dart';
import 'package:ubuntu_logger/ubuntu_logger.dart';
import 'package:ubuntu_provision/ubuntu_provision.dart';
import 'package:ubuntu_service/ubuntu_service.dart';
import 'package:ubuntu_utils/ubuntu_utils.dart';
import 'package:ubuntu_widgets/ubuntu_widgets.dart';
final _log = Logger('identity');
/// The regular expression pattern for valid usernames:
/// - must start with a lowercase letter
/// - may contain lowercase letters, digits, hyphens, and underscores
const kValidUsernamePattern = r'^[a-z][a-z0-9-_]*$';
/// The regular expression pattern for valid hostnames:
/// - must start and end with a letter or digit
/// - may contain letters, digits, hyphens, and dots
const kValidHostnamePattern = r'^[a-zA-Z0-9]([a-zA-Z0-9.-]*[a-zA-Z0-9])*$';
/// The maximum length for valid real names
const kMaxRealNameLength = 160;
/// The maximum length for valid hostnames
const kMaxHostnameLength = 64;
/// The maximum length for valid usernames
const kMaxUsernameLength = 32;
final identityModelProvider = ChangeNotifierProvider(
(_) => IdentityModel(
service: getService<IdentityService>(),
activeDirectory: getService<ActiveDirectoryService>(),
network: getService<NetworkService>(),
telemetry: tryGetService<TelemetryService>(),
),
);
/// [IdentityPage]'s view model.
class IdentityModel extends SafeChangeNotifier with PropertyStreamNotifier {
/// Creates the model with the given client.
IdentityModel({
required IdentityService service,
required ActiveDirectoryService activeDirectory,
required NetworkService network,
TelemetryService? telemetry,
}) : _service = service,
_activeDirectory = activeDirectory,
_network = network,
_telemetry = telemetry {
Listenable.merge([
_realName,
_hostname,
_username,
_usernameValidation,
_password,
_confirmedPassword,
_autoLogin,
_productName,
_showPassword,
_hasActiveDirectorySupport,
_useActiveDirectory,
]).addListener(notifyListeners);
addPropertyListener('State', notifyListeners);
setProperties(_network.propertiesChanged);
}
final IdentityService _service;
final ActiveDirectoryService _activeDirectory;
final NetworkService _network;
final TelemetryService? _telemetry;
final _realName = ValueNotifier<String?>(null);
final _username = ValueNotifier<String?>(null);
final _usernameValidation =
ValueNotifier<UsernameValidation>(UsernameValidation.OK);
final _hostname = ValueNotifier<String?>(null);
final _password = ValueNotifier<String?>(null);
final _confirmedPassword = ValueNotifier<String?>(null);
final _autoLogin = ValueNotifier<bool>(false);
final _productName = ValueNotifier<String>('');
final _showPassword = ValueNotifier<bool>(false);
final _hasActiveDirectorySupport = ValueNotifier<bool?>(null);
final _useActiveDirectory = ValueNotifier<bool>(false);
/// The current real name.
String get realName => _realName.value ?? '';
set realName(String value) => _realName.value = value;
/// The current hostname or generates one if not set.
String get hostname => _hostname.value ?? _generateHostname();
set hostname(String value) => _hostname.value = value;
// Generates a hostname `<username>-<system hostname>`.
String _generateHostname() {
if (username.isEmpty || _productName.value.isEmpty) return '';
return '$username-${_productName.value}'.truncate(kMaxHostnameLength);
}
/// The current username or a sanitized real name if not set.
String get username =>
_username.value ??
realName.sanitize().toLowerCase().truncate(kMaxUsernameLength);
set username(String value) => _username.value = value;
/// The current password.
String get password => _password.value ?? '';
set password(String value) => _password.value = value;
/// The confirmed password for validation.
String get confirmedPassword => _confirmedPassword.value ?? '';
set confirmedPassword(String value) => _confirmedPassword.value = value;
/// Estimates the strength of the password.
PasswordStrength get passwordStrength => estimatePasswordStrength(password);
/// Whether to enable automatic login, or require password.
bool get autoLogin => _autoLogin.value;
set autoLogin(bool value) => _autoLogin.value = value;
/// Returns true if there is at least site-wide connectivity.
bool get isConnected => _network.isConnected || _network.isConnectedSite;
/// Whether Active Directory support is available or null when unknown.
bool? get hasActiveDirectorySupport => _hasActiveDirectorySupport.value;
/// Whether to use Active Directory.
bool get useActiveDirectory => _useActiveDirectory.value;
set useActiveDirectory(bool value) => _useActiveDirectory.value = value;
/// Whether the current input is valid.
bool get isValid {
return realName.isNotEmpty &&
realName.length <= kMaxRealNameLength &&
hostname.isNotEmpty &&
hostname.length <= kMaxHostnameLength &&
username.isNotEmpty &&
username.length <= kMaxUsernameLength &&
usernameOk &&
password.isNotEmpty &&
password == confirmedPassword &&
RegExp(kValidUsernamePattern).hasMatch(username) &&
RegExp(kValidHostnamePattern).hasMatch(hostname);
}
/// The server response on whether the desired username is available.
UsernameValidation get usernameValidation => _usernameValidation.value;
bool get usernameOk => _usernameValidation.value == UsernameValidation.OK;
Future<void> validate() async {
if (username.isNotEmpty &&
RegExp(kValidUsernamePattern).hasMatch(username)) {
_usernameValidation.value = await _service.validateUsername(username);
}
}
/// Loads the identity data from the server, and resolves the system hostname.
Future<void> init() async {
final identity = await _service.getIdentity();
_realName.value ??= identity.realname.orIfEmpty(null);
_hostname.value ??= identity.hostname.orIfEmpty(null);
_username.value ??= identity.username.orIfEmpty(null);
_log.info('Loaded identity: $identity');
_productName.value = await _readProductName();
_log.info('Read product name: ${_productName.value}');
_autoLogin.value = identity.autoLogin;
_hasActiveDirectorySupport.value = await _activeDirectory.hasSupport();
_useActiveDirectory.value = await _activeDirectory.isUsed();
}
/// Saves the identity data to the server.
Future<void> save() async {
final identity = Identity(
realname: realName,
hostname: hostname,
username: username,
password: password,
autoLogin: autoLogin,
);
_log.info('Saved identity: $identity');
await _telemetry?.addMetric('UseActiveDirectory', useActiveDirectory);
await Future.wait([
_service.setIdentity(identity),
_activeDirectory.setUsed(useActiveDirectory),
]);
}
/// Defines if the password is shown or obscured.
bool get showPassword => _showPassword.value;
set showPassword(bool value) => _showPassword.value = value;
}
/// The DMI product name file. Available for testing.
@visibleForTesting
const kDMIProductNameFile = '/sys/devices/virtual/dmi/id/product_name';
@visibleForTesting
const kDMIProductVersionFile = '/sys/devices/virtual/dmi/id/product_version';
@visibleForTesting
const kDMIVendorFile = '/sys/devices/virtual/dmi/id/sys_vendor';
Future<String> _readDmiFile(String path) {
return File(path)
.readAsString()
.then((value) => value.trim().sanitize())
.catchError((_) => '');
}
Future<String> _readProductName() async {
String productName;
final vendor = await _readDmiFile(kDMIVendorFile);
if (vendor == 'IBM' || vendor == 'LENOVO') {
productName = await _readDmiFile(kDMIProductVersionFile);
if (productName.isEmpty) {
productName = await _readDmiFile(kDMIProductNameFile);
}
} else {
productName = await _readDmiFile(kDMIProductNameFile);
if (productName.isEmpty) {
productName = await _readDmiFile(kDMIProductVersionFile);
}
}
return productName;
}
extension _StringTruncate on String {
String truncate(int length) =>
this.length > length ? substring(0, length) : this;
}