libeufin

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

commit 6e6b303318ba906f8fd6f7f72c1a576e0994dae6
parent 1b4fee7d989d7b595588cc28e5d8c7494d9b83d3
Author: Antoine A <>
Date:   Thu, 10 Oct 2024 15:07:48 +0200

common: fix KTOR interceptor

Diffstat:
Mbank/src/main/kotlin/tech/libeufin/bank/auth/auth.kt | 4++--
Mbank/src/main/kotlin/tech/libeufin/bank/helpers.kt | 2+-
Mcommon/src/main/kotlin/api/route.kt | 6++----
Mnexus/src/main/kotlin/tech/libeufin/nexus/api/helpers.kt | 2+-
4 files changed, 6 insertions(+), 8 deletions(-)

diff --git a/bank/src/main/kotlin/tech/libeufin/bank/auth/auth.kt b/bank/src/main/kotlin/tech/libeufin/bank/auth/auth.kt @@ -59,7 +59,7 @@ val ApplicationCall.authToken: ByteArray? get() = attributes.getOrNull(AUTH_TOKE * You can check is the currently authenticated user is admin using [isAdmin]. **/ fun Route.authAdmin(db: Database, pwCrypto: PwCrypto, scope: TokenScope, enforce: Boolean = true, callback: Route.() -> Unit): Route = - intercept(callback) { + intercept("AuthAdmin", callback) { if (enforce) { val username = this.authenticateBankRequest(db, pwCrypto, scope) if (username != "admin") { @@ -86,7 +86,7 @@ fun Route.authAdmin(db: Database, pwCrypto: PwCrypto, scope: TokenScope, enforce * You can check is the currently authenticated user is admin using [isAdmin]. **/ fun Route.auth(db: Database, pwCrypto: PwCrypto ,scope: TokenScope, allowAdmin: Boolean = false, requireAdmin: Boolean = false, callback: Route.() -> Unit): Route = - intercept(callback) { + intercept("Auth", callback) { val authUsername = this.authenticateBankRequest(db, pwCrypto, scope) if (requireAdmin && authUsername != "admin") { throw unauthorized("Only administrator allowed") diff --git a/bank/src/main/kotlin/tech/libeufin/bank/helpers.kt b/bank/src/main/kotlin/tech/libeufin/bank/helpers.kt @@ -116,7 +116,7 @@ suspend fun createAdminAccount(db: Database, cfg: BankConfig, pw: String? = null } fun Route.conditional(implemented: Boolean, callback: Route.() -> Unit): Route = - intercept(callback) { + intercept("Conditional", callback) { if (!implemented) { throw apiError(HttpStatusCode.NotImplemented, "API not implemented", TalerErrorCode.END) } diff --git a/common/src/main/kotlin/api/route.kt b/common/src/main/kotlin/api/route.kt @@ -23,10 +23,8 @@ import io.ktor.server.application.* import io.ktor.server.routing.* import io.ktor.util.pipeline.* -fun Route.intercept(build: Route.() -> Unit, lambda: suspend ApplicationCall.() -> Unit): Route { - val plugin = createRouteScopedPlugin( - "Interceptor" - ) { +fun Route.intercept(name: String, build: Route.() -> Unit, lambda: suspend ApplicationCall.() -> Unit): Route { + val plugin = createRouteScopedPlugin(name) { onCall { call -> call.lambda() } } val subRoute = createChild(object : RouteSelector() { diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/api/helpers.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/api/helpers.kt @@ -29,7 +29,7 @@ import tech.libeufin.nexus.AuthMethod /** Apply api configuration for a route: conditional access and authentication */ fun Route.authApi(cfg: ApiConfig?, callback: Route.() -> Unit): Route = - intercept(callback) { + intercept("Auth", callback) { if (cfg == null) { throw apiError(HttpStatusCode.NotImplemented, "API not implemented", TalerErrorCode.END) }