gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] branch master updated: refactor package structure


From: gnunet
Subject: [libeufin] branch master updated: refactor package structure
Date: Mon, 04 Nov 2019 17:30:06 +0100

This is an automated email from the git hooks/post-receive script.

dold pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new 0800e6a  refactor package structure
0800e6a is described below

commit 0800e6a51177dcfa6d15c4ff6dca7362603c10d4
Author: Florian Dold <address@hidden>
AuthorDate: Mon Nov 4 17:30:03 2019 +0100

    refactor package structure
---
 .../{ => tech/libeufin/sandbox}/CryptoUtil.kt      |   9 +-
 .../main/kotlin/{ => tech/libeufin/sandbox}/DB.kt  |   2 +-
 .../kotlin/{ => tech/libeufin/sandbox}/JSON.kt     |   0
 .../kotlin/{ => tech/libeufin/sandbox}/Main.kt     |  13 +-
 .../kotlin/{ => tech/libeufin/sandbox}/XMLUtil.kt  | 166 +++++++++++----------
 .../libeufin/schema/ebics_h004/EbicsMessages.kt    |  16 ++
 sandbox/src/test/kotlin/XmlUtilTest.kt             |   6 +-
 7 files changed, 116 insertions(+), 96 deletions(-)

diff --git a/sandbox/src/main/kotlin/CryptoUtil.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/CryptoUtil.kt
similarity index 98%
rename from sandbox/src/main/kotlin/CryptoUtil.kt
rename to sandbox/src/main/kotlin/tech/libeufin/sandbox/CryptoUtil.kt
index b188fd4..adfa371 100644
--- a/sandbox/src/main/kotlin/CryptoUtil.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/CryptoUtil.kt
@@ -37,11 +37,6 @@ import javax.crypto.spec.IvParameterSpec
 import javax.crypto.spec.SecretKeySpec
 
 
-
-
-
-
-
 /**
  * RSA key pair.
  */
@@ -133,9 +128,9 @@ class CryptoUtil {
          */
         fun getEbicsPublicKeyHash(publicKey: RSAPublicKey): ByteArray {
             val keyBytes = ByteArrayOutputStream()
-            keyBytes.writeBytes(publicKey.publicExponent.toByteArray())
+            
keyBytes.writeBytes(publicKey.publicExponent.toByteArray().toHexString().toByteArray())
             keyBytes.write(' '.toInt())
-            keyBytes.writeBytes(publicKey.modulus.toByteArray())
+            
keyBytes.writeBytes(publicKey.modulus.toByteArray().toHexString().toByteArray())
             val digest = MessageDigest.getInstance("SHA-256")
             return digest.digest(keyBytes.toByteArray())
         }
diff --git a/sandbox/src/main/kotlin/DB.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
similarity index 99%
rename from sandbox/src/main/kotlin/DB.kt
rename to sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
index e869a58..b594dcb 100644
--- a/sandbox/src/main/kotlin/DB.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/DB.kt
@@ -17,7 +17,7 @@
  * <http://www.gnu.org/licenses/>
  */
 
-package tech.libeufin.sandbox.db
+package tech.libeufin.sandbox
 
 import org.jetbrains.exposed.dao.*
 import org.jetbrains.exposed.sql.*
diff --git a/sandbox/src/main/kotlin/JSON.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt
similarity index 100%
rename from sandbox/src/main/kotlin/JSON.kt
rename to sandbox/src/main/kotlin/tech/libeufin/sandbox/JSON.kt
diff --git a/sandbox/src/main/kotlin/Main.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
similarity index 98%
rename from sandbox/src/main/kotlin/Main.kt
rename to sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
index 6f40c6e..eecfa42 100644
--- a/sandbox/src/main/kotlin/Main.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt
@@ -44,7 +44,6 @@ import org.jetbrains.exposed.sql.transactions.transaction
 import org.slf4j.Logger
 import org.slf4j.LoggerFactory
 import org.w3c.dom.Document
-import tech.libeufin.sandbox.db.*
 import tech.libeufin.schema.ebics_h004.EbicsKeyManagementResponse
 import tech.libeufin.schema.ebics_h004.EbicsNoPubKeyDigestsRequest
 import tech.libeufin.schema.ebics_h004.EbicsUnsecuredRequest
@@ -61,8 +60,6 @@ import javax.sql.rowset.serial.SerialBlob
 
 val logger: Logger = LoggerFactory.getLogger("tech.libeufin.sandbox")
 
-val xmlProcess = XMLUtil()
-
 data class EbicsRequestError(val statusCode: HttpStatusCode) : 
Exception("Ebics request error")
 
 private suspend fun ApplicationCall.respondEbicsKeyManagement(
@@ -129,7 +126,7 @@ private suspend fun ApplicationCall.ebicsweb() {
 
     val bodyDocument: Document? = XMLUtil.parseStringIntoDom(body)
 
-    if (bodyDocument == null || (!xmlProcess.validateFromDom(bodyDocument))) {
+    if (bodyDocument == null || (!XMLUtil.validateFromDom(bodyDocument))) {
         respondEbicsInvalidXml()
         return
     }
@@ -343,7 +340,8 @@ private suspend fun ApplicationCall.ebicsweb() {
                             
CryptoUtil.loadRsaPublicKey(sigPubBlob.toByteArray())
                         )
                     }
-                    val validationResult = 
XMLUtil.verifyEbicsDocument(bodyDocument, 
subscriberKeys.authenticationPublicKey)
+                    val validationResult =
+                        XMLUtil.verifyEbicsDocument(bodyDocument, 
subscriberKeys.authenticationPublicKey)
                     logger.info("validationResult: $validationResult")
                 }
                 else -> {
@@ -434,7 +432,10 @@ fun main() {
                     if (host == null) null
                     else EbicsHostResponse(host.hostId, host.ebicsVersion)
                 }
-                if (resp == null) call.respond(HttpStatusCode.NotFound, 
SandboxError("host not found"))
+                if (resp == null) call.respond(
+                    HttpStatusCode.NotFound,
+                    SandboxError("host not found")
+                )
                 else call.respond(resp)
             }
             get("/ebics/subscribers") {
diff --git a/sandbox/src/main/kotlin/XMLUtil.kt 
b/sandbox/src/main/kotlin/tech/libeufin/sandbox/XMLUtil.kt
similarity index 76%
rename from sandbox/src/main/kotlin/XMLUtil.kt
rename to sandbox/src/main/kotlin/tech/libeufin/sandbox/XMLUtil.kt
index 1274b7c..5eb8bc8 100644
--- a/sandbox/src/main/kotlin/XMLUtil.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/XMLUtil.kt
@@ -31,11 +31,8 @@ import org.xml.sax.InputSource
 import org.xml.sax.SAXException
 import org.xml.sax.SAXParseException
 import java.io.*
-import java.lang.UnsupportedOperationException
-import java.security.MessageDigest
 import java.security.PrivateKey
 import java.security.PublicKey
-import java.util.*
 import javax.xml.XMLConstants
 import javax.xml.bind.JAXBContext
 import javax.xml.bind.JAXBElement
@@ -56,6 +53,7 @@ import javax.xml.transform.dom.DOMSource
 import javax.xml.transform.stream.StreamResult
 import javax.xml.transform.stream.StreamSource
 import javax.xml.validation.SchemaFactory
+import javax.xml.validation.Validator
 import javax.xml.xpath.XPath
 import javax.xml.xpath.XPathConstants
 import javax.xml.xpath.XPathFactory
@@ -63,7 +61,7 @@ import javax.xml.xpath.XPathFactory
 /**
  * Helpers for dealing with XML in EBICS.
  */
-class XMLUtil {
+class XMLUtil private constructor() {
     /**
      * This URI dereferencer allows handling the resource reference used for
      * XML signatures in EBICS.
@@ -101,93 +99,105 @@ class XMLUtil {
      * Validator for EBICS messages.
      */
     private val validator = try {
-        val classLoader = ClassLoader.getSystemClassLoader()
-        val sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
-        sf.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "file")
-        sf.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "")
-        sf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true)
-        sf.errorHandler = object : ErrorHandler {
-            override fun warning(p0: SAXParseException?) {
-                println("Warning: $p0")
-            }
 
-            override fun error(p0: SAXParseException?) {
-                println("Error: $p0")
-            }
+    } catch (e: SAXException) {
+        e.printStackTrace()
+        throw e
+    }
+
+    companion object {
+
+        private var cachedEbicsValidator: Validator? = null
+
+        private fun getEbicsValidator(): Validator {
+            val currentValidator = cachedEbicsValidator
+            if (currentValidator != null)
+                return currentValidator
+            val classLoader = ClassLoader.getSystemClassLoader()
+            val sf = 
SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI)
+            sf.setProperty(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "file")
+            sf.setProperty(XMLConstants.ACCESS_EXTERNAL_DTD, "")
+            sf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true)
+            sf.errorHandler = object : ErrorHandler {
+                override fun warning(p0: SAXParseException?) {
+                    println("Warning: $p0")
+                }
 
-            override fun fatalError(p0: SAXParseException?) {
-                println("Fatal error: $p0")
+                override fun error(p0: SAXParseException?) {
+                    println("Error: $p0")
+                }
+
+                override fun fatalError(p0: SAXParseException?) {
+                    println("Fatal error: $p0")
+                }
             }
-        }
-        sf.resourceResolver = object : LSResourceResolver {
-            override fun resolveResource(
-                type: String?,
-                namespaceURI: String?,
-                publicId: String?,
-                systemId: String?,
-                baseUri: String?
-            ): LSInput? {
-                if (type != "http://www.w3.org/2001/XMLSchema";) {
-                    return null
+            sf.resourceResolver = object : LSResourceResolver {
+                override fun resolveResource(
+                    type: String?,
+                    namespaceURI: String?,
+                    publicId: String?,
+                    systemId: String?,
+                    baseUri: String?
+                ): LSInput? {
+                    if (type != "http://www.w3.org/2001/XMLSchema";) {
+                        return null
+                    }
+                    val res = classLoader.getResourceAsStream("xsd/$systemId") 
?: return null
+                    return DOMInputImpl(publicId, systemId, baseUri, res, 
"UTF-8")
                 }
-                val res = classLoader.getResourceAsStream("xsd/$systemId") ?: 
return null
-                return DOMInputImpl(publicId, systemId, baseUri, res, "UTF-8")
             }
+            val schemaInputs: Array<Source> = listOf("xsd/ebics_H004.xsd", 
"xsd/ebics_hev.xsd").map {
+                val resUrl = classLoader.getResource(it) ?: throw 
FileNotFoundException("Schema file $it not found.")
+                StreamSource(File(resUrl.toURI()))
+            }.toTypedArray()
+            val bundle = sf.newSchema(schemaInputs)
+            val newValidator = bundle.newValidator()
+            cachedEbicsValidator = newValidator
+            return newValidator
         }
-        val schemaInputs: Array<Source> = listOf("xsd/ebics_H004.xsd", 
"xsd/ebics_hev.xsd").map {
-            val resUrl = classLoader.getResource(it) ?: throw 
FileNotFoundException("Schema file $it not found.")
-            StreamSource(File(resUrl.toURI()))
-        }.toTypedArray()
-        val bundle = sf.newSchema(schemaInputs)
-        bundle.newValidator()
-    } catch (e: SAXException) {
-        e.printStackTrace()
-        throw e
-    }
 
+        /**
+         *
+         * @param xmlDoc the XML document to validate
+         * @return true when validation passes, false otherwise
+         */
+        fun validate(xmlDoc: StreamSource): Boolean {
+            try {
+                getEbicsValidator().validate(xmlDoc)
+            } catch (e: Exception) {
+                logger.warn("Validation failed: {}", e)
+                return false
+            }
+            return true;
+        }
 
-    /**
-     *
-     * @param xmlDoc the XML document to validate
-     * @return true when validation passes, false otherwise
-     */
-    fun validate(xmlDoc: StreamSource): Boolean {
-        try {
-            validator?.validate(xmlDoc)
-        } catch (e: Exception) {
-            logger.warn("Validation failed: {}", e)
-            return false
+        /**
+         * Validates the DOM against the Schema(s) of this object.
+         * @param domDocument DOM to validate
+         * @return true/false if the document is valid/invalid
+         */
+        fun validateFromDom(domDocument: Document): Boolean {
+            try {
+                getEbicsValidator().validate(DOMSource(domDocument))
+            } catch (e: SAXException) {
+                e.printStackTrace()
+                return false
+            }
+            return true
         }
-        return true;
-    }
 
-    /**
-     * Validates the DOM against the Schema(s) of this object.
-     * @param domDocument DOM to validate
-     * @return true/false if the document is valid/invalid
-     */
-    fun validateFromDom(domDocument: Document): Boolean {
-        try {
-            validator?.validate(DOMSource(domDocument))
-        } catch (e: SAXException) {
-            e.printStackTrace()
-            return false
+        /**
+         * Craft object to be passed to the XML validator.
+         * @param xmlString XML body, as read from the POST body.
+         * @return InputStream object, as wanted by the validator.
+         */
+        fun validateFromString(xmlString: String): Boolean {
+            val xmlInputStream: InputStream = 
ByteArrayInputStream(xmlString.toByteArray())
+            val xmlSource = StreamSource(xmlInputStream)
+            return validate(xmlSource)
         }
-        return true
-    }
 
-    /**
-     * Craft object to be passed to the XML validator.
-     * @param xmlString XML body, as read from the POST body.
-     * @return InputStream object, as wanted by the validator.
-     */
-    fun validateFromString(xmlString: String): Boolean {
-        val xmlInputStream: InputStream = 
ByteArrayInputStream(xmlString.toByteArray())
-        val xmlSource = StreamSource(xmlInputStream)
-        return this.validate(xmlSource)
-    }
 
-    companion object {
         inline fun <reified T> convertJaxbToString(obj: T): String {
             val sw = StringWriter()
             val jc = JAXBContext.newInstance(T::class.java)
diff --git 
a/sandbox/src/main/kotlin/tech/libeufin/schema/ebics_h004/EbicsMessages.kt 
b/sandbox/src/main/kotlin/tech/libeufin/schema/ebics_h004/EbicsMessages.kt
index f98d65f..2b33ec6 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/schema/ebics_h004/EbicsMessages.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/schema/ebics_h004/EbicsMessages.kt
@@ -581,4 +581,20 @@ class EbicsNoPubKeyDigestsRequest {
 
     @XmlAccessorType(XmlAccessType.NONE)
     class EmptyBody
+}
+
+
+@XmlAccessorType(XmlAccessType.NONE)
+@XmlType(name = "", propOrder = ["authenticationPubKeyInfo", 
"encryptionPubKeyInfo", "hostID"])
+@XmlRootElement(name = "HPBResponseOrderData")
+class HPBResponseOrderData {
+    @get:XmlElement(name = "AuthenticationPubKeyInfo", required = true)
+    lateinit var authenticationPubKeyInfo: AuthenticationPubKeyInfoType
+
+    @get:XmlElement(name = "EncryptionPubKeyInfo", required = true)
+    lateinit var encryptionPubKeyInfo: EncryptionPubKeyInfoType
+
+    @get:XmlElement(name = "HostID", required = true)
+    @get:XmlJavaTypeAdapter(CollapsedStringAdapter::class)
+    lateinit var hostID: String
 }
\ No newline at end of file
diff --git a/sandbox/src/test/kotlin/XmlUtilTest.kt 
b/sandbox/src/test/kotlin/XmlUtilTest.kt
index ec9b461..37b2ab9 100644
--- a/sandbox/src/test/kotlin/XmlUtilTest.kt
+++ b/sandbox/src/test/kotlin/XmlUtilTest.kt
@@ -8,20 +8,18 @@ import javax.xml.transform.stream.StreamSource
 
 class XmlUtilTest {
 
-    val processor = tech.libeufin.sandbox.XMLUtil()
-
     @Test
     fun hevValidation(){
         val classLoader = ClassLoader.getSystemClassLoader()
         val hev = classLoader.getResourceAsStream("ebics_hev.xml")
-        assertTrue(processor.validate(StreamSource(hev)))
+        assertTrue(XMLUtil.validate(StreamSource(hev)))
     }
 
     @Test
     fun iniValidation(){
         val classLoader = ClassLoader.getSystemClassLoader()
         val ini = 
classLoader.getResourceAsStream("ebics_ini_request_sample.xml")
-        assertTrue(processor.validate(StreamSource(ini)))
+        assertTrue(XMLUtil.validate(StreamSource(ini)))
     }
 
     @Test

-- 
To stop receiving notification emails like this one, please contact
address@hidden.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]