From 5231c8b690271d4b3dee492bceaf90a6a588c989 Mon Sep 17 00:00:00 2001 From: Nicholas Kalar Date: Mon, 25 Aug 2025 15:50:42 -0400 Subject: [PATCH] Added Staff authentication --- .../kotlin/routes/CollectionItemRoutes.kt | 2 +- src/main/kotlin/routes/LibraryRoutes.kt | 75 ++++++++++--------- src/main/kotlin/routes/PatronRoutes.kt | 65 +++++++++------- src/main/kotlin/routes/StaffRoutes.kt | 13 ++-- 4 files changed, 87 insertions(+), 68 deletions(-) diff --git a/src/main/kotlin/routes/CollectionItemRoutes.kt b/src/main/kotlin/routes/CollectionItemRoutes.kt index 4fb321e..7d2a40c 100644 --- a/src/main/kotlin/routes/CollectionItemRoutes.kt +++ b/src/main/kotlin/routes/CollectionItemRoutes.kt @@ -42,7 +42,7 @@ fun Application.configureCollectionItemRoutes(dbConnection: Connection) { } } - authenticate("auth-jwt") { + authenticate("staff") { post("/items") { try { val item = call.receive() diff --git a/src/main/kotlin/routes/LibraryRoutes.kt b/src/main/kotlin/routes/LibraryRoutes.kt index 6329a53..5da29bf 100644 --- a/src/main/kotlin/routes/LibraryRoutes.kt +++ b/src/main/kotlin/routes/LibraryRoutes.kt @@ -7,6 +7,7 @@ import codes.kalar.model.NewLibrary import codes.kalar.service.LibraryService import io.ktor.http.* import io.ktor.server.application.* +import io.ktor.server.auth.authenticate import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* @@ -41,7 +42,10 @@ fun Application.configureLibraryRoutes(dbConnection: Connection) { } catch (cause: DbElementNotFoundException) { call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to find Library.") } catch (cause: NumberFormatException) { - call.respond(HttpStatusCode.BadRequest, "Unable to parse number format. \"${call.pathParameters["id"]}\" is not a number.") + call.respond( + HttpStatusCode.BadRequest, + "Unable to parse number format. \"${call.pathParameters["id"]}\" is not a number." + ) } } @@ -49,43 +53,44 @@ fun Application.configureLibraryRoutes(dbConnection: Connection) { // TODO Add search for collection_it where itemID && libraryID } - post("/libraries") { - val library = call.receive() - try { - val id = libraryService.create(library) - call.respondText("${library.name} is posted with the ID: $id") - } catch (cause: DbElementInsertionException) { - call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to insert Library.") - } - } - - patch("/libraries") { - try { - val library = call.receive() - val patchedLibrary = libraryService.update(library) - call.respond(HttpStatusCode.OK, patchedLibrary) - } catch (cause: DbElementInsertionException) { - log.error(cause.message) - call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Library.") - } catch (cause: ContentTransformationException) { - log.error(cause.message) - call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments") + authenticate("staff") { + post("/libraries") { + val library = call.receive() + try { + val id = libraryService.create(library) + call.respondText("${library.name} is posted with the ID: $id") + } catch (cause: DbElementInsertionException) { + call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to insert Library.") + } } - } + patch("/libraries") { + try { + val library = call.receive() + val patchedLibrary = libraryService.update(library) + call.respond(HttpStatusCode.OK, patchedLibrary) + } catch (cause: DbElementInsertionException) { + log.error(cause.message) + call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Library.") + } catch (cause: ContentTransformationException) { + log.error(cause.message) + call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments") + } + } - delete("/libraries") { - try { - val id = call.parameters["id"]!!.toLong() - log.info("Deleting item with id=$id") - libraryService.delete(id) - call.respondText(":(", status = HttpStatusCode.OK) - } catch (cause: DbElementNotFoundException) { - log.error(cause.message, cause) - call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments") - } catch (cause: NumberFormatException) { - log.error(cause.message, cause) - call.respond(HttpStatusCode.BadRequest, cause.message ?: "Invalid ID format") + delete("/libraries") { + try { + val id = call.parameters["id"]!!.toLong() + log.info("Deleting item with id=$id") + libraryService.delete(id) + call.respondText(":(", status = HttpStatusCode.OK) + } catch (cause: DbElementNotFoundException) { + log.error(cause.message, cause) + call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments") + } catch (cause: NumberFormatException) { + log.error(cause.message, cause) + call.respond(HttpStatusCode.BadRequest, cause.message ?: "Invalid ID format") + } } } } diff --git a/src/main/kotlin/routes/PatronRoutes.kt b/src/main/kotlin/routes/PatronRoutes.kt index ea533ea..c02fb53 100644 --- a/src/main/kotlin/routes/PatronRoutes.kt +++ b/src/main/kotlin/routes/PatronRoutes.kt @@ -7,6 +7,7 @@ import codes.kalar.model.Patron import codes.kalar.service.PatronService import io.ktor.http.* import io.ktor.server.application.* +import io.ktor.server.auth.authenticate import io.ktor.server.request.* import io.ktor.server.response.* import io.ktor.server.routing.* @@ -42,39 +43,49 @@ fun Application.configurePatronRoutes(dbConnection: Connection) { } } - post("/patron") { - try { - val patron = call.receive() - val id = patronService.create(patron) - call.respondText("Adding ${patron.name} to database with the id of $id", status = HttpStatusCode.OK) - } catch (cause: DbElementInsertionException) { - call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments") - } catch (cause: ContentTransformationException) { - call.respond(HttpStatusCode.BadRequest, "Bad Arguments. Must pass a valid CollectionItem object.") + authenticate("staff") { + post("/patron") { + try { + val patron = call.receive() + val id = patronService.create(patron) + call.respondText("Adding ${patron.name} to database with the id of $id", status = HttpStatusCode.OK) + } catch (cause: DbElementInsertionException) { + call.respond(HttpStatusCode.BadRequest, cause.message ?: "Bad Arguments") + } catch (cause: ContentTransformationException) { + call.respond(HttpStatusCode.BadRequest, "Bad Arguments. Must pass a valid CollectionItem object.") + } } } - patch("/patron") { - try { - val patron = call.receive() - val patchedPatron = patronService.update(patron) - call.respondText("${patron.name} is patched") - } catch (cause: DbElementInsertionException) { - call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Patron.") - } catch (cause: ContentTransformationException) { - + authenticate("general") { + patch("/patron") { + try { + val patron = call.receive() + val isPatched = patronService.update(patron) + if (isPatched) { + call.respond(HttpStatusCode.OK, "${patron.name} is patched") + } else { + call.respond(HttpStatusCode.BadRequest, "${patron.name} is not patched") + } + } catch (cause: DbElementInsertionException) { + call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to update Patron.") + } catch (cause: ContentTransformationException) { + call.respond(HttpStatusCode.BadRequest, "Bad Arguments. Must pass a valid Patron object.") + } } } - delete("/patron/{id}") { - try { - val id = call.pathParameters["id"]!!.toLong() - patronService.delete(id) - call.respond(HttpStatusCode.OK, "Successfully deleted the patron") - } catch (cause: DbElementInsertionException) { - call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to delete Patron.") - } catch (cause: NumberFormatException) { - call.respond(HttpStatusCode.BadRequest, cause.message ?: "ID needs to be a number.") + authenticate("staff") { + delete("/patron/{id}") { + try { + val id = call.pathParameters["id"]!!.toLong() + patronService.delete(id) + call.respond(HttpStatusCode.OK, "Successfully deleted the patron") + } catch (cause: DbElementInsertionException) { + call.respond(HttpStatusCode.BadRequest, cause.message ?: "Unable to delete Patron.") + } catch (cause: NumberFormatException) { + call.respond(HttpStatusCode.BadRequest, cause.message ?: "ID needs to be a number.") + } } } } diff --git a/src/main/kotlin/routes/StaffRoutes.kt b/src/main/kotlin/routes/StaffRoutes.kt index d3a28ba..20a3e1f 100644 --- a/src/main/kotlin/routes/StaffRoutes.kt +++ b/src/main/kotlin/routes/StaffRoutes.kt @@ -1,6 +1,7 @@ package codes.kalar.routes import io.ktor.server.application.* +import io.ktor.server.auth.authenticate import io.ktor.server.response.* import io.ktor.server.routing.* import java.sql.Connection @@ -16,16 +17,18 @@ fun Application.configureStaffRoutes(dbConnection: Connection) { call.respondText(call.parameters["id"]!!) } - post("/staff") { + authenticate("staff") { + post("/staff") { - } + } - patch("/staff") { + patch("/staff") { - } + } - delete("/staff/{id}") { + delete("/staff/{id}") { + } } } } \ No newline at end of file