Skip to content

Commit

Permalink
Added encryption properties
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 ed65bbb commit 6740daf
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 8 deletions.
40 changes: 40 additions & 0 deletions src/main/kotlin/at/bitfire/dav4jvm/property/push/AuthSecret.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package at.bitfire.dav4jvm.property.push

import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.PropertyFactory
import at.bitfire.dav4jvm.XmlReader
import org.xmlpull.v1.XmlPullParser

/**
* Represents a `{DAV:Push}auth-secret` property.
*
* Experimental! See https://github.com/bitfireAT/webdav-push/
*/
class AuthSecret private constructor(
val secret: String?
): Property {

companion object {

@JvmField
val NAME = Property.Name(NS_WEBDAV_PUSH, "auth-secret")

}


object Factory: PropertyFactory {

override fun getName() = NAME

override fun create(parser: XmlPullParser): AuthSecret =
AuthSecret(XmlReader(parser).readText())

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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}client-public-key` property.
*
* Experimental! See https://github.com/bitfireAT/webdav-push/
*/
data class ClientPublicKey(
val type: String?,
val value: String?
): Property {

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)
}

}

}
40 changes: 40 additions & 0 deletions src/main/kotlin/at/bitfire/dav4jvm/property/push/PushResource.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

package at.bitfire.dav4jvm.property.push

import at.bitfire.dav4jvm.Property
import at.bitfire.dav4jvm.PropertyFactory
import at.bitfire.dav4jvm.XmlReader
import org.xmlpull.v1.XmlPullParser

/**
* Represents a `{DAV:Push}push-resource` property.
*
* Experimental! See https://github.com/bitfireAT/webdav-push/
*/
class PushResource private constructor(
val resource: String?
): Property {

companion object {

@JvmField
val NAME = Property.Name(NS_WEBDAV_PUSH, "push-resource")

}


object Factory: PropertyFactory {

override fun getName() = NAME

override fun create(parser: XmlPullParser): PushResource =
PushResource(XmlReader(parser).readText())

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ 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

/**
Expand All @@ -23,22 +24,36 @@ class WebPushSubscription: Property {
@JvmField
val NAME = Property.Name(NS_WEBDAV_PUSH, "web-push-subscription")

val PUSH_RESOURCE = Property.Name(NS_WEBDAV_PUSH, "push-resource")

}

var pushResource: String? = null
var pushResource: PushResource? = null
var clientPublicKey: ClientPublicKey? = null
var authSecret: AuthSecret? = null


object Factory: PropertyFactory {

override fun getName() = NAME

override fun create(parser: XmlPullParser) =
WebPushSubscription().apply {
pushResource = XmlReader(parser).readTextProperty(PUSH_RESOURCE)
override fun create(parser: XmlPullParser): WebPushSubscription {
val subscription = WebPushSubscription()

val depth = parser.depth
var eventType = parser.eventType
while (!(eventType == XmlPullParser.END_TAG && parser.depth == depth)) {
if (eventType == XmlPullParser.START_TAG && parser.depth == depth + 1) {
when (parser.propertyName()) {
PushResource.NAME -> subscription.pushResource = PushResource.Factory.create(parser)
ClientPublicKey.NAME -> subscription.clientPublicKey = ClientPublicKey.Factory.create(parser)
AuthSecret.NAME -> subscription.authSecret = AuthSecret.Factory.create(parser)
}
}
eventType = parser.next()
}

return subscription
}

}

}
}
2 changes: 1 addition & 1 deletion src/test/kotlin/at/bitfire/dav4jvm/property/WebPushTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class WebPushTest: PropertyTest() {
"</push-register>")
val result = results.first() as PushRegister
assertEquals(Instant.ofEpochSecond(1703066611), result.expires)
assertEquals("https://up.example.net/yohd4yai5Phiz1wi", result.subscription?.webPushSubscription?.pushResource)
assertEquals("https://up.example.net/yohd4yai5Phiz1wi", result.subscription?.webPushSubscription?.pushResource?.resource)
}

@Test
Expand Down

0 comments on commit 6740daf

Please sign in to comment.