From 0a111292d43d1b07c42ed4f43f1b63223fbfe7c2 Mon Sep 17 00:00:00 2001 From: Arnau Mora Gras Date: Mon, 18 Nov 2024 08:54:00 +0100 Subject: [PATCH] Added generic public key class Signed-off-by: Arnau Mora Gras --- .../dav4jvm/property/push/ClientPublicKey.kt | 22 +------ .../dav4jvm/property/push/PushPublicKey.kt | 65 +++++++++++++++++++ .../dav4jvm/property/push/ServerPublicKey.kt | 24 +------ .../bitfire/dav4jvm/property/WebPushTest.kt | 12 +++- 4 files changed, 78 insertions(+), 45 deletions(-) create mode 100644 src/main/kotlin/at/bitfire/dav4jvm/property/push/PushPublicKey.kt diff --git a/src/main/kotlin/at/bitfire/dav4jvm/property/push/ClientPublicKey.kt b/src/main/kotlin/at/bitfire/dav4jvm/property/push/ClientPublicKey.kt index 2b15c15..c20bc7d 100644 --- a/src/main/kotlin/at/bitfire/dav4jvm/property/push/ClientPublicKey.kt +++ b/src/main/kotlin/at/bitfire/dav4jvm/property/push/ClientPublicKey.kt @@ -9,29 +9,11 @@ import org.xmlpull.v1.XmlPullParser * * Experimental! See https://github.com/bitfireAT/webdav-push/ */ -data class ClientPublicKey( - val type: String?, - val value: String? -): Property { - +class ClientPublicKey: PushPublicKey() { companion object { @JvmField val NAME = Property.Name(NS_WEBDAV_PUSH, "client-public-key") - - const val PROP_TYPE = "type" - } - - object Factory: PropertyFactory { - - override fun getName() = PushTransports.NAME - - override fun create(parser: XmlPullParser): ClientPublicKey { - val type = parser.getAttributeValue(NS_WEBDAV_PUSH, PROP_TYPE) - val value = parser.text - - return ClientPublicKey(type, value) - } - } + object Factory : PushPublicKey.Factory(NAME, ::ClientPublicKey) } diff --git a/src/main/kotlin/at/bitfire/dav4jvm/property/push/PushPublicKey.kt b/src/main/kotlin/at/bitfire/dav4jvm/property/push/PushPublicKey.kt new file mode 100644 index 0000000..9acbe5d --- /dev/null +++ b/src/main/kotlin/at/bitfire/dav4jvm/property/push/PushPublicKey.kt @@ -0,0 +1,65 @@ +package at.bitfire.dav4jvm.property.push + +import at.bitfire.dav4jvm.Property +import at.bitfire.dav4jvm.PropertyFactory +import at.bitfire.dav4jvm.XmlReader +import at.bitfire.dav4jvm.XmlUtils.propertyName +import org.xmlpull.v1.XmlPullParser + +/** + * Represents a public key property from Push. + * + * Experimental! See https://github.com/bitfireAT/webdav-push/ + * + * @see ClientPublicKey + * @see ServerPublicKey + */ +abstract class PushPublicKey: Property { + + companion object { + @JvmField + val NAME = Property.Name(NS_WEBDAV_PUSH, "server-public-key") + + val PROP_TYPE = Property.Name(NS_WEBDAV_PUSH, "type") + } + + var type: String? = null + var key: String? = null + + + override fun equals(other: Any?): Boolean { + if (this === other) return true + if (other !is PushPublicKey) return false + + if (type != other.type) return false + if (key != other.key) return false + + return true + } + + override fun hashCode(): Int { + var result = type?.hashCode() ?: 0 + result = 31 * result + (key?.hashCode() ?: 0) + return result + } + + + abstract class Factory( + private val name: Property.Name, + private val constructor: () -> KeyType + ): PropertyFactory { + + override fun getName() = name + + override fun create(parser: XmlPullParser): KeyType { + val publicKey = constructor() + + publicKey.type = parser.getAttributeValue(null, "type") + publicKey.key = XmlReader(parser).readText() + + return publicKey + } + + } + +} diff --git a/src/main/kotlin/at/bitfire/dav4jvm/property/push/ServerPublicKey.kt b/src/main/kotlin/at/bitfire/dav4jvm/property/push/ServerPublicKey.kt index 33cadc3..db3ccdf 100644 --- a/src/main/kotlin/at/bitfire/dav4jvm/property/push/ServerPublicKey.kt +++ b/src/main/kotlin/at/bitfire/dav4jvm/property/push/ServerPublicKey.kt @@ -1,37 +1,17 @@ package at.bitfire.dav4jvm.property.push import at.bitfire.dav4jvm.Property -import at.bitfire.dav4jvm.PropertyFactory -import org.xmlpull.v1.XmlPullParser /** * Represents a `{DAV:Push}server-public-key` property. * * Experimental! See https://github.com/bitfireAT/webdav-push/ */ -data class ServerPublicKey( - val type: String?, - val value: String? -): Property { - +class ServerPublicKey: PushPublicKey() { companion object { @JvmField val NAME = Property.Name(NS_WEBDAV_PUSH, "server-public-key") - - const val PROP_TYPE = "type" - } - - object Factory: PropertyFactory { - - override fun getName() = PushTransports.NAME - - override fun create(parser: XmlPullParser): ServerPublicKey { - val type = parser.getAttributeValue(NS_WEBDAV_PUSH, PROP_TYPE) - val value = parser.text - - return ServerPublicKey(type, value) - } - } + object Factory : PushPublicKey.Factory(NAME, ::ServerPublicKey) } diff --git a/src/test/kotlin/at/bitfire/dav4jvm/property/WebPushTest.kt b/src/test/kotlin/at/bitfire/dav4jvm/property/WebPushTest.kt index d24280d..689b0f3 100644 --- a/src/test/kotlin/at/bitfire/dav4jvm/property/WebPushTest.kt +++ b/src/test/kotlin/at/bitfire/dav4jvm/property/WebPushTest.kt @@ -1,6 +1,5 @@ package at.bitfire.dav4jvm.property -import at.bitfire.dav4jvm.Property import at.bitfire.dav4jvm.property.push.* import at.bitfire.dav4jvm.property.webdav.SyncToken import okhttp3.Protocol @@ -8,7 +7,6 @@ import okhttp3.internal.http.StatusLine import org.junit.Assert.* import org.junit.Test import java.time.Instant -import kotlin.math.exp class WebPushTest: PropertyTest() { @@ -19,13 +17,21 @@ class WebPushTest: PropertyTest() { " " + " \n" + " https://up.example.net/yohd4yai5Phiz1wi\n" + + " BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4\n" + + " BTBZMqHH6r4Tts7J_aSIgg" + " \n" + " " + " Wed, 20 Dec 2023 10:03:31 GMT" + "") val result = results.first() as PushRegister assertEquals(Instant.ofEpochSecond(1703066611), result.expires) - assertEquals("https://up.example.net/yohd4yai5Phiz1wi", result.subscription?.webPushSubscription?.pushResource?.resource) + val subscription = result.subscription?.webPushSubscription + assertEquals("https://up.example.net/yohd4yai5Phiz1wi", subscription?.pushResource?.resource) + assertEquals("BTBZMqHH6r4Tts7J_aSIgg", subscription?.authSecret?.secret) + + val publicKey = subscription?.clientPublicKey + assertEquals("p256dh", publicKey?.type) + assertEquals("BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4", publicKey?.key) } @Test