115 lines · 6.3 KB · KotlinRaw
package com.cottondb.keysimport com.cottondb.keys.provider.Sitesimport com.cottondb.keys.provider.WebAuthnimport kotlinx.serialization.json.Jsonimport kotlinx.serialization.json.JsonObjectimport kotlinx.serialization.json.JsonPrimitiveimport kotlinx.serialization.json.buildJsonObjectimport kotlinx.serialization.json.jsonObjectimport kotlinx.serialization.json.jsonPrimitiveimport kotlinx.serialization.json.putimport org.junit.Assert.assertArrayEqualsimport org.junit.Assert.assertEqualsimport org.junit.Assert.assertFalseimport org.junit.Assert.assertTrueimport org.junit.Testimport java.io.File/** The provider's WebAuthn JSON and its sense of sites, held to the spec's shapes with invented sites and ids. */class KeysProviderTest { private val creation = """ {"rp":{"id":"passkey.example","name":"Passkey Example"}, "user":{"id":"c3ludGhldGljLXVzZXI","name":"ada@passkey.example","displayName":"Ada"}, "challenge":"Y2hhbGxlbmdl","pubKeyCredParams":[{"type":"public-key","alg":-8},{"type":"public-key","alg":-7}], "excludeCredentials":[{"type":"public-key","id":"AAAA"}],"authenticatorSelection":{"residentKey":"required"}} """.trimIndent() @Test fun creation_options_are_read() { val options = WebAuthn.creation(creation) assertEquals("passkey.example", options.rpId) assertEquals("Passkey Example", options.rpName) assertEquals("c3ludGhldGljLXVzZXI", options.userId) assertEquals("ada@passkey.example", options.userName) assertEquals("Y2hhbGxlbmdl", options.challenge) assertEquals(listOf("AAAA"), options.exclude) assertTrue(options.es256) assertFalse(WebAuthn.creation(creation.replace("\"alg\":-7", "\"alg\":-257")).es256) } @Test fun request_options_are_read() { val asked = WebAuthn.request("""{"rpId":"passkey.example","challenge":"abc","allowCredentials":[{"type":"public-key","id":"QkJC"}],"userVerification":"preferred"}""") assertEquals("passkey.example", asked.rpId) assertEquals(listOf("QkJC"), asked.allow) } /** CollectedClientData: type, the challenge as it came, the app's origin and package; the hash is its SHA-256. */ @Test fun client_data_is_the_specs() { val data = WebAuthn.clientData("webauthn.get", "Y2hhbGxlbmdl", "android:apk-key-hash:abc", "com.example.app") val parsed = Json.parseToJsonElement(data.decodeToString()).jsonObject assertEquals("webauthn.get", parsed["type"]!!.jsonPrimitive.content) assertEquals("Y2hhbGxlbmdl", parsed["challenge"]!!.jsonPrimitive.content) assertEquals("android:apk-key-hash:abc", parsed["origin"]!!.jsonPrimitive.content) assertEquals("com.example.app", parsed["androidPackageName"]!!.jsonPrimitive.content) assertEquals(32, WebAuthn.sha256(data).size) val origin = WebAuthn.apkKeyHash(byteArrayOf(1, 2, 3)) assertTrue(origin.startsWith("android:apk-key-hash:")) assertArrayEquals(WebAuthn.sha256(byteArrayOf(1, 2, 3)), WebAuthn.unb64url(origin.removePrefix("android:apk-key-hash:"))) } @Test fun registration_and_assertion_carry_what_the_core_answered() { val created = buildJsonObject { put("credential_id", "Y3JlZA"); put("attestation_object", "YXR0"); put("authenticator_data", "YXV0aA"); put("public_key", "c3BraQ"); put("algorithm", -7) } val reg = Json.parseToJsonElement(WebAuthn.registration(created, "{}".toByteArray())).jsonObject assertEquals("Y3JlZA", reg.text("id")) assertEquals("Y3JlZA", reg.text("rawId")) assertEquals("public-key", reg.text("type")) assertEquals("platform", reg.text("authenticatorAttachment")) val response = reg["response"]!!.jsonObject assertEquals("YXR0", response.text("attestationObject")) assertEquals("c3BraQ", response.text("publicKey")) assertEquals(-7, response["publicKeyAlgorithm"]!!.jsonPrimitive.content.toInt()) assertEquals("e30", response.text("clientDataJSON")) val signed = buildJsonObject { put("credential_id", "Y3JlZA"); put("authenticator_data", "YXV0aA"); put("signature", "c2ln"); put("user_handle", "dXNlcg") } val got = Json.parseToJsonElement(WebAuthn.assertion(signed, "{}".toByteArray())).jsonObject assertEquals("c2ln", got["response"]!!.jsonObject.text("signature")) assertEquals("dXNlcg", got["response"]!!.jsonObject.text("userHandle")) assertFalse("no key in either", (reg.toString() + got.toString()).contains("private", ignoreCase = true)) } @Test fun a_site_reads_back_to_its_host() { // Which logins match a caller is the core's rule (keys.forService, cotton-core keys::autofill), tested there. assertEquals("example.com", Sites.host("https://www.example.com/login?x=1")) assertEquals("accounts.example.com", Sites.host("accounts.example.com")) assertEquals(null, Sites.host("localhost")) } /** Digital Asset Links: only a statement naming the app, its certificate and the login-credentials relation lets it in. */ @Test fun asset_links_name_the_app_and_its_certificate() { val links = """[{"relation":["delegate_permission/common.get_login_creds"], "target":{"namespace":"android_app","package_name":"com.example.app","sha256_cert_fingerprints":["AB:CD"]}}]""" assertTrue(Sites.allows(links, "com.example.app", listOf("ab:cd"))) assertFalse(Sites.allows(links, "com.example.other", listOf("AB:CD"))) assertFalse(Sites.allows(links, "com.example.app", listOf("EF:01"))) assertFalse(Sites.allows(links.replace("get_login_creds", "something_else"), "com.example.app", listOf("AB:CD"))) assertFalse(Sites.allows("not json", "com.example.app", listOf("AB:CD"))) assertEquals(32 * 3 - 1, Sites.fingerprint(byteArrayOf(9)).length) } /** The shipped privileged list is Google's shape and names Chrome. */ @Test fun the_privileged_list_names_chrome() { val repo = generateSequence(File("").absoluteFile) { it.parentFile }.first { File(it, "apps/registry.json").isFile } val list = File(repo, "apps/android/app/src/keys/assets/privileged_browsers.json").readText() assertTrue("com.android.chrome" in Sites.privilegedPackages(list)) } private fun JsonObject.text(key: String) = (this[key] as? JsonPrimitive)?.content}