This commit is contained in:
Dain Nilsson 2022-05-11 14:04:31 +02:00
commit 1c9d585b27
No known key found for this signature in database
GPG Key ID: F04367096FBA95E8
2 changed files with 28 additions and 14 deletions

View File

@ -247,7 +247,7 @@ class OathManager(
val code = val code =
if (credentialData.oathType == OathType.TOTP && !requireTouch) { if (credentialData.oathType == OathType.TOTP && !requireTouch) {
// recalculate the code // recalculate the code
calculateCode(session, credential, System.currentTimeMillis()) calculateCode(session, credential)
} else null } else null
val addedCred = _model.add( val addedCred = _model.add(
@ -348,7 +348,7 @@ class OathManager(
val code = _model.updateCode( val code = _model.updateCode(
session.deviceId, session.deviceId,
credential.model(session.deviceId), credential.model(session.deviceId),
calculateCode(session, credential, System.currentTimeMillis()).model() calculateCode(session, credential).model()
) )
if (code != null) { if (code != null) {
@ -371,20 +371,21 @@ class OathManager(
* Returns Steam code or standard TOTP code based on the credential. * Returns Steam code or standard TOTP code based on the credential.
* @param session OathSession which calculates the TOTP code * @param session OathSession which calculates the TOTP code
* @param credential * @param credential
* @param timestamp time for TOTP calculation
* *
* @return calculated Code * @return calculated Code
*/ */
private fun calculateCode( private fun calculateCode(
session: OathSession, session: OathSession,
credential: Credential, credential: Credential
timestamp: Long ): Code {
) = // Manual calculate, need to pad timer to avoid immediate expiration
if (credential.isSteamCredential()) { val timestamp = System.currentTimeMillis() + 10000
return if (credential.isSteamCredential()) {
session.calculateSteamCode(credential, timestamp) session.calculateSteamCode(credential, timestamp)
} else { } else {
session.calculateCode(credential, timestamp) session.calculateCode(credential, timestamp)
} }
}
private suspend fun sendDeviceInfo(device: YubiKeyDevice) { private suspend fun sendDeviceInfo(device: YubiKeyDevice) {
@ -490,11 +491,15 @@ class OathManager(
} }
private fun calculateOathCodes(session: OathSession): Map<Credential, Code> { private fun calculateOathCodes(session: OathSession): Map<Credential, Code> {
val timeStamp = System.currentTimeMillis() var timestamp = System.currentTimeMillis()
return session.calculateCodes(timeStamp).map { (credential, code) -> if (!_isUsbKey) {
// NFC, need to pad timer to avoid immediate expiration
timestamp += 10000
}
return session.calculateCodes(timestamp).map { (credential, code) ->
Pair( Pair(
credential, if (credential.isSteamCredential()) { credential, if (credential.isSteamCredential() && !credential.isTouchRequired) {
session.calculateSteamCode(credential, timeStamp) session.calculateSteamCode(credential, timestamp)
} else { } else {
code code
} }

View File

@ -240,6 +240,11 @@ class _DesktopCredentialListNotifier extends OathCredentialListNotifier {
@override @override
Future<OathCode> calculate(OathCredential credential, Future<OathCode> calculate(OathCredential credential,
{bool update = true}) async { {bool update = true}) async {
var now = DateTime.now().millisecondsSinceEpoch ~/ 1000;
if (update) {
// Manually triggered, need to pad timer to avoid immediate expiration
now += 10;
}
final OathCode code; final OathCode code;
final signaler = Signaler(); final signaler = Signaler();
UserInteractionController? controller; UserInteractionController? controller;
@ -256,7 +261,7 @@ class _DesktopCredentialListNotifier extends OathCredentialListNotifier {
} }
}); });
if (credential.isSteam) { if (credential.isSteam) {
final timeStep = DateTime.now().millisecondsSinceEpoch ~/ 30000; final timeStep = now ~/ 30;
var result = await _session.command('calculate', var result = await _session.command('calculate',
target: ['accounts', credential.id], target: ['accounts', credential.id],
params: { params: {
@ -266,8 +271,12 @@ class _DesktopCredentialListNotifier extends OathCredentialListNotifier {
code = OathCode(_formatSteam(result['response']), timeStep * 30, code = OathCode(_formatSteam(result['response']), timeStep * 30,
(timeStep + 1) * 30); (timeStep + 1) * 30);
} else { } else {
var result = await _session.command('code', var result = await _session.command(
target: ['accounts', credential.id], signal: signaler); 'code',
target: ['accounts', credential.id],
params: {'timestamp': now},
signal: signaler,
);
code = OathCode.fromJson(result); code = OathCode.fromJson(result);
} }
_log.debug('Calculate', jsonEncode(code)); _log.debug('Calculate', jsonEncode(code));