feat: upgrade ktor and kotlin and secure endpoints
This commit is contained in:
1 parent
87812d02d3
commit
5d4e855b8e
11 files changed
+128
-40
No files matched your search
@@ -5,9 +5,9 @@ val logback_version: String by project
|
||||
val exposed_version: String by project
|
||||
val h2_version: String by project
|
||||
plugins {
|
||||
kotlin("jvm") version "1.9.0"
|
||||
id("io.ktor.plugin") version "2.3.3"
|
||||
kotlin("plugin.serialization") version "1.9.0"
|
||||
kotlin("jvm") version "2.3.21"
|
||||
id("io.ktor.plugin") version "3.5.1"
|
||||
kotlin("plugin.serialization") version "2.3.21"
|
||||
id("com.palantir.docker") version "0.36.0"
|
||||
}
|
||||
|
||||
@@ -30,10 +30,10 @@ dependencies {
|
||||
implementation("io.ktor:ktor-server-auth-jvm")
|
||||
implementation("io.ktor:ktor-server-auth-jwt-jvm")
|
||||
implementation("io.ktor:ktor-server-sessions-jvm")
|
||||
implementation("io.ktor:ktor-server-host-common-jvm")
|
||||
implementation("io.ktor:ktor-server-cors-jvm")
|
||||
implementation("io.ktor:ktor-server-content-negotiation-jvm")
|
||||
implementation("io.ktor:ktor-serialization-kotlinx-json-jvm")
|
||||
implementation("io.ktor:ktor-server-swagger:$ktor_version")
|
||||
implementation("org.jetbrains.exposed:exposed-core:$exposed_version")
|
||||
implementation("org.jetbrains.exposed:exposed-jdbc:$exposed_version")
|
||||
implementation("io.ktor:ktor-server-netty-jvm")
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
ktor_version=2.3.3
|
||||
kotlin_version=1.9.0
|
||||
ktor_version=3.5.1
|
||||
kotlin_version=2.3.21
|
||||
logback_version=1.4.14
|
||||
kotlin.code.style=official
|
||||
exposed_version=0.43.0
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-bin.zip
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.11-bin.zip
|
||||
networkTimeout=10000
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
|
||||
@@ -45,6 +45,14 @@ object MarksDao {
|
||||
}.resultedValues?.isNotEmpty() ?: false
|
||||
}
|
||||
|
||||
suspend fun setMarks(marks: List<Mark>) = dbQuery {
|
||||
Marks.batchUpsert(marks) { mark ->
|
||||
this[Marks.mid] = mark.mid
|
||||
this[Marks.gid] = mark.gid
|
||||
this[Marks.value] = mark.value
|
||||
}.isNotEmpty() ?: false
|
||||
}
|
||||
|
||||
suspend fun removeMark(ministrantId: Int, gottesdienstId: Int): Boolean = dbQuery {
|
||||
Marks.deleteWhere {
|
||||
(Marks.gid eq gottesdienstId) and
|
||||
|
||||
@@ -111,6 +111,18 @@ object MinistrantenDao {
|
||||
statement.resultedValues?.singleOrNull()?.let(::resultRowToMinistrant)
|
||||
}
|
||||
|
||||
suspend fun getMinistrantById(id: Int): Ministrant? = dbQuery {
|
||||
Ministranten.select(Ministranten.id eq id).map {
|
||||
resultRowToMinistrant(it)
|
||||
}.firstOrNull()
|
||||
}
|
||||
|
||||
suspend fun getMinistrantenById(ids: List<Int>): List<Ministrant> = dbQuery {
|
||||
Ministranten.select { Ministranten.id inList ids }.map {
|
||||
resultRowToMinistrant(it)
|
||||
}
|
||||
}
|
||||
|
||||
suspend fun deleteMinistrant(id: Int): Boolean = dbQuery {
|
||||
Ministranten.deleteWhere { Ministranten.id eq id } > 0
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.walamana.plugins
|
||||
import at.favre.lib.crypto.bcrypt.BCrypt
|
||||
import com.auth0.jwt.JWT
|
||||
import com.auth0.jwt.algorithms.Algorithm
|
||||
import com.auth0.jwt.interfaces.Claim
|
||||
import com.auth0.jwt.interfaces.Payload
|
||||
import de.walamana.models.Ministrant
|
||||
import de.walamana.models.MinistrantenDao
|
||||
@@ -13,7 +14,7 @@ import io.ktor.server.auth.jwt.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.sessions.*
|
||||
import java.util.*
|
||||
import java.util.Date
|
||||
|
||||
|
||||
const val SALT_ROUNDS = 10;
|
||||
@@ -56,6 +57,22 @@ fun Application.getJWTEnvironment(): JWTEnvironment {
|
||||
|
||||
fun Payload.username() = getClaim("username").asString()
|
||||
fun Payload.mid() = getClaim("id").asInt()
|
||||
fun Payload.privileges(): List<String> {
|
||||
val claim = getClaim("privileges")
|
||||
return if (claim.isNull || claim.asList(String::class.java).isNullOrEmpty()) emptyList() else claim.asList(String::class.java)
|
||||
}
|
||||
|
||||
fun Payload.canAccessMinistrant(username: String): Boolean {
|
||||
return this.username() == "admin" || this.privileges().contains(username)
|
||||
}
|
||||
|
||||
fun Payload.canAccessMinistranten(usernames: List<String>): Boolean {
|
||||
for (username in usernames) {
|
||||
if(!canAccessMinistrant(username))
|
||||
return false;
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
object Security {
|
||||
|
||||
@@ -25,14 +25,6 @@ fun Application.configureSerialization() {
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
routing {
|
||||
get("/json/kotlinx-serialization") {
|
||||
println("Test")
|
||||
// call.respond("Test")
|
||||
call.respond(mapOf("hello" to "world"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typealias CommaSeperatedStringList = @Serializable(StringListAsCommaSeperatedString::class) List<String>;
|
||||
|
||||
@@ -2,22 +2,30 @@ package de.walamana.views
|
||||
|
||||
import de.walamana.models.Gottesdienst
|
||||
import de.walamana.models.GottesdiensteDao
|
||||
import de.walamana.plugins.getJWTEnvironment
|
||||
import de.walamana.plugins.username
|
||||
import de.walamana.service.ZelebrationsplanParser
|
||||
import io.ktor.http.*
|
||||
import io.ktor.http.content.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.auth.*
|
||||
import io.ktor.server.auth.jwt.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.util.*
|
||||
|
||||
fun Route.configureGottesdiensteRoutes() {
|
||||
authenticate {
|
||||
route("/gottesdienste") {
|
||||
get {
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@get call.respond(HttpStatusCode.Unauthorized)
|
||||
val data = GottesdiensteDao.allGottesdienste()
|
||||
call.respond(data)
|
||||
}
|
||||
put {
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@put call.respond(HttpStatusCode.Unauthorized)
|
||||
if (principal.payload.username() != "admin") return@put call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Nur Admin kann Gottesdienste erstellen"))
|
||||
val data = call.receive<Gottesdienst>()
|
||||
val gottesdienst = GottesdiensteDao.createGottesdienst(
|
||||
data.name,
|
||||
@@ -28,7 +36,8 @@ fun Route.configureGottesdiensteRoutes() {
|
||||
call.respond(mapOf("id" to gottesdienst?.id))
|
||||
}
|
||||
patch {
|
||||
// TODO: Access only by admin
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@patch call.respond(HttpStatusCode.Unauthorized)
|
||||
if (principal.payload.username() != "admin") return@patch call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Nur Admin kann Gottesdienste aktualisieren"))
|
||||
val data = call.receive<Gottesdienst>()
|
||||
val changed = GottesdiensteDao.updateGottesdienst(
|
||||
data.id,
|
||||
@@ -40,12 +49,16 @@ fun Route.configureGottesdiensteRoutes() {
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
delete {
|
||||
val id = call.request.queryParameters["id"]?.toInt() ?: TODO("fail")
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@delete call.respond(HttpStatusCode.Unauthorized)
|
||||
if (principal.payload.username() != "admin") return@delete call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Nur Admin kann Gottesdienste löschen"))
|
||||
val id = call.request.queryParameters["id"]?.toInt() ?: return@delete call.respond(HttpStatusCode.BadRequest, mapOf("msg" to "Missing id parameter"))
|
||||
GottesdiensteDao.deleteGottesdienst(id)
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
|
||||
post("/parseZelebrationsplan") {
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@post call.respond(HttpStatusCode.Unauthorized)
|
||||
if (principal.payload.username() != "admin") return@post call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Nur Admin kann Pläne hochladen"))
|
||||
val id = call.parameters.getOrFail("id").toInt()
|
||||
val multipartData = call.receiveMultipart()
|
||||
multipartData.forEachPart { part ->
|
||||
@@ -57,4 +70,5 @@ fun Route.configureGottesdiensteRoutes() {
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,8 +1,15 @@
|
||||
package de.walamana.views
|
||||
|
||||
import de.walamana.models.*
|
||||
import de.walamana.plugins.canAccessMinistrant
|
||||
import de.walamana.plugins.canAccessMinistranten
|
||||
import de.walamana.plugins.mid
|
||||
import de.walamana.plugins.username
|
||||
import de.walamana.service.PlanDao
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.auth.*
|
||||
import io.ktor.server.auth.jwt.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
@@ -10,37 +17,53 @@ import io.ktor.server.util.*
|
||||
|
||||
|
||||
fun Route.configureMarksView() {
|
||||
authenticate {
|
||||
route("/marks") {
|
||||
get {
|
||||
val data = MarksDao.allMarksForPlan(call.receiveParameters().getOrFail("id").toInt())
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@get call.respond(HttpStatusCode.Unauthorized)
|
||||
val planId = call.receiveParameters().getOrFail("id").toInt()
|
||||
val plan = PlanDao.constructPlan(planId, isAnonymous = false)
|
||||
val data = MarksDao.allMarksForPlan(planId)
|
||||
call.respond(data)
|
||||
}
|
||||
put {
|
||||
patch {
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@patch call.respond(HttpStatusCode.Unauthorized)
|
||||
val changedMarks = call.receive<List<Mark>>()
|
||||
for(changedMark in changedMarks) {
|
||||
val mark = MarksDao.setMark(
|
||||
changedMark.mid,
|
||||
changedMark.gid,
|
||||
changedMark.value
|
||||
val ministrantenIds = changedMarks.map { it.mid }.distinct()
|
||||
val ministrantenUsernames = MinistrantenDao.getMinistrantenById(ministrantenIds).map { it.username }
|
||||
if (ministrantenUsernames.size != ministrantenIds.size) {
|
||||
return@patch call.respond(
|
||||
HttpStatusCode.BadRequest,
|
||||
mapOf("msg" to "Es wurden ungültige Ministranten IDs angegeben.")
|
||||
)
|
||||
}
|
||||
call.respond(HttpStatusCode.OK)
|
||||
if (!principal.payload.canAccessMinistranten(ministrantenUsernames)) {
|
||||
return@patch call.respond(
|
||||
HttpStatusCode.Forbidden,
|
||||
mapOf("msg" to "Sie haben keine Berechtigung für diese Ministrianten")
|
||||
)
|
||||
}
|
||||
patch {
|
||||
// TODO: Access only by admin
|
||||
val changedMarks = call.receive<List<Mark>>()
|
||||
for(changedMark in changedMarks) {
|
||||
val mark = MarksDao.setMark(
|
||||
changedMark.mid,
|
||||
changedMark.gid,
|
||||
changedMark.value
|
||||
if (!MarksDao.setMarks(changedMarks)) {
|
||||
return@patch call.respond(
|
||||
HttpStatusCode.InternalServerError,
|
||||
mapOf("msg" to "Server konnte Änderungen nicht speichern")
|
||||
)
|
||||
}
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
delete {
|
||||
GottesdiensteDao.deleteGottesdienst(call.receiveParameters().getOrFail("id").toInt())
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@delete call.respond(HttpStatusCode.Unauthorized)
|
||||
val ministerId = call.request.queryParameters["mid"]?.toInt() ?: return@delete call.respond(HttpStatusCode.BadRequest, mapOf("msg" to "Missing mid parameter"))
|
||||
val gottesdienstId = call.request.queryParameters["gid"]?.toInt() ?: return@delete call.respond(HttpStatusCode.BadRequest, mapOf("msg" to "Missing gid parameter"))
|
||||
val ministantUsername = if (ministerId == 0) "admin" else {
|
||||
MinistrantenDao.getMinistrantById(ministerId)?.username ?: return@delete call.respond(HttpStatusCode.BadRequest, mapOf("msg" to "Ungültige Ministrianten-ID"))
|
||||
}
|
||||
if (!principal.payload.canAccessMinistrant(ministantUsername)) {
|
||||
return@delete call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Sie haben keine Berechtigung für diese Ministrianten"))
|
||||
}
|
||||
MarksDao.removeMark(ministerId, gottesdienstId)
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,21 +3,28 @@ package de.walamana.views
|
||||
import de.walamana.models.Ministrant
|
||||
import de.walamana.models.MinistrantenDao
|
||||
import de.walamana.models.SimplifiedMinistrant
|
||||
import de.walamana.plugins.username
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.auth.*
|
||||
import io.ktor.server.auth.jwt.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.routing.*
|
||||
import io.ktor.server.util.*
|
||||
|
||||
fun Route.configureMinistrantenRoutes() {
|
||||
authenticate {
|
||||
route("/ministranten") {
|
||||
get {
|
||||
call.principal<JWTPrincipal>() ?: return@get call.respond(HttpStatusCode.Unauthorized)
|
||||
val data = MinistrantenDao.allMinistranten();
|
||||
call.respond(data)
|
||||
}
|
||||
put {
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@put call.respond(HttpStatusCode.Unauthorized)
|
||||
val data = call.receive<Ministrant>()
|
||||
if (principal.payload.username() != "admin") return@put call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Nur Admin kann Ministrianten erstellen"))
|
||||
val passwordHash = data.passwordHash ?: return@put
|
||||
val ministrant = MinistrantenDao.createMinistrant(
|
||||
data.username,
|
||||
@@ -30,11 +37,12 @@ fun Route.configureMinistrantenRoutes() {
|
||||
call.respond(mapOf("id" to ministrant?.id))
|
||||
}
|
||||
patch {
|
||||
// TODO: Access only by admin
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@patch call.respond(HttpStatusCode.Unauthorized)
|
||||
if (principal.payload.username() != "admin") return@patch call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Nur Admin kann Ministrianten aktualisieren"))
|
||||
val simple = false
|
||||
if(simple) {
|
||||
val data = call.receive<SimplifiedMinistrant>()
|
||||
val changed = MinistrantenDao.updateMinistrant(
|
||||
MinistrantenDao.updateMinistrant(
|
||||
id = data.id,
|
||||
username = data.username,
|
||||
firstname = data.firstname,
|
||||
@@ -42,7 +50,7 @@ fun Route.configureMinistrantenRoutes() {
|
||||
)
|
||||
}else{
|
||||
val data = call.receive<Ministrant>()
|
||||
val changed = MinistrantenDao.updateMinistrant(
|
||||
MinistrantenDao.updateMinistrant(
|
||||
data.id,
|
||||
data.username,
|
||||
null,
|
||||
@@ -55,9 +63,12 @@ fun Route.configureMinistrantenRoutes() {
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
delete {
|
||||
val id = call.request.queryParameters["id"]?.toInt() ?: TODO("fail")
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@delete call.respond(HttpStatusCode.Unauthorized)
|
||||
if (principal.payload.username() != "admin") return@delete call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Nur Admin kann Ministrianten löschen"))
|
||||
val id = call.request.queryParameters["id"]?.toInt() ?: return@delete call.respond(HttpStatusCode.BadRequest, mapOf("msg" to "Missing id parameter"))
|
||||
MinistrantenDao.deleteMinistrant(id)
|
||||
call.respond(HttpStatusCode.OK)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,9 @@ package de.walamana.views
|
||||
import de.walamana.models.GottesdienstGroup
|
||||
import de.walamana.models.GottesdienstGroupDao
|
||||
import de.walamana.models.GottesdienstGroups
|
||||
import de.walamana.plugins.getJWTEnvironment
|
||||
import de.walamana.plugins.username
|
||||
import de.walamana.service.PlanDao
|
||||
import io.ktor.http.*
|
||||
import io.ktor.server.application.*
|
||||
import io.ktor.server.auth.*
|
||||
import io.ktor.server.auth.jwt.*
|
||||
@@ -26,26 +27,36 @@ fun Route.configurePlanRoutes() {
|
||||
}
|
||||
}
|
||||
route("/groups") {
|
||||
authenticate {
|
||||
get {
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@get call.respond(HttpStatusCode.Unauthorized)
|
||||
if (principal.payload.username() != "admin") return@get call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Nur Admin kann Gruppen abfragen"))
|
||||
val offset = call.parameters.get("offset")?.toLong() ?: 0
|
||||
val amount = call.parameters.get("amount")?.toInt() ?: 10
|
||||
val groups = GottesdienstGroupDao.getPlans(amount, offset)
|
||||
call.respond(groups)
|
||||
}
|
||||
post {
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@post call.respond(HttpStatusCode.Unauthorized)
|
||||
if (principal.payload.username() != "admin") return@post call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Nur Admin kann Gruppen erstellen"))
|
||||
val group = call.receive<GottesdienstGroup>()
|
||||
val res = GottesdienstGroupDao.createPlan(group.from, group.to) ?: TODO("fail")
|
||||
call.respond(res)
|
||||
}
|
||||
patch {
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@patch call.respond(HttpStatusCode.Unauthorized)
|
||||
if (principal.payload.username() != "admin") return@patch call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Nur Admin kann Gruppen aktualisieren"))
|
||||
val group = call.receive<GottesdienstGroup>()
|
||||
val res = GottesdienstGroupDao.updatePlan(group) ?: TODO("fail")
|
||||
call.respond(res)
|
||||
}
|
||||
delete {
|
||||
val principal = call.principal<JWTPrincipal>() ?: return@delete call.respond(HttpStatusCode.Unauthorized)
|
||||
if (principal.payload.username() != "admin") return@delete call.respond(HttpStatusCode.Forbidden, mapOf("msg" to "Nur Admin kann Gruppen löschen"))
|
||||
val id = call.parameters.getOrFail("id").toInt()
|
||||
val res = GottesdienstGroupDao.deletePlan(id)
|
||||
call.respond(hashMapOf("success" to res))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user