refactor: SignatureBaseを抽象化
This commit is contained in:
parent
db0ff2c825
commit
9f02f19ca7
src/main/kotlin/dev/usbharu/httpsignature/common
|
@ -0,0 +1,39 @@
|
||||||
|
package dev.usbharu.httpsignature.common
|
||||||
|
|
||||||
|
class DefaultSignatureBase : SignatureBase {
|
||||||
|
|
||||||
|
private val list = mutableListOf<Component>()
|
||||||
|
|
||||||
|
fun addComponent(component: Component) {
|
||||||
|
if (list.indexOf(component) != -1) {
|
||||||
|
throw IllegalArgumentException("Component with identifier ${component.componentIdentifier} already exists.")
|
||||||
|
}
|
||||||
|
list.add(component)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun generateSignatureBase(signatureParameters: List<SignatureParameter>): String {
|
||||||
|
val signatureBase =
|
||||||
|
list.joinToString(
|
||||||
|
separator = "",
|
||||||
|
postfix = "\n"
|
||||||
|
) { component -> "${component.componentIdentifier}: ${component.componentValue}" }
|
||||||
|
|
||||||
|
val signatureParams = "\"@signature-params\": " + generateSignatureParameterString(signatureParameters)
|
||||||
|
|
||||||
|
return signatureBase + signatureParams
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun generateSignatureParameterString(signatureParameters: List<SignatureParameter>): String {
|
||||||
|
return (listOf(
|
||||||
|
list.joinToString(
|
||||||
|
" ",
|
||||||
|
"(",
|
||||||
|
")"
|
||||||
|
) { it.componentIdentifier }
|
||||||
|
) + signatureParameters.map { "${it.name}=${it.value}" }).joinToString(";")
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun coveredComponents(): List<String> {
|
||||||
|
return list.map { it.componentIdentifier }
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,39 +1,7 @@
|
||||||
package dev.usbharu.httpsignature.common
|
package dev.usbharu.httpsignature.common
|
||||||
|
|
||||||
class SignatureBase() {
|
interface SignatureBase {
|
||||||
|
fun generateSignatureBase(signatureParameters: List<SignatureParameter>): String
|
||||||
private val list = mutableListOf<Component>()
|
fun generateSignatureParameterString(signatureParameters: List<SignatureParameter>): String
|
||||||
|
fun coveredComponents(): List<String>
|
||||||
fun addComponent(component: Component) {
|
|
||||||
if (list.indexOf(component) != -1) {
|
|
||||||
throw IllegalArgumentException("Component with identifier ${component.componentIdentifier} already exists.")
|
|
||||||
}
|
|
||||||
list.add(component)
|
|
||||||
}
|
|
||||||
|
|
||||||
fun generateSignatureBase(signatureParameters: List<SignatureParameter>): String {
|
|
||||||
val signatureBase =
|
|
||||||
list.joinToString(
|
|
||||||
separator = "",
|
|
||||||
postfix = "\n"
|
|
||||||
) { component -> "${component.componentIdentifier}: ${component.componentValue}" }
|
|
||||||
|
|
||||||
val signatureParams = "\"@signature-params\": " + generateSignatureParameterString(signatureParameters)
|
|
||||||
|
|
||||||
return signatureBase + signatureParams
|
|
||||||
}
|
|
||||||
|
|
||||||
fun generateSignatureParameterString(signatureParameters: List<SignatureParameter>): String {
|
|
||||||
return (listOf(
|
|
||||||
list.joinToString(
|
|
||||||
" ",
|
|
||||||
"(",
|
|
||||||
")"
|
|
||||||
) { it.componentIdentifier }
|
|
||||||
) + signatureParameters.map { "${it.name}=${it.value}" }).joinToString(";")
|
|
||||||
}
|
|
||||||
|
|
||||||
fun coveredComponents(): List<String> {
|
|
||||||
return list.map { it.componentIdentifier }
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -5,9 +5,9 @@ import java.net.http.HttpRequest
|
||||||
import kotlin.jvm.optionals.getOrNull
|
import kotlin.jvm.optionals.getOrNull
|
||||||
|
|
||||||
class SignatureBaseBuilder {
|
class SignatureBaseBuilder {
|
||||||
private val signatureBase = SignatureBase()
|
private val signatureBase = DefaultSignatureBase()
|
||||||
|
|
||||||
fun build(): SignatureBase {
|
fun build(): DefaultSignatureBase {
|
||||||
return signatureBase
|
return signatureBase
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue