From 6a9803aa813a196fe8c5e54d7bf214310ea700e0 Mon Sep 17 00:00:00 2001 From: Shiina Kin Date: Thu, 12 Sep 2024 20:59:46 +0800 Subject: [PATCH] feat: modify route --- .../kotlin/io/sakurasou/plugins/Routing.kt | 57 ++++++++++++------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/app/src/main/kotlin/io/sakurasou/plugins/Routing.kt b/app/src/main/kotlin/io/sakurasou/plugins/Routing.kt index f64b1ae4..3375ec90 100644 --- a/app/src/main/kotlin/io/sakurasou/plugins/Routing.kt +++ b/app/src/main/kotlin/io/sakurasou/plugins/Routing.kt @@ -1,7 +1,10 @@ package io.sakurasou.plugins +import com.ucasoft.ktor.simpleCache.cacheOutput import io.ktor.http.* import io.ktor.server.application.* +import io.ktor.server.auth.* +import io.ktor.server.auth.jwt.* import io.ktor.server.http.content.* import io.ktor.server.plugins.autohead.* import io.ktor.server.plugins.doublereceive.* @@ -9,9 +12,15 @@ import io.ktor.server.plugins.statuspages.* import io.ktor.server.resources.* import io.ktor.server.response.* import io.ktor.server.routing.* +import io.ktor.util.* +import io.sakurasou.config.InstanceCenter.relationDao +import io.sakurasou.config.InstanceCenter.userDao import io.sakurasou.controller.* import io.sakurasou.exception.FileSizeException +import io.sakurasou.exception.UnauthorizedAccessException +import io.sakurasou.exception.UserNotFoundException import io.sakurasou.exception.WrongParameterException +import io.sakurasou.extension.getPrincipal fun Application.configureRouting() { install(Resources) @@ -25,32 +34,38 @@ fun Application.configureRouting() { exception { call: ApplicationCall, cause -> call.respondText(text = "400: $cause", status = HttpStatusCode.BadRequest) } + exception { call: ApplicationCall, cause -> + call.respondText(text = "401: $cause", status = HttpStatusCode.Unauthorized) + } + exception { call: ApplicationCall, cause -> + call.respondText(text = "400: $cause", status = HttpStatusCode.BadRequest) + } } install(AutoHeadResponse) install(DoubleReceive) routing { route("api") { - // authenticate { - // - // } - commonRoute() - imageRoute() - albumRoute() - strategyRoute() - settingRoute() - userRoute() - groupRoute() - roleRoute() - // cacheOutput(2.seconds) { - // get("/short") { - // call.respond(Random.nextInt().toString()) - // } - // } - // cacheOutput { - // get("/default") { - // call.respond(Random.nextInt().toString()) - // } - // } + authRoute(userDao, relationDao) + cacheOutput { commonRoute() } + authenticate("auth-jwt") { + intercept(ApplicationCallPipeline.Call) { + val principal = call.principal() + val username = principal!!.payload.getClaim("username").asString() + val role: List = principal.payload.getClaim("role").asList(String::class.java) + call.attributes.put(AttributeKey("username"), username) + call.attributes.put(AttributeKey("role"), role) + } + get("helloworld") { + call.respond(call.attributes.getPrincipal()) + } + imageRoute() + albumRoute() + strategyRoute() + settingRoute() + userRoute(userDao) + groupRoute() + roleRoute() + } } staticResources("", "static") // post("/double-receive") {