yubioath-flutter/lib/oath/state.dart

385 lines
12 KiB
Dart
Raw Normal View History

2021-11-19 17:05:57 +03:00
import 'dart:async';
import 'dart:convert';
import 'dart:math';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:logging/logging.dart';
import 'package:yubico_authenticator/app/models.dart';
2021-11-19 17:05:57 +03:00
import '../app/state.dart';
2021-11-19 17:05:57 +03:00
import '../core/state.dart';
import 'models.dart';
final log = Logger('oath.state');
2021-12-06 12:26:38 +03:00
final _sessionProvider = Provider.autoDispose
.family<RpcNodeSession, List<String>>((ref, devicePath) =>
RpcNodeSession(ref.watch(rpcProvider), devicePath, ['ccid', 'oath']));
2021-11-19 17:05:57 +03:00
// This remembers the key for all devices for the duration of the process.
final _lockKeyProvider =
StateNotifierProvider.family<_LockKeyNotifier, String?, List<String>>(
(ref, devicePath) => _LockKeyNotifier(null));
class _LockKeyNotifier extends StateNotifier<String?> {
_LockKeyNotifier(String? state) : super(state);
setKey(String key) {
state = key;
}
2021-12-08 13:20:04 +03:00
unsetKey() {
state = null;
}
}
2021-11-19 17:05:57 +03:00
final oathStateProvider = StateNotifierProvider.autoDispose
.family<OathStateNotifier, OathState?, List<String>>(
2021-12-06 12:26:38 +03:00
(ref, devicePath) {
final session = ref.watch(_sessionProvider(devicePath));
2022-01-21 13:30:14 +03:00
final notifier = OathStateNotifier(session, ref);
2021-12-06 12:26:38 +03:00
session
..setErrorHandler('state-reset', (_) async {
ref.refresh(_sessionProvider(devicePath));
})
..setErrorHandler('auth-required', (_) async {
await notifier.refresh();
});
ref.onDispose(() {
session
..unserErrorHandler('state-reset')
..unserErrorHandler('auth-required');
});
return notifier..refresh();
},
2021-11-19 17:05:57 +03:00
);
class OathStateNotifier extends StateNotifier<OathState?> {
final RpcNodeSession _session;
2022-01-21 13:30:14 +03:00
final Ref _ref;
OathStateNotifier(this._session, this._ref) : super(null);
2021-11-19 17:05:57 +03:00
refresh() async {
var result = await _session.command('get');
log.config('application status', jsonEncode(result));
var oathState = OathState.fromJson(result['data']);
2022-01-21 13:30:14 +03:00
final key = _ref.read(_lockKeyProvider(_session.devicePath));
if (oathState.locked && key != null) {
2021-12-08 13:20:04 +03:00
final result = await _session.command('validate', params: {'key': key});
if (result['unlocked']) {
oathState = oathState.copyWith(locked: false);
} else {
2022-01-21 13:30:14 +03:00
_ref.read(_lockKeyProvider(_session.devicePath).notifier).unsetKey();
2021-12-08 13:20:04 +03:00
}
}
2021-11-19 17:05:57 +03:00
if (mounted) {
state = oathState;
2021-11-19 17:05:57 +03:00
}
}
2022-01-21 13:30:14 +03:00
Future<void> reset() async {
await _session.command('reset');
_ref.read(_lockKeyProvider(_session.devicePath).notifier).unsetKey();
_ref.refresh(_sessionProvider(_session.devicePath));
}
2021-11-19 17:05:57 +03:00
Future<bool> unlock(String password) async {
var result =
await _session.command('derive', params: {'password': password});
var key = result['key'];
2021-12-08 13:20:04 +03:00
final status = await _session.command('validate', params: {'key': key});
if (mounted && status['unlocked']) {
log.config('applet unlocked');
2022-01-21 13:30:14 +03:00
_ref.read(_lockKeyProvider(_session.devicePath).notifier).setKey(key);
2021-11-19 17:05:57 +03:00
state = state?.copyWith(locked: false);
}
2021-12-08 13:20:04 +03:00
return status['unlocked'];
}
Future<bool> _checkPassword(String password) async {
var result =
await _session.command('derive', params: {'password': password});
2022-01-21 13:30:14 +03:00
return _ref.read(_lockKeyProvider(_session.devicePath)) == result['key'];
2021-12-08 13:20:04 +03:00
}
Future<bool> setPassword(String? current, String password) async {
if (state?.hasKey ?? false) {
if (current != null) {
if (!await _checkPassword(current)) {
return false;
}
} else {
return false;
}
}
var result =
await _session.command('derive', params: {'password': password});
var key = result['key'];
await _session.command('set_key', params: {'key': key});
log.config('OATH key set');
2022-01-21 13:30:14 +03:00
_ref.read(_lockKeyProvider(_session.devicePath).notifier).setKey(key);
2021-12-08 13:20:04 +03:00
if (mounted) {
state = state?.copyWith(hasKey: true);
}
return true;
}
Future<bool> unsetPassword(String current) async {
if (state?.hasKey ?? false) {
if (!await _checkPassword(current)) {
return false;
}
}
await _session.command('unset_key');
2022-01-21 13:30:14 +03:00
_ref.read(_lockKeyProvider(_session.devicePath).notifier).unsetKey();
2021-12-08 13:20:04 +03:00
if (mounted) {
state = state?.copyWith(hasKey: false, locked: false);
}
2021-11-19 17:05:57 +03:00
return true;
}
}
final credentialListProvider = StateNotifierProvider.autoDispose
.family<CredentialListNotifier, List<OathPair>?, List<String>>(
(ref, devicePath) {
var notifier = CredentialListNotifier(
2021-11-19 17:05:57 +03:00
ref.watch(_sessionProvider(devicePath)),
ref.watch(oathStateProvider(devicePath).select((s) => s?.locked ?? true)),
);
ref.listen<WindowState>(windowStateProvider, (_, windowState) {
notifier._notifyWindowState(windowState);
}, fireImmediately: true);
return notifier;
2021-11-19 17:05:57 +03:00
},
);
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 CredentialListNotifier extends StateNotifier<List<OathPair>?> {
final RpcNodeSession _session;
final bool _locked;
Timer? _timer;
CredentialListNotifier(this._session, this._locked) : super(null);
void _notifyWindowState(WindowState windowState) {
if (_locked) return;
if (windowState.active) {
_scheduleRefresh();
} else {
_timer?.cancel();
}
}
2021-11-19 17:05:57 +03:00
@override
void dispose() {
_timer?.cancel();
super.dispose();
}
@override
@protected
set state(List<OathPair>? value) {
super.state = value != null ? List.unmodifiable(value) : null;
}
2021-12-06 12:26:38 +03:00
Future<OathCode> calculate(OathCredential credential,
{bool update = true}) async {
2021-12-06 13:06:49 +03:00
final OathCode code;
2021-11-19 17:05:57 +03:00
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);
}
log.config('Calculate', jsonEncode(code));
2021-12-06 12:26:38 +03:00
if (update && mounted) {
2021-11-19 17:05:57 +03:00
final creds = state!.toList();
final i = creds.indexWhere((e) => e.credential.id == credential.id);
state = creds..[i] = creds[i].copyWith(code: code);
}
2021-12-06 12:26:38 +03:00
return code;
2021-11-19 17:05:57 +03:00
}
2021-12-06 12:26:38 +03:00
Future<OathCredential> addAccount(Uri otpauth,
{bool requireTouch = false, bool update = true}) async {
2021-11-19 17:05:57 +03:00
var result = await _session.command('put', target: [
'accounts'
], params: {
'uri': otpauth.toString(),
'require_touch': requireTouch,
});
final credential = OathCredential.fromJson(result);
2021-12-06 12:26:38 +03:00
if (update && mounted) {
2021-11-19 17:05:57 +03:00
state = state!.toList()..add(OathPair(credential, null));
if (!requireTouch && credential.oathType == OathType.totp) {
await calculate(credential);
2021-11-19 17:05:57 +03:00
}
}
2021-12-06 12:26:38 +03:00
return credential;
2021-11-19 17:05:57 +03:00
}
2022-01-24 12:59:09 +03:00
Future<void> 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'];
if (mounted) {
final newState = state!.toList();
final index = newState.indexWhere((e) => e.credential == credential);
final oldPair = newState.removeAt(index);
newState.add(OathPair(
credential.copyWith(id: credentialId, issuer: issuer, name: name),
oldPair.code,
));
state = newState;
}
}
2022-01-21 17:45:04 +03:00
Future<void> deleteAccount(OathCredential credential) async {
await _session.command('delete', target: ['accounts', credential.id]);
if (mounted) {
state = state!.toList()..removeWhere((e) => e.credential == credential);
}
}
2021-11-19 17:05:57 +03:00
refresh() async {
if (_locked) return;
log.config('refreshing credentials...');
2021-11-19 17:05:57 +03:00
var result = await _session.command('calculate_all', target: ['accounts']);
log.config('Entries', jsonEncode(result));
2021-11-19 17:05:57 +03:00
2021-12-06 13:06:49 +03:00
final pairs = [];
2021-12-06 12:26:38 +03:00
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']);
2021-12-06 13:06:49 +03:00
pairs.add(OathPair(credential, code));
2021-12-06 12:26:38 +03:00
}
2021-12-06 13:06:49 +03:00
2021-12-06 12:26:38 +03:00
if (mounted) {
2021-12-06 13:06:49 +03:00
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);
}
}
2021-11-19 17:05:57 +03:00
state = current;
_scheduleRefresh();
}
}
_scheduleRefresh() {
_timer?.cancel();
if (_locked) return;
if (state == null) {
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) {
_timer = null;
} else {
final earliest = expirations.reduce(min) * 1000;
final now = DateTime.now().millisecondsSinceEpoch;
if (earliest < now) {
refresh();
} else {
_timer = Timer(Duration(milliseconds: earliest - now), refresh);
}
}
2021-11-19 17:05:57 +03:00
}
}
}
2021-12-03 17:15:00 +03:00
final favoritesProvider =
StateNotifierProvider<FavoritesNotifier, List<String>>(
(ref) => FavoritesNotifier(ref.watch(prefProvider)));
2021-11-19 17:05:57 +03:00
2021-12-03 17:15:00 +03:00
class FavoritesNotifier extends StateNotifier<List<String>> {
static const String _key = 'OATH_STATE_FAVORITES';
final SharedPreferences _prefs;
FavoritesNotifier(this._prefs) : super(_prefs.getStringList(_key) ?? []);
2021-12-03 17:15:00 +03:00
toggleFavorite(String credentialId) {
if (state.contains(credentialId)) {
state = state.toList()..remove(credentialId);
} else {
state = [credentialId, ...state];
}
2021-12-03 17:15:00 +03:00
_prefs.setStringList(_key, state);
2021-11-19 17:05:57 +03:00
}
}
final filteredCredentialsProvider = StateNotifierProvider.autoDispose
.family<FilteredCredentialsNotifier, List<OathPair>, List<OathPair>>(
(ref, full) {
2021-12-03 17:15:00 +03:00
return FilteredCredentialsNotifier(full, ref.watch(searchProvider));
2021-11-19 17:05:57 +03:00
});
class FilteredCredentialsNotifier extends StateNotifier<List<OathPair>> {
final String query;
FilteredCredentialsNotifier(
List<OathPair> full,
this.query,
) : super(
full
.where((pair) =>
"${pair.credential.issuer ?? ''}:${pair.credential.name}"
.toLowerCase()
.contains(query.toLowerCase()))
.toList()
..sort((a, b) {
2021-12-03 17:15:00 +03:00
String searchKey(OathCredential c) => (c.issuer ?? '') + c.name;
return searchKey(a.credential).compareTo(searchKey(b.credential));
}),
);
2021-11-19 17:05:57 +03:00
}