Credential id as String extension function

This commit is contained in:
Adam Velebil 2022-03-16 09:41:26 +01:00
parent dfa8058b9d
commit dfe1d9003e
No known key found for this signature in database
GPG Key ID: AC6D6B9D715FC084
2 changed files with 6 additions and 9 deletions

View File

@ -4,6 +4,7 @@ import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.yubico.authenticator.SerializeHelpers.Companion.idAsString
import com.yubico.authenticator.SerializeHelpers.Companion.toJson
import com.yubico.authenticator.api.Pigeon
import com.yubico.yubikit.android.transport.nfc.NfcYubiKeyDevice
@ -217,9 +218,7 @@ class MainViewModel : ViewModel() {
private fun getOathCredential(oathSession: OathSession, credentialId: String) =
oathSession.credentials.firstOrNull { credential ->
(credential != null) && SerializeHelpers.credentialIdAsString(
credential.id
) == credentialId
(credential != null) && credential.idAsString() == credentialId
} ?: throw Exception("Failed to find account to delete")
@ -272,8 +271,8 @@ class MainViewModel : ViewModel() {
val jsonResult =
session.renameCredential(credential, name, issuer)
.toJson(session.deviceId)
.toString()
.toJson(session.deviceId)
.toString()
result.success(jsonResult)
}

View File

@ -74,16 +74,14 @@ class SerializeHelpers {
)
)
fun credentialIdAsString(id: ByteArray): String = id.joinToString(
fun Credential.idAsString() = id.joinToString(
separator = ""
) { b -> "%02x".format(b) }
fun Credential.toJson(deviceId: String) =
JsonObject(
mapOf(
"id" to JsonPrimitive(
credentialIdAsString(id)
),
"id" to JsonPrimitive(idAsString()),
"device_id" to JsonPrimitive(deviceId),
"issuer" to JsonPrimitive(issuer),
"name" to JsonPrimitive(accountName),