Skip to content

Commit

Permalink
Tar i bruk PayloadRequest og PayloadResponse i payload endepunkt
Browse files Browse the repository at this point in the history
  • Loading branch information
thburnett committed Sep 20, 2023
1 parent 86c3a3a commit b7ffb21
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 42 deletions.
28 changes: 6 additions & 22 deletions payload-processor/src/main/kotlin/no/nav/emottak/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private fun Application.serverSetup() {

get("/payload/test") {
val testByteArray = this::class.java.classLoader.getResource("xml/test.xml").readBytes()
val melding = Melding(
val request = PayloadRequest(
header = Header(
messageId = UUID.randomUUID().toString(),
conversationId = UUID.randomUUID().toString(),
Expand All @@ -63,34 +63,18 @@ private fun Application.serverSetup() {
service = "melding",
action = "send"
),
originalPayload = testByteArray,
processedPayload = testByteArray
payload = testByteArray
)
val prosessertMelding = processor.processOutgoing(melding)
call.respond(prosessertMelding)
val response = processor.processOutgoing(request)
call.respond(response)
}

post("/payload") {

val request: PayloadRequest = call.receive(PayloadRequest::class)
println(Json.encodeToString(PayloadRequest.serializer(),request))
call.respondText("Hello")
}

post("/payload/incoming") {
call.receiveMultipart().forEachPart {
print(it is PartData.BinaryItem)
print(it is PartData.FileItem)
}
call.respondText("Hello")
val response = processor.process(request)
call.respond(response)
}

post("/payload/outgoing") {
call.receiveMultipart().forEachPart {
print(it is PartData.BinaryItem)
print(it is PartData.FileItem)
}
call.respondText("Hello")
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package no.nav.emottak.melding

import no.nav.emottak.melding.model.Melding
import no.nav.emottak.melding.model.PayloadRequest
import no.nav.emottak.melding.model.PayloadResponse
import no.nav.emottak.melding.process.dekomprimer
import no.nav.emottak.melding.process.dekrypter
import no.nav.emottak.melding.process.komprimer
Expand All @@ -10,19 +12,38 @@ import no.nav.emottak.melding.process.verifiserSignatur

class Processor {

fun processIncoming(melding: Melding): Melding {
return melding
fun process(payloadRequest: PayloadRequest): PayloadResponse {
return if (payloadRequest.isIncomingMessage()) {
processIncoming(payloadRequest)
} else {
processOutgoing(payloadRequest)
}
}

private fun processIncoming(payloadRequest: PayloadRequest): PayloadResponse {
val melding = Melding(payloadRequest)
.dekrypter()
.dekomprimer()
//.verifiserXML()
.verifiserSignatur()
return PayloadResponse(
melding.processedPayload
)
}

fun processOutgoing(melding: Melding): Melding {
return melding
fun processOutgoing(payloadRequest: PayloadRequest): PayloadResponse {
val melding = Melding(payloadRequest)
//.verifiserXML()
.signer()
.komprimer()
.krypter()
return PayloadResponse(
melding.processedPayload
)
}
}

fun PayloadRequest.isIncomingMessage(): Boolean {
//TODO
return true
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,12 @@ data class Melding(
val sertifikatSjekket: Boolean = false,
val komprimert: Boolean = false,
val dekomprimert: Boolean = false
)
) {
constructor(payloadRequest: PayloadRequest) : this(
header = payloadRequest.header,
originalPayload = payloadRequest.payload,
processedPayload = payloadRequest.payload)
}

@Serializable
data class PayloadRequest(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package no.nav.emottak.melding.process
import io.ktor.server.plugins.BadRequestException
import no.nav.emottak.melding.model.Header
import no.nav.emottak.melding.model.Melding
import no.nav.emottak.util.sertifikat.createX509Certificate
import no.nav.emottak.util.hentKrypteringssertifikat
import org.bouncycastle.asn1.ASN1ObjectIdentifier
import org.bouncycastle.cms.CMSAlgorithm
Expand All @@ -13,10 +14,7 @@ import org.bouncycastle.cms.CMSTypedData
import org.bouncycastle.cms.jcajce.JceCMSContentEncryptorBuilder
import org.bouncycastle.cms.jcajce.JceKeyTransRecipientInfoGenerator
import org.bouncycastle.jce.provider.BouncyCastleProvider
import java.io.ByteArrayInputStream
import java.security.cert.CertificateEncodingException
import java.security.cert.CertificateException
import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate

private val kryptering = Kryptering()
Expand Down Expand Up @@ -82,12 +80,3 @@ private fun krypterDokument(input: ByteArray, certificates: List<X509Certificate
throw e
}
}

private fun createX509Certificate(certificate: ByteArray): X509Certificate {
val cf = CertificateFactory.getInstance("X.509")
return try {
cf.generateCertificate(ByteArrayInputStream(certificate)) as X509Certificate
} catch (e: CertificateException) {
throw e
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.ktor.server.plugins.BadRequestException
import no.nav.emottak.melding.model.Melding
import no.nav.emottak.util.createValidateContext
import no.nav.emottak.util.retrieveXMLSignature
import no.nav.emottak.util.validateAlgorithms
import no.nav.emottak.util.signatur.validateAlgorithms
import no.nav.emottak.util.createDocument
import no.nav.emottak.util.getByteArrayFromDocument
import org.w3c.dom.Document
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.emottak.util
package no.nav.emottak.util.signatur

import io.ktor.server.plugins.BadRequestException
import javax.xml.crypto.dsig.DigestMethod
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package no.nav.emottak.util
package no.nav.emottak.util.signatur

import java.security.Key
import java.security.KeyException
Expand Down

0 comments on commit b7ffb21

Please sign in to comment.