libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit dcc1089eb5857a368b544b8aff6360bef38b5ed6
parent 5888c5283bce2400d655cc9e29823f99a144b504
Author: ms <ms@taler.net>
Date:   Thu, 14 Oct 2021 17:08:04 +0200

fix AttributeKey usage

Diffstat:
Msandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt | 20+++++++-------------
Mutil/src/main/kotlin/Config.kt | 23+++++++++++++++++++----
Mutil/src/main/kotlin/HTTP.kt | 4++--
3 files changed, 28 insertions(+), 19 deletions(-)

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt @@ -454,20 +454,14 @@ val sandboxApp: Application.() -> Unit = { } } intercept(ApplicationCallPipeline.Setup) { - /** - * Allows disabling authentication during tests. - */ - call.application.apply { - attributes.put(AttributeKey("withAuth"), WITH_AUTH) - } - call.application.apply { - if (adminPassword != null) { - call.attributes.put(AttributeKey("adminPassword"), adminPassword) - /** - * When not given, the checker expects --no-auth to have been specified on the CLI. - */ - } + logger.info("Going Setup phase.") + val ac: ApplicationCall = call + ac.attributes.put(WITH_AUTH_ATTRIBUTE_KEY, WITH_AUTH) + if (adminPassword != null) { + ac.attributes.put(AttributeKey("adminPassword"), adminPassword) } + logger.info("Finish Setup phase.") + return@intercept } intercept(ApplicationCallPipeline.Fallback) { if (this.call.response.status() == null) { diff --git a/util/src/main/kotlin/Config.kt b/util/src/main/kotlin/Config.kt @@ -10,6 +10,17 @@ import org.slf4j.LoggerFactory import printLnErr import kotlin.system.exitProcess +/** + * Putting those values into the 'attributes' container because they + * are needed by the util routines that do NOT have Sandbox and Nexus + * as dependencies, and therefore cannot access their global variables. + * + * Note: putting Sandbox and Nexus as Utils dependencies would result + * into circular dependency. + */ +val WITH_AUTH_ATTRIBUTE_KEY = AttributeKey<Boolean>("withAuth") +val ADMIN_PASSWORD_ATTRIBUTE_KEY = AttributeKey<String>("adminPassword") + fun getVersion(): String { return Loader.getResource( "version.txt", ClassLoader.getSystemClassLoader() @@ -58,10 +69,14 @@ internal fun <T : Any>ApplicationCall.maybeAttribute(name: String): T? { return this.attributes[key] } -internal fun <T : Any>ApplicationCall.ensureAttribute(name: String): T { - val key = AttributeKey<T>("name") - if (!this.attributes.contains(key)) - throw internalServerError("Attribute $name not found along the call.") +/** + * Retun the attribute, or throw 500 Internal server error. + */ +fun <T : Any>ApplicationCall.ensureAttribute(key: AttributeKey<T>): T { + if (!this.attributes.contains(key)) { + println("Error: attribute $key not found along the call.") + throw internalServerError("Attribute $key not found along the call.") + } return this.attributes[key] } diff --git a/util/src/main/kotlin/HTTP.kt b/util/src/main/kotlin/HTTP.kt @@ -89,7 +89,7 @@ fun ApplicationRequest.getBaseUrl(): String { * environment. */ fun ApplicationRequest.basicAuth() { - val withAuth = this.call.ensureAttribute<Boolean>("withAuth") + val withAuth = this.call.ensureAttribute(WITH_AUTH_ATTRIBUTE_KEY) if (!withAuth) { logger.info("Authentication is disabled - assuming tests currently running.") return @@ -97,7 +97,7 @@ fun ApplicationRequest.basicAuth() { val credentials = getHTTPBasicAuthCredentials(this) if (credentials.first == "admin") { // env must contain the admin password, because --with-auth is true. - val adminPassword = this.call.ensureAttribute<String>("adminPassword") + val adminPassword = this.call.ensureAttribute(ADMIN_PASSWORD_ATTRIBUTE_KEY) if (credentials.second != adminPassword) throw unauthorized( "Admin authentication failed" )