2022-01-27 14:34:29 +03:00
|
|
|
import 'dart:async';
|
|
|
|
import 'dart:convert';
|
|
|
|
import 'dart:math';
|
|
|
|
|
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
|
|
|
import 'package:logging/logging.dart';
|
|
|
|
|
|
|
|
import '../../app/models.dart';
|
|
|
|
import '../../app/state.dart';
|
2022-02-22 17:22:41 +03:00
|
|
|
import '../../core/models.dart';
|
2022-01-27 14:34:29 +03:00
|
|
|
import '../../oath/models.dart';
|
|
|
|
import '../../oath/state.dart';
|
|
|
|
import '../rpc.dart';
|
|
|
|
import '../state.dart';
|
|
|
|
|
2022-02-21 11:38:09 +03:00
|
|
|
final _log = Logger('desktop.oath.state');
|
2022-01-27 14:34:29 +03:00
|
|
|
|
|
|
|
final _sessionProvider =
|
2022-02-08 15:44:35 +03:00
|
|
|
Provider.autoDispose.family<RpcNodeSession, DevicePath>(
|
2022-01-27 14:34:29 +03:00
|
|
|
(ref, devicePath) =>
|
|
|
|
RpcNodeSession(ref.watch(rpcProvider), devicePath, ['ccid', 'oath']),
|
|
|
|
);
|
|
|
|
|
2022-02-08 14:25:36 +03:00
|
|
|
// This remembers the key for all devices for the duration of the process.
|
|
|
|
final _oathLockKeyProvider =
|
2022-02-08 15:44:35 +03:00
|
|
|
StateNotifierProvider.family<_LockKeyNotifier, String?, DevicePath>(
|
2022-02-08 14:25:36 +03:00
|
|
|
(ref, devicePath) => _LockKeyNotifier(null));
|
|
|
|
|
|
|
|
class _LockKeyNotifier extends StateNotifier<String?> {
|
|
|
|
_LockKeyNotifier(String? state) : super(state);
|
|
|
|
|
|
|
|
setKey(String key) {
|
|
|
|
state = key;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsetKey() {
|
|
|
|
state = null;
|
|
|
|
}
|
2022-02-08 15:44:35 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
super.dispose();
|
|
|
|
}
|
2022-02-08 14:25:36 +03:00
|
|
|
}
|
|
|
|
|
2022-01-27 14:34:29 +03:00
|
|
|
final desktopOathState = StateNotifierProvider.autoDispose
|
2022-03-24 00:55:18 +03:00
|
|
|
.family<OathStateNotifier, AsyncValue<OathState>, DevicePath>(
|
2022-01-27 14:34:29 +03:00
|
|
|
(ref, devicePath) {
|
|
|
|
final session = ref.watch(_sessionProvider(devicePath));
|
|
|
|
final notifier = _DesktopOathStateNotifier(session, ref);
|
|
|
|
session
|
|
|
|
..setErrorHandler('state-reset', (_) async {
|
|
|
|
ref.refresh(_sessionProvider(devicePath));
|
|
|
|
})
|
|
|
|
..setErrorHandler('auth-required', (_) async {
|
|
|
|
await notifier.refresh();
|
|
|
|
});
|
|
|
|
ref.onDispose(() {
|
|
|
|
session
|
2022-03-15 19:16:14 +03:00
|
|
|
..unsetErrorHandler('state-reset')
|
|
|
|
..unsetErrorHandler('auth-required');
|
2022-01-27 14:34:29 +03:00
|
|
|
});
|
|
|
|
return notifier..refresh();
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
class _DesktopOathStateNotifier extends OathStateNotifier {
|
|
|
|
final RpcNodeSession _session;
|
|
|
|
final Ref _ref;
|
|
|
|
_DesktopOathStateNotifier(this._session, this._ref) : super();
|
|
|
|
|
2022-03-24 00:55:18 +03:00
|
|
|
refresh() => updateState(() async {
|
|
|
|
final result = await _session.command('get');
|
|
|
|
_log.config('application status', jsonEncode(result));
|
|
|
|
var oathState = OathState.fromJson(result['data']);
|
|
|
|
final key = _ref.read(_oathLockKeyProvider(_session.devicePath));
|
|
|
|
if (oathState.locked && key != null) {
|
|
|
|
final result =
|
|
|
|
await _session.command('validate', params: {'key': key});
|
|
|
|
if (result['valid']) {
|
|
|
|
oathState = oathState.copyWith(locked: false);
|
|
|
|
} else {
|
|
|
|
_ref
|
|
|
|
.read(_oathLockKeyProvider(_session.devicePath).notifier)
|
|
|
|
.unsetKey();
|
|
|
|
}
|
2022-03-09 19:47:50 +03:00
|
|
|
}
|
2022-03-24 00:55:18 +03:00
|
|
|
return oathState;
|
|
|
|
});
|
2022-01-27 14:34:29 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> reset() async {
|
|
|
|
await _session.command('reset');
|
2022-02-08 14:25:36 +03:00
|
|
|
_ref.read(_oathLockKeyProvider(_session.devicePath).notifier).unsetKey();
|
2022-01-27 14:34:29 +03:00
|
|
|
_ref.refresh(_sessionProvider(_session.devicePath));
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
2022-02-22 17:22:41 +03:00
|
|
|
Future<Pair<bool, bool>> unlock(String password,
|
|
|
|
{bool remember = false}) async {
|
|
|
|
var derive =
|
2022-01-27 14:34:29 +03:00
|
|
|
await _session.command('derive', params: {'password': password});
|
2022-02-22 17:22:41 +03:00
|
|
|
var key = derive['key'];
|
|
|
|
final validate = await _session
|
2022-02-08 14:25:36 +03:00
|
|
|
.command('validate', params: {'key': key, 'remember': remember});
|
2022-02-22 17:22:41 +03:00
|
|
|
final bool valid = validate['valid'];
|
|
|
|
final bool remembered = validate['remembered'];
|
2022-03-09 19:47:50 +03:00
|
|
|
if (valid) {
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.config('applet unlocked');
|
2022-02-08 14:25:36 +03:00
|
|
|
_ref.read(_oathLockKeyProvider(_session.devicePath).notifier).setKey(key);
|
2022-03-24 00:55:18 +03:00
|
|
|
setData(state.value!.copyWith(
|
2022-02-08 14:25:36 +03:00
|
|
|
locked: false,
|
2022-02-22 17:22:41 +03:00
|
|
|
remembered: remembered,
|
2022-03-09 19:47:50 +03:00
|
|
|
));
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
2022-02-22 17:22:41 +03:00
|
|
|
return Pair(valid, remembered);
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<bool> _checkPassword(String password) async {
|
|
|
|
var result =
|
2022-02-08 14:25:36 +03:00
|
|
|
await _session.command('validate', params: {'password': password});
|
2022-02-22 17:22:41 +03:00
|
|
|
return result['valid'];
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> setPassword(String? current, String password) async {
|
2022-03-24 00:55:18 +03:00
|
|
|
final oathState = state.value!;
|
2022-03-09 19:47:50 +03:00
|
|
|
if (oathState.hasKey) {
|
2022-01-27 14:34:29 +03:00
|
|
|
if (current != null) {
|
|
|
|
if (!await _checkPassword(current)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:47:50 +03:00
|
|
|
if (oathState.remembered) {
|
2022-02-22 17:22:41 +03:00
|
|
|
// Remembered, keep remembering
|
|
|
|
await _session
|
|
|
|
.command('set_key', params: {'password': password, 'remember': true});
|
|
|
|
} else {
|
|
|
|
// Not remembered, keep in keyProvider
|
|
|
|
var derive =
|
|
|
|
await _session.command('derive', params: {'password': password});
|
|
|
|
var key = derive['key'];
|
|
|
|
await _session.command('set_key', params: {'key': key});
|
|
|
|
_ref.read(_oathLockKeyProvider(_session.devicePath).notifier).setKey(key);
|
|
|
|
}
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.config('OATH key set');
|
2022-02-22 17:22:41 +03:00
|
|
|
|
2022-03-09 19:47:50 +03:00
|
|
|
if (!oathState.hasKey) {
|
2022-03-24 00:55:18 +03:00
|
|
|
setData(oathState.copyWith(hasKey: true));
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<bool> unsetPassword(String current) async {
|
2022-03-24 00:55:18 +03:00
|
|
|
final oathState = state.value!;
|
2022-03-09 19:47:50 +03:00
|
|
|
if (oathState.hasKey) {
|
2022-01-27 14:34:29 +03:00
|
|
|
if (!await _checkPassword(current)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
await _session.command('unset_key');
|
2022-02-08 14:25:36 +03:00
|
|
|
_ref.read(_oathLockKeyProvider(_session.devicePath).notifier).unsetKey();
|
2022-03-24 00:55:18 +03:00
|
|
|
setData(oathState.copyWith(hasKey: false, locked: false));
|
2022-01-27 14:34:29 +03:00
|
|
|
return true;
|
|
|
|
}
|
2022-02-08 14:25:36 +03:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> forgetPassword() async {
|
|
|
|
await _session.command('forget');
|
|
|
|
_ref.read(_oathLockKeyProvider(_session.devicePath).notifier).unsetKey();
|
2022-03-24 00:55:18 +03:00
|
|
|
setData(state.value!.copyWith(remembered: false));
|
2022-02-08 14:25:36 +03:00
|
|
|
}
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
final desktopOathCredentialListProvider = StateNotifierProvider.autoDispose
|
2022-02-08 15:44:35 +03:00
|
|
|
.family<OathCredentialListNotifier, List<OathPair>?, DevicePath>(
|
2022-01-27 14:34:29 +03:00
|
|
|
(ref, devicePath) {
|
|
|
|
var notifier = _DesktopCredentialListNotifier(
|
|
|
|
ref.watch(_sessionProvider(devicePath)),
|
2022-03-24 00:55:18 +03:00
|
|
|
ref.watch(oathStateProvider(devicePath)
|
|
|
|
.select((r) => r.whenOrNull(data: (state) => state.locked) ?? true)),
|
2022-01-27 14:34:29 +03:00
|
|
|
);
|
|
|
|
ref.listen<WindowState>(windowStateProvider, (_, windowState) {
|
|
|
|
notifier._notifyWindowState(windowState);
|
|
|
|
}, fireImmediately: true);
|
|
|
|
return notifier;
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
extension on OathCredential {
|
|
|
|
bool get isSteam => issuer == 'Steam' && oathType == OathType.totp;
|
|
|
|
}
|
|
|
|
|
|
|
|
const String _steamCharTable = '23456789BCDFGHJKMNPQRTVWXY';
|
|
|
|
String _formatSteam(String response) {
|
|
|
|
final offset = int.parse(response.substring(response.length - 1), radix: 16);
|
|
|
|
var number =
|
|
|
|
int.parse(response.substring(offset * 2, offset * 2 + 8), radix: 16) &
|
|
|
|
0x7fffffff;
|
|
|
|
var value = '';
|
|
|
|
for (var i = 0; i < 5; i++) {
|
|
|
|
value += _steamCharTable[number % _steamCharTable.length];
|
|
|
|
number ~/= _steamCharTable.length;
|
|
|
|
}
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
class _DesktopCredentialListNotifier extends OathCredentialListNotifier {
|
|
|
|
final RpcNodeSession _session;
|
|
|
|
final bool _locked;
|
|
|
|
Timer? _timer;
|
|
|
|
_DesktopCredentialListNotifier(this._session, this._locked) : super();
|
|
|
|
|
|
|
|
void _notifyWindowState(WindowState windowState) {
|
|
|
|
if (_locked) return;
|
|
|
|
if (windowState.active) {
|
|
|
|
_scheduleRefresh();
|
|
|
|
} else {
|
|
|
|
_timer?.cancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.config('OATH notifier discarded');
|
2022-01-27 14:34:29 +03:00
|
|
|
_timer?.cancel();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<OathCode> calculate(OathCredential credential,
|
|
|
|
{bool update = true}) async {
|
|
|
|
final OathCode code;
|
|
|
|
if (credential.isSteam) {
|
|
|
|
final timeStep = DateTime.now().millisecondsSinceEpoch ~/ 30000;
|
|
|
|
var result = await _session.command('calculate', target: [
|
|
|
|
'accounts',
|
|
|
|
credential.id
|
|
|
|
], params: {
|
|
|
|
'challenge': timeStep.toRadixString(16).padLeft(16, '0'),
|
|
|
|
});
|
|
|
|
code = OathCode(
|
|
|
|
_formatSteam(result['response']), timeStep * 30, (timeStep + 1) * 30);
|
|
|
|
} else {
|
|
|
|
var result =
|
|
|
|
await _session.command('code', target: ['accounts', credential.id]);
|
|
|
|
code = OathCode.fromJson(result);
|
|
|
|
}
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.config('Calculate', jsonEncode(code));
|
2022-01-27 14:34:29 +03:00
|
|
|
if (update && mounted) {
|
|
|
|
final creds = state!.toList();
|
|
|
|
final i = creds.indexWhere((e) => e.credential.id == credential.id);
|
|
|
|
state = creds..[i] = creds[i].copyWith(code: code);
|
|
|
|
}
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<OathCredential> addAccount(Uri otpauth,
|
2022-04-06 12:53:50 +03:00
|
|
|
{bool requireTouch = false}) async {
|
2022-01-27 14:34:29 +03:00
|
|
|
var result = await _session.command('put', target: [
|
|
|
|
'accounts'
|
|
|
|
], params: {
|
|
|
|
'uri': otpauth.toString(),
|
|
|
|
'require_touch': requireTouch,
|
|
|
|
});
|
2022-04-06 12:53:50 +03:00
|
|
|
refresh();
|
|
|
|
return OathCredential.fromJson(result);
|
2022-01-27 14:34:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<OathCredential> renameAccount(
|
|
|
|
OathCredential credential,
|
|
|
|
String? issuer,
|
|
|
|
String name,
|
|
|
|
) async {
|
|
|
|
final result = await _session.command('rename', target: [
|
|
|
|
'accounts',
|
|
|
|
credential.id,
|
|
|
|
], params: {
|
|
|
|
'issuer': issuer,
|
|
|
|
'name': name,
|
|
|
|
});
|
|
|
|
String credentialId = result['credential_id'];
|
|
|
|
final renamedCredential =
|
|
|
|
credential.copyWith(id: credentialId, issuer: issuer, name: name);
|
|
|
|
if (mounted) {
|
|
|
|
final newState = state!.toList();
|
|
|
|
final index = newState.indexWhere((e) => e.credential == credential);
|
|
|
|
final oldPair = newState.removeAt(index);
|
|
|
|
newState.add(OathPair(
|
|
|
|
renamedCredential,
|
|
|
|
oldPair.code,
|
|
|
|
));
|
|
|
|
state = newState;
|
|
|
|
}
|
|
|
|
return renamedCredential;
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<void> deleteAccount(OathCredential credential) async {
|
|
|
|
await _session.command('delete', target: ['accounts', credential.id]);
|
|
|
|
if (mounted) {
|
|
|
|
state = state!.toList()..removeWhere((e) => e.credential == credential);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
refresh() async {
|
|
|
|
if (_locked) return;
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.config('refreshing credentials...');
|
2022-01-27 14:34:29 +03:00
|
|
|
var result = await _session.command('calculate_all', target: ['accounts']);
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.config('Entries', jsonEncode(result));
|
2022-01-27 14:34:29 +03:00
|
|
|
|
|
|
|
final pairs = [];
|
|
|
|
for (var e in result['entries']) {
|
|
|
|
final credential = OathCredential.fromJson(e['credential']);
|
|
|
|
final code = e['code'] == null
|
|
|
|
? null
|
|
|
|
: credential.isSteam // Steam codes require a re-calculate
|
|
|
|
? await calculate(credential, update: false)
|
|
|
|
: OathCode.fromJson(e['code']);
|
|
|
|
pairs.add(OathPair(credential, code));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mounted) {
|
|
|
|
final current = state?.toList() ?? [];
|
|
|
|
for (var pair in pairs) {
|
|
|
|
final i =
|
|
|
|
current.indexWhere((e) => e.credential.id == pair.credential.id);
|
|
|
|
if (i < 0) {
|
|
|
|
current.add(pair);
|
|
|
|
} else if (pair.code != null) {
|
|
|
|
current[i] = current[i].copyWith(code: pair.code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
state = current;
|
|
|
|
_scheduleRefresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_scheduleRefresh() {
|
|
|
|
_timer?.cancel();
|
|
|
|
if (_locked) return;
|
|
|
|
if (state == null) {
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.config('No OATH state, refresh immediately');
|
2022-01-27 14:34:29 +03:00
|
|
|
refresh();
|
|
|
|
} else if (mounted) {
|
|
|
|
final expirations = (state ?? [])
|
|
|
|
.where((pair) =>
|
|
|
|
pair.credential.oathType == OathType.totp &&
|
|
|
|
!pair.credential.touchRequired)
|
|
|
|
.map((e) => e.code)
|
|
|
|
.whereType<OathCode>()
|
|
|
|
.map((e) => e.validTo);
|
|
|
|
if (expirations.isEmpty) {
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.config('No expirations, no refresh');
|
2022-01-27 14:34:29 +03:00
|
|
|
_timer = null;
|
|
|
|
} else {
|
|
|
|
final earliest = expirations.reduce(min) * 1000;
|
|
|
|
final now = DateTime.now().millisecondsSinceEpoch;
|
|
|
|
if (earliest < now) {
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.config('Already expired, refresh immediately');
|
2022-01-27 14:34:29 +03:00
|
|
|
refresh();
|
|
|
|
} else {
|
2022-02-21 11:38:09 +03:00
|
|
|
_log.config('Schedule refresh in ${earliest - now}ms');
|
2022-01-27 14:34:29 +03:00
|
|
|
_timer = Timer(Duration(milliseconds: earliest - now), refresh);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|