Created
June 14, 2019 14:13
-
-
Save thavel/664fe827044eb21ffe2b2a9e78780107 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test.thavel.webauthn.dto | |
import com.github.kittinunf.fuel.core.ResponseDeserializable | |
import com.google.gson.Gson | |
import com.google.gson.GsonBuilder | |
import duo.labs.webauthn.models.AuthenticatorMakeCredentialOptions | |
import duo.labs.webauthn.models.PublicKeyCredentialDescriptor | |
import duo.labs.webauthn.models.RpEntity | |
import duo.labs.webauthn.models.UserEntity | |
import test.thavel.webauthn.utils.ByteArrayUtils | |
data class RegistrationRequest ( | |
val publicKey: PublicKey | |
) { | |
data class PublicKey ( | |
val challenge: String, | |
val rp: RP, | |
val user: User, | |
val pubKeyCredParams: List<PubKeyCredParams>, | |
val authenticatorSelection: AuthenticatorSelection, | |
val timeout: Int, | |
val excludeCredentials: List<ExcludeCredentials>?, | |
val attestation: String | |
) { | |
data class RP ( | |
val name: String, | |
val id: String | |
) | |
data class User ( | |
val name: String, | |
val displayName: String, | |
val id: String | |
) | |
data class PubKeyCredParams ( | |
val type: String, | |
val alg: Int | |
) { | |
fun toList(): List<Any> { | |
return listOf(this.type, this.alg) | |
} | |
} | |
data class AuthenticatorSelection ( | |
val requireResidentKey: Boolean, | |
val userVerification: String | |
) | |
data class ExcludeCredentials ( | |
val type: String, | |
val id: String | |
) | |
} | |
class Deserializer: ResponseDeserializable<RegistrationRequest> { | |
override fun deserialize(content: String) = Gson().fromJson(content, RegistrationRequest::class.java) | |
} | |
fun format(): String { | |
val algs = mutableListOf<List<Any>>() | |
for (alg in publicKey.pubKeyCredParams) { | |
algs.add(alg.toList()) | |
} | |
val requireUserVerification = (publicKey.authenticatorSelection.userVerification != "") | |
return GsonBuilder().create().toJson(mapOf( | |
"authenticatorExtensions" to "", | |
"clientDataHash" to publicKey.challenge, | |
"credTypesAndPubKeyAlgs" to algs, | |
"excludeCredentials" to (publicKey.excludeCredentials ?: emptyList()), | |
"requireResidentKey" to publicKey.authenticatorSelection.requireResidentKey, | |
"requireUserPresence" to !requireUserVerification, | |
"requireUserVerification" to requireUserVerification, | |
"rp" to publicKey.rp, | |
"user" to publicKey.user | |
)) | |
} | |
fun toOptions(): AuthenticatorMakeCredentialOptions { | |
val rp = RpEntity() | |
rp.id = publicKey.rp.id | |
rp.name = publicKey.rp.name | |
val user = UserEntity() | |
user.id = ByteArrayUtils.decodeBase64URL(publicKey.user.id) | |
user.displayName = publicKey.user.displayName | |
user.name = publicKey.user.name | |
val requireUserVerification = (publicKey.authenticatorSelection.userVerification != "") | |
val algs = mutableListOf<android.util.Pair<String, Long>>() | |
for (alg in publicKey.pubKeyCredParams) { | |
algs.add(android.util.Pair(alg.type, alg.alg.toLong())) | |
} | |
val excludes = mutableListOf<PublicKeyCredentialDescriptor>() | |
for (ex in publicKey.excludeCredentials ?: emptyList()) { | |
excludes.add(PublicKeyCredentialDescriptor( | |
ex.type, | |
ByteArrayUtils.decodeBase64URL(ex.id), | |
emptyList() | |
)) | |
} | |
val options = AuthenticatorMakeCredentialOptions() | |
options.clientDataHash = ByteArrayUtils.decodeBase64URL(publicKey.challenge) | |
options.rpEntity = rp | |
options.userEntity = user | |
options.requireResidentKey = publicKey.authenticatorSelection.requireResidentKey | |
options.requireUserPresence = !requireUserVerification | |
options.requireUserVerification = requireUserVerification | |
options.credTypesAndPubKeyAlgs = algs | |
options.excludeCredentialDescriptorList = excludes | |
return options | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment