Skip to content

Commit

Permalink
Added generic public key class
Browse files Browse the repository at this point in the history
Signed-off-by: Arnau Mora Gras <[email protected]>
  • Loading branch information
ArnyminerZ committed Nov 18, 2024
1 parent 6740daf commit 0a11129
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClientPublicKey>(NAME, ::ClientPublicKey)
}
65 changes: 65 additions & 0 deletions src/main/kotlin/at/bitfire/dav4jvm/property/push/PushPublicKey.kt
Original file line number Diff line number Diff line change
@@ -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<KeyType: PushPublicKey>(
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
}

}

}
Original file line number Diff line number Diff line change
@@ -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<ServerPublicKey>(NAME, ::ServerPublicKey)
}
12 changes: 9 additions & 3 deletions src/test/kotlin/at/bitfire/dav4jvm/property/WebPushTest.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
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
import okhttp3.internal.http.StatusLine
import org.junit.Assert.*
import org.junit.Test
import java.time.Instant
import kotlin.math.exp

class WebPushTest: PropertyTest() {

Expand All @@ -19,13 +17,21 @@ class WebPushTest: PropertyTest() {
" <subscription>" +
" <web-push-subscription>\n" +
" <push-resource>https://up.example.net/yohd4yai5Phiz1wi</push-resource>\n" +
" <client-public-key type=\"p256dh\">BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4</client-public-key>\n" +
" <auth-secret>BTBZMqHH6r4Tts7J_aSIgg</auth-secret>" +
" </web-push-subscription>\n" +
" </subscription>" +
" <expires>Wed, 20 Dec 2023 10:03:31 GMT</expires>" +
"</push-register>")
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
Expand Down

0 comments on commit 0a11129

Please sign in to comment.