|
| 1 | +package org.monogram.core.ui.auth |
| 2 | + |
| 3 | +import android.content.Context |
| 4 | +import android.os.Build |
| 5 | +import android.util.Base64 |
| 6 | +import androidx.credentials.CreatePublicKeyCredentialRequest |
| 7 | +import androidx.credentials.CreatePublicKeyCredentialResponse |
| 8 | +import androidx.credentials.CredentialManager |
| 9 | +import androidx.credentials.GetCredentialRequest |
| 10 | +import androidx.credentials.GetPublicKeyCredentialOption |
| 11 | +import androidx.credentials.PublicKeyCredential |
| 12 | +import org.json.JSONObject |
| 13 | +import org.monogram.core.models.PasskeyCredential |
| 14 | +import java.security.MessageDigest |
| 15 | + |
| 16 | +object PasskeyCredentials { |
| 17 | + class ClientDataMismatchException : Exception("Credential provider did not use the requested client data") |
| 18 | + |
| 19 | + private const val RELYING_PARTY = "telegram.org" |
| 20 | + private const val ORIGIN = "https://telegram.org" |
| 21 | + |
| 22 | + val supported: Boolean get() = Build.VERSION.SDK_INT >= 28 |
| 23 | + |
| 24 | + suspend fun request( |
| 25 | + context: Context, |
| 26 | + options: String, |
| 27 | + registration: Boolean |
| 28 | + ): PasskeyCredential { |
| 29 | + check(supported) |
| 30 | + val operation = if (registration) Operation.Register else Operation.Login |
| 31 | + val request = CredentialRequest.parse(options, operation) |
| 32 | + val manager = CredentialManager.create(context) |
| 33 | + val response = when (operation) { |
| 34 | + Operation.Register -> register(context, manager, request) |
| 35 | + Operation.Login -> login(context, manager, request) |
| 36 | + } |
| 37 | + return CredentialResponse(response).toCredential(request) |
| 38 | + } |
| 39 | + |
| 40 | + private suspend fun register( |
| 41 | + context: Context, |
| 42 | + manager: CredentialManager, |
| 43 | + request: CredentialRequest, |
| 44 | + ): String { |
| 45 | + val response = manager.createCredential( |
| 46 | + context, |
| 47 | + CreatePublicKeyCredentialRequest( |
| 48 | + requestJson = request.publicKeyJson, |
| 49 | + clientDataHash = request.clientDataHash, |
| 50 | + origin = ORIGIN, |
| 51 | + ), |
| 52 | + ) as CreatePublicKeyCredentialResponse |
| 53 | + return response.registrationResponseJson |
| 54 | + } |
| 55 | + |
| 56 | + private suspend fun login( |
| 57 | + context: Context, |
| 58 | + manager: CredentialManager, |
| 59 | + request: CredentialRequest, |
| 60 | + ): String { |
| 61 | + val response = manager.getCredential( |
| 62 | + context, |
| 63 | + GetCredentialRequest.Builder() |
| 64 | + .addCredentialOption( |
| 65 | + GetPublicKeyCredentialOption( |
| 66 | + request.publicKeyJson, |
| 67 | + request.clientDataHash |
| 68 | + ) |
| 69 | + ) |
| 70 | + .setOrigin(ORIGIN) |
| 71 | + .build(), |
| 72 | + ) |
| 73 | + return (response.credential as PublicKeyCredential).authenticationResponseJson |
| 74 | + } |
| 75 | + |
| 76 | + private enum class Operation(val webAuthnType: String) { |
| 77 | + Register("webauthn.create"), |
| 78 | + Login("webauthn.get"), |
| 79 | + } |
| 80 | + |
| 81 | + private class CredentialRequest( |
| 82 | + val operation: Operation, |
| 83 | + val publicKeyJson: String, |
| 84 | + val clientDataJson: String, |
| 85 | + ) { |
| 86 | + val clientDataHash: ByteArray = MessageDigest.getInstance("SHA-256") |
| 87 | + .digest(clientDataJson.toByteArray(Charsets.UTF_8)) |
| 88 | + |
| 89 | + companion object { |
| 90 | + fun parse(options: String, operation: Operation): CredentialRequest { |
| 91 | + val publicKey = JSONObject(options).getJSONObject("publicKey") |
| 92 | + val relyingParty = when (operation) { |
| 93 | + Operation.Register -> publicKey.getJSONObject("rp").getString("id") |
| 94 | + Operation.Login -> publicKey.getString("rpId") |
| 95 | + } |
| 96 | + require(relyingParty == RELYING_PARTY) |
| 97 | + val clientData = JSONObject().apply { |
| 98 | + put("type", operation.webAuthnType) |
| 99 | + put("challenge", publicKey.getString("challenge")) |
| 100 | + put("origin", ORIGIN) |
| 101 | + }.toString() |
| 102 | + return CredentialRequest(operation, publicKey.toString(), clientData) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private class CredentialResponse(json: String) { |
| 108 | + private val credential = JSONObject(json) |
| 109 | + private val response = credential.getJSONObject("response") |
| 110 | + |
| 111 | + fun toCredential(request: CredentialRequest): PasskeyCredential { |
| 112 | + val id = credential.getString("id") |
| 113 | + val rawId = credential.getString("rawId") |
| 114 | + val returnedClientData = response.optString("clientDataJSON") |
| 115 | + if (returnedClientData.isNotEmpty() && returnedClientData != "<placeholder>") { |
| 116 | + val returnedBytes = Base64.decode(returnedClientData, Base64.URL_SAFE or Base64.NO_WRAP) |
| 117 | + if (!MessageDigest.isEqual( |
| 118 | + MessageDigest.getInstance("SHA-256").digest(returnedBytes), |
| 119 | + request.clientDataHash, |
| 120 | + )) throw ClientDataMismatchException() |
| 121 | + } |
| 122 | + return when (request.operation) { |
| 123 | + Operation.Register -> PasskeyCredential( |
| 124 | + id = id, |
| 125 | + rawId = rawId, |
| 126 | + clientData = request.clientDataJson, |
| 127 | + attestationData = bytes("attestationObject"), |
| 128 | + ) |
| 129 | + |
| 130 | + Operation.Login -> PasskeyCredential( |
| 131 | + id = id, |
| 132 | + rawId = rawId, |
| 133 | + clientData = request.clientDataJson, |
| 134 | + authenticatorData = bytes("authenticatorData"), |
| 135 | + signature = bytes("signature"), |
| 136 | + userHandle = bytes("userHandle").toString(Charsets.UTF_8), |
| 137 | + ) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private fun bytes(name: String): ByteArray = |
| 142 | + Base64.decode(response.getString(name), Base64.URL_SAFE or Base64.NO_WRAP) |
| 143 | + } |
| 144 | +} |
0 commit comments