Skip to content

Commit

Permalink
parsing av FailSignal
Browse files Browse the repository at this point in the history
  • Loading branch information
alpet committed Oct 24, 2023
1 parent 1ca934f commit 9d29ae9
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 38 deletions.
26 changes: 24 additions & 2 deletions ebms-provider/src/main/kotlin/no/nav/emottak/ebms/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package no.nav.emottak.ebms

import com.sun.tools.xjc.util.DOMUtils
import io.ktor.http.*
import io.ktor.http.content.*
import io.ktor.serialization.kotlinx.json.json
Expand All @@ -13,15 +14,19 @@ import io.ktor.server.plugins.contentnegotiation.ContentNegotiation
import io.ktor.server.request.*
import io.ktor.server.response.*
import io.ktor.server.routing.*
import io.ktor.util.*
import no.nav.emottak.ebms.model.*
import no.nav.emottak.ebms.processing.ProcessingService
import no.nav.emottak.ebms.validation.DokumentValidator
import no.nav.emottak.ebms.validation.MimeHeaders
import no.nav.emottak.ebms.validation.MimeValidationException
import no.nav.emottak.ebms.validation.asParseAsSoapFault
import no.nav.emottak.ebms.validation.validateMime
import no.nav.emottak.ebms.validation.validateMimeAttachment
import no.nav.emottak.ebms.validation.validateMimeSoapEnvelope
import no.nav.emottak.ebms.xml.asString
import no.nav.emottak.ebms.xml.getDocumentBuilder
import no.nav.emottak.ebms.xml.xmlMarshaller
import no.nav.emottak.util.marker
import java.io.ByteArrayInputStream

Expand Down Expand Up @@ -72,8 +77,16 @@ fun Application.myApplicationModule() {
}
try {
DokumentValidator().validate(ebMSDocument)
}catch(ex:Exception) {
call.respond(HttpStatusCode.InternalServerError,"Validation feilet")
}catch(ex:MimeValidationException) {
call.respond(HttpStatusCode.InternalServerError,ex.asParseAsSoapFault())
}catch (ex2:Exception) {
ebMSDocument
.createFail(EbMSError().createError(EbMSError.Code.OTHER_XML.name,"Validation failed"))
.toEbmsDokument()
.also {
call.respondEbmsDokument(it)
return@post
}
}

val message = ebMSDocument.buildEbmMessage()
Expand Down Expand Up @@ -135,3 +148,12 @@ suspend fun ApplicationCall.receiveEbmsDokument() : EbMSDocument {
}
}
}

suspend fun ApplicationCall.respondEbmsDokument(ebmsDokument:EbMSDocument) {

val payload = ebmsDokument
.dokument
.asString()
// .encodeBase64()
this.respondText(payload , ContentType.parse("text/xml"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import no.nav.emottak.melding.model.PayloadResponse
import no.nav.emottak.melding.model.SignatureDetails
import no.nav.emottak.melding.model.SignatureDetailsRequest
import no.nav.emottak.melding.model.ValidationResult
import no.nav.emottak.util.decodeBase64
import org.apache.xml.security.algorithms.MessageDigestAlgorithm
import org.apache.xml.security.signature.XMLSignature
import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.MessageHeader


Expand All @@ -39,6 +42,18 @@ suspend fun getPublicSigningDetails(cpaId: String, partyType: String, partyId: S
service = service,
action = action
)
val debug = true
if (debug) {
val cert = decodeBase64("MIIGKzCCBBOgAwIBAgILAZV/ETITzRpPW2AwDQYJKoZIhvcNAQELBQAwbjELMAkGA1UEBhMCTk8xGDAWBgNVBGEMD05UUk5PLTk4MzE2MzMyNzETMBEGA1UECgwKQnV5cGFzcyBBUzEwMC4GA1UEAwwnQnV5cGFzcyBDbGFzcyAzIFRlc3Q0IENBIEcyIFNUIEJ1c2luZXNzMB4XDTIyMDkyMjExMzQxN1oXDTI1MDkyMjIxNTkwMFowTzELMAkGA1UEBhMCTk8xEjAQBgNVBAoMCVNQRUFSRSBBUzESMBAGA1UEAwwJU1BFQVJFIEFTMRgwFgYDVQRhDA9OVFJOTy05OTM5NTQ4OTYwggGiMA0GCSqGSIb3DQEBAQUAA4IBjwAwggGKAoIBgQCwHoYUs81oVde0a8JgduNSSxeNaDs3kUleGjRApc+kz7tc7k386zXenFxnvIwNaVGdHVs3dN5O06h5QlG7rlFsxR+Btz6oFFwi/5WcAtDxJj4XRVL0evLXZY86D8TmAtMgdTQvRZ39jfPpkBW5kxIPi7DomS0/Bis2vsyy1Abr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Oi4SScJZOtV+HwtV8GXKDfPB8SKIKjpAWF1sqijn3T45cLWDn87teaVtURCu+VrxWuvb48RJBPotf3JpHBzKeAQfOdxVLD2VuDI9EtC77ZvGWbY2ve9Va99pZ7z1iXLvXiqjcm+4AKNtjgnLcVBEYw1DZBM/0ZaRv2o4PK5mX/faGeA0zCQa1dd8BkkUW6AvLFHUR2QEwcbhd78PR5wtbqoA+C945HK6u74VDYlpMQSO5JtKdZlgoscuf4RRhPkDAPUkKtwcL3jO6ep4yr958xL+EVYd9tKpbmGArXwD9JlEkfURMi06iHXkQKiwEQ26hrNcd4snBjsvtqWm6A0BhGToLhXTYJNfTYZNh5CG10C7IzBGzFqwG+ZQmeu1RV4ltIiJQWn6NO32fFi5pSkfJ04O+W6hsaFiIMH7khgaGYdV32zfHP34Pj1sfjUoWmKIyU1J3gifWnidhZgFNx+senCTMBHYHU=".toByteArray())
//val timestamp = messageHeader.messageData.timestamp
//SecurityUtils.validateCertificate(trustStore, certificate, timestamp)
return SignatureDetails(
certificate = cert,
signatureAlgorithm = XMLSignature.ALGO_ID_SIGNATURE_RSA_SHA256,
hashFunction = MessageDigestAlgorithm.ALGO_ID_DIGEST_SHA256
)
}

return httpClientUtil.postSignatureDetailsRequest("$cpaRepoEndpoint/signing/certificate", request)
}

Expand Down
30 changes: 27 additions & 3 deletions ebms-provider/src/main/kotlin/no/nav/emottak/ebms/MessageUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,32 @@
*/
package no.nav.emottak.ebms

class MessageUtils {
companion object {
fun getMessage(): String = "Hello World!"
import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.From
import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.MessageData
import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.MessageHeader
import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.Service
import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.To
import java.time.Instant
import java.util.*

fun MessageHeader.createResponseHeader(newFromRole: String, newToRole: String, newAction: String?, newService: String?): MessageHeader {
val messageHeader = MessageHeader()
messageHeader.conversationId = this.conversationId
messageHeader.from = From().also {
it.partyId.addAll(this.to.partyId)
it.role = newFromRole
}
messageHeader.to = To().also {
it.partyId.addAll(this.from.partyId)
it.role = newToRole
}
messageHeader.service = if (newService != null) Service().also { it.value = newService } else this.service
messageHeader.action = newAction ?: this.action
messageHeader.cpaId = this.cpaId
messageHeader.messageData = MessageData().also {
it.messageId = this.messageData.messageId + "_RESPONSE"
it.refToMessageId = this.messageData.messageId
it.timestamp = Date.from(Instant.now())
}
return messageHeader
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,14 @@
*/
package no.nav.emottak.ebms.model

import no.nav.emottak.EBMS_SERVICE_URI
import no.nav.emottak.ebms.createResponseHeader
import no.nav.emottak.ebms.processing.SignaturValidator
import no.nav.emottak.ebms.xml.xmlMarshaller
import no.nav.emottak.melding.model.SignatureDetails
import no.nav.emottak.util.marker
import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.Error
import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.ErrorList
import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.MessageHeader
import org.slf4j.LoggerFactory
import org.w3c.dom.Document
Expand All @@ -36,6 +40,12 @@ data class EbMSDocument(val messageId: String, val dokument: Document, val attac
throw RuntimeException("Unrecognized dokument type")

}

fun createFail(error: Error): EbMSMessageError {
return EbMSMessageError(this.messageHeader().createResponseHeader(newFromRole = "ERROR_RESPONDER", newToRole = "ERROR_RECEIVER", newAction = "MessageError", newService = EBMS_SERVICE_URI), ErrorList().also {
it.error.add(error)
})
}
fun messageHeader():MessageHeader {
val node: Node =this.dokument.getElementsByTagName("eb:MessageHeader").item(0)
return xmlMarshaller.unmarshal(node)
Expand All @@ -52,6 +62,7 @@ fun EbMSDocument.sjekkSignature(signatureDetails: SignatureDetails) {
}



fun EbMSDocument.buildEbmMessage(): EbMSBaseMessage {
val envelope: Envelope = xmlMarshaller.unmarshal( this.dokument)
val header = envelope.header
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package no.nav.emottak.ebms.model

import no.nav.emottak.EBMS_SERVICE_URI
import no.nav.emottak.Event
import no.nav.emottak.ebms.createResponseHeader
import no.nav.emottak.ebms.processing.DekrypteringProcessor
import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.AckRequested
import org.oasis_open.committees.ebxml_msg.schema.msg_header_2_0.Acknowledgment
Expand Down Expand Up @@ -80,26 +81,4 @@ class EbMSPayloadMessage(
}
}

}

fun MessageHeader.createResponseHeader(newFromRole: String, newToRole: String, newAction: String?, newService: String?): MessageHeader {
val messageHeader = MessageHeader()
messageHeader.conversationId = this.conversationId
messageHeader.from = From().also {
it.partyId.addAll(this.to.partyId)
it.role = newFromRole
}
messageHeader.to = To().also {
it.partyId.addAll(this.from.partyId)
it.role = newToRole
}
messageHeader.service = if (newService != null) Service().also { it.value = newService } else this.service
messageHeader.action = newAction ?: this.action
messageHeader.cpaId = this.cpaId
messageHeader.messageData = MessageData().also {
it.messageId = this.messageData.messageId + "_RESPONSE"
it.refToMessageId = this.messageData.messageId
it.timestamp = Date.from(Instant.now())
}
return messageHeader
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,7 @@ class EbMSMessageError(
override val dokument: Document? = null
) : EbMSBaseMessage {

fun process() {
try {

}catch (ex: Exception) {
return
}
}

fun toEbmsDokument(): EbMSDocument {
log.warn(this.messageHeader.marker(), "Oppretter ErrorList")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ class ProcessingService() {
}
}

private fun acknowledgment(acknowledgment: Acknowledgment) {
private fun acknowledgment(acknowledgment: EbmsAcknowledgment) {

}

private fun fail(fail:EbMSError) {
private fun fail(fail:EbMSMessageError) {

}

fun process(message: EbMSBaseMessage) {
when (message) {
is EbmsAcknowledgment -> message.process()
is EbMSMessageError -> message.process()
is EbMSPayloadMessage -> message.process()
is EbmsAcknowledgment -> acknowledgment(message)
is EbMSMessageError -> fail(message)
is EbMSPayloadMessage -> payloadMessage(message)
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions ebms-provider/src/main/kotlin/no/nav/emottak/ebms/xml/DomUtils.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
package no.nav.emottak.ebms.xml

import org.w3c.dom.Document
import java.io.StringWriter
import javax.xml.parsers.DocumentBuilder
import javax.xml.parsers.DocumentBuilderFactory
import javax.xml.parsers.ParserConfigurationException
import javax.xml.transform.Transformer
import javax.xml.transform.TransformerFactory
import javax.xml.transform.dom.DOMSource
import javax.xml.transform.stream.StreamResult


@Throws(ParserConfigurationException::class)
fun getDocumentBuilder(): DocumentBuilder {
Expand All @@ -15,4 +22,15 @@ fun getDocumentBuilder(): DocumentBuilder {
dbf.isExpandEntityReferences = false
dbf.isNamespaceAware = true
return dbf.newDocumentBuilder()
}



fun Document.asString() : String {
val domSource = DOMSource(this)
val transformer: Transformer = TransformerFactory.newInstance().newTransformer()
val sw = StringWriter()
val sr = StreamResult(sw)
transformer.transform(domSource, sr)
return sw.toString()
}

0 comments on commit 9d29ae9

Please sign in to comment.