YADESK-687 update model properly

This commit is contained in:
Adam Velebil 2022-05-10 08:42:35 +02:00
parent 74ed915c4d
commit 54d6c96e50
No known key found for this signature in database
GPG Key ID: AC6D6B9D715FC084
3 changed files with 44 additions and 9 deletions

View File

@ -110,7 +110,11 @@ class Model {
}
fun update(deviceId: String, credentials: Map<Credential, Code?>) {
if (this.session.deviceId != deviceId) {
// is the model already holding credentials for the deviceId
val sameDevice = this._credentials.keys.firstOrNull()?.deviceId == deviceId
if (!sameDevice) {
// device was changed, we use the new list
this._credentials.clear()
this._credentials.putAll(from = credentials)
@ -119,7 +123,7 @@ class Model {
// update codes for non interactive keys
for ((credential, code) in credentials) {
if (!credential.isInteractive()) {
if (!credential.isInteractive() || !this._credentials.contains(credential)) {
this._credentials[credential] = code
}
}

View File

@ -126,6 +126,43 @@ class ModelTest {
assertTrue(model.credentials.find { it.code == updatedCode } != null)
}
@Test
fun `update uses all credentials from its input `() {
val d = "device"
model.update(d, emptyCredentials())
// in next update the device has credentials
val totp1 = totp(deviceId = d, name = "totp1", touchRequired = false)
val code1 = code(value = "111111")
val totp2 = totp(deviceId = d, name = "totp2", touchRequired = true)
val code2 = code(value = "222222")
val hotp1 = hotp(deviceId = d, name = "hotp1", touchRequired = false)
val code3 = code(value = "33333")
val hotp2 = hotp(deviceId = d, name = "hotp2", touchRequired = true)
val code4 = code(value = "4444")
val m1 = mapOf(totp1 to code1, totp2 to code2, hotp1 to code3, hotp2 to code4)
model.update(d, m1)
// all four are present
val foundTotp1 = model.credentials.find { it.credential == totp1 }
assertTrue(foundTotp1 != null)
assertEquals("111111", foundTotp1?.code?.value)
val foundTotp2 = model.credentials.find { it.credential == totp2 }
assertTrue(foundTotp2 != null)
assertEquals("222222", foundTotp2?.code?.value)
val foundHotp1 = model.credentials.find { it.credential == hotp1 }
assertTrue(foundHotp1 != null)
assertEquals("33333", foundHotp1?.code?.value)
val foundHotp2 = model.credentials.find { it.credential == hotp2 }
assertTrue(foundHotp2 != null)
assertEquals("4444", foundHotp2?.code?.value)
}
@Test
fun `update preserves non-interactive codes`() {
val d = "device"

View File

@ -243,17 +243,11 @@ class _AndroidCredentialListNotifier extends OathCredentialListNotifier {
if (_currentDevice == null) return;
_log.debug('refreshing credentials...');
final pairs = [];
try {
var resultString = await _api.refreshCodes();
var result = jsonDecode(resultString);
for (var e in result['entries']) {
final credential = OathCredential.fromJson(e['credential']);
final code = e['code'] == null ? null : OathCode.fromJson(e['code']);
pairs.add(OathPair(credential, code));
}
final pairs = result.map((e) => OathPair.fromJson(e)).toList();
if (mounted) {
final current = state?.toList() ?? [];