Skip to content

Commit

Permalink
Initial migration to javalin6
Browse files Browse the repository at this point in the history
  • Loading branch information
sauterl committed Mar 6, 2024
1 parent 995843b commit a7085e8
Show file tree
Hide file tree
Showing 8 changed files with 169 additions and 141 deletions.
10 changes: 4 additions & 6 deletions backend/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ compileTestKotlin {

dependencies {

def javalinOpenapi = '5.6.1'

///// Frontend files (produced by sub-project).
implementation frontendClasspath(project(path: ":frontend", configuration: 'frontendFiles'))

Expand All @@ -64,11 +62,11 @@ dependencies {

////// Javalin
implementation group: 'io.javalin', name: 'javalin', version: version_javalin
kapt("io.javalin.community.openapi:openapi-annotation-processor:$javalinOpenapi")
kapt("io.javalin.community.openapi:openapi-annotation-processor:$version_javalinopenapi")

implementation group: 'io.javalin.community.openapi', name: 'javalin-openapi-plugin', version: javalinOpenapi
implementation group: 'io.javalin.community.openapi', name:'javalin-swagger-plugin', version: javalinOpenapi
implementation group: 'io.javalin.community.ssl', name: 'ssl-plugin', version: version_javalin
implementation group: 'io.javalin.community.openapi', name: 'javalin-openapi-plugin', version: version_javalinopenapi
implementation group: 'io.javalin.community.openapi', name:'javalin-swagger-plugin', version: version_javalinopenapi
implementation group: 'io.javalin.community.ssl', name: 'ssl-plugin', version: version_javalinssl

////// Bcrypt
implementation group: 'org.mindrot', name: 'jbcrypt', version: version_bcrypt
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/kotlin/dev/dres/DRES.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import kotlin.system.exitProcess
*/
object DRES {
/** Version of DRES. */
const val VERSION = "2.0.0-RC4"
const val VERSION = "2.0.0-RC5"

/** Application root; should be relative to JAR file or classes path. */
val APPLICATION_ROOT: Path =
Expand Down
12 changes: 11 additions & 1 deletion backend/src/main/kotlin/dev/dres/api/rest/AccessManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ object AccessManager {
}
}

fun hasAccess(ctx: Context): Boolean {
val permittedRoles = ctx.routeRoles()
return when {
permittedRoles.isEmpty() -> true
permittedRoles.contains(ApiRole.ANYONE) -> true
rolesOfSession(ctx.sessionToken()).any { it in permittedRoles } -> true
else -> false
}
}

/** An internal [ConcurrentHashMap] that maps [SessionToken]s to [ApiRole]s. */
private val sessionRoleMap = HashMap<SessionToken, MutableSet<ApiRole>>()

Expand Down Expand Up @@ -155,4 +165,4 @@ object AccessManager {
fun getRunManagerForUser(userId: UserId): Set<RunManager> = this.locks.read {
return this.usersToRunMap[userId] ?: emptySet()
}
}
}
88 changes: 45 additions & 43 deletions backend/src/main/kotlin/dev/dres/api/rest/ClientOpenApiPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,57 @@ package dev.dres.api.rest
import com.fasterxml.jackson.databind.node.ObjectNode
import dev.dres.DRES
import io.javalin.openapi.CookieAuth
import io.javalin.openapi.plugin.OpenApiConfiguration
import io.javalin.openapi.plugin.OpenApiPlugin
import io.javalin.openapi.plugin.OpenApiPluginConfiguration
import io.javalin.openapi.plugin.SecurityComponentConfiguration

class ClientOpenApiPlugin : OpenApiPlugin(OpenApiPluginConfiguration()
.withDocumentationPath("/client-oas")
.withDefinitionConfiguration { _, u ->
u.withOpenApiInfo { t ->
t.title = "DRES Client API"
t.version = DRES.VERSION
t.description = "Client API for DRES (Distributed Retrieval Evaluation Server), Version ${DRES.VERSION}"
}
u.withSecurity(
SecurityComponentConfiguration()
.withSecurityScheme("CookieAuth", CookieAuth(AccessManager.SESSION_COOKIE_NAME))
)
u.withDefinitionProcessor { doc ->

val blacklist = setOf(
"/external/",
"/collection",
"/run",
"/audit",
"/mediaItem",
"/score",
"/user/list",
"/user/session/",
"/evaluation/admin",
"/evaluation/template",
"/evaluation/{evaluationId}/judge",
"/evaluation/{evaluationId}/vote",
"/evaluation/{evaluationId}/submission",
"/evaluation/{evaluationId}/task",
"/evaluation/{evaluationId}/{taskId}",
"/download",
"/mediaitem",
"/template",
"/preview",
"/status/info"
class ClientOpenApiPlugin : OpenApiPlugin({
it
.withDocumentationPath("/clientapi.json")
.withDefinitionConfiguration { _, u ->
u.withOpenApiInfo { t ->
t.title = "DRES Client API"
t.version = DRES.VERSION
t.description =
"Client API for DRES (Distributed Retrieval Evaluation Server), Version ${DRES.VERSION}"
}
u.withSecurity(
SecurityComponentConfiguration()
.withSecurityScheme("CookieAuth", CookieAuth(AccessManager.SESSION_COOKIE_NAME))
)
u.withDefinitionProcessor { doc ->

val blacklist = setOf(
"/external/",
"/collection",
"/run",
"/audit",
"/mediaItem",
"/score",
"/user/list",
"/user/session/",
"/evaluation/admin",
"/evaluation/template",
"/evaluation/{evaluationId}/judge",
"/evaluation/{evaluationId}/vote",
"/evaluation/{evaluationId}/submission",
"/evaluation/{evaluationId}/task",
"/evaluation/{evaluationId}/{taskId}",
"/download",
"/mediaitem",
"/template",
"/preview",
"/status/info"
)

val relevantRoutes =
doc["paths"].fields().asSequence().filter { blacklist.none { b -> it.key.contains(b) } }.map { it.key }
.toList()
val relevantRoutes =
doc["paths"].fields().asSequence()
.filter { blacklist.none { b -> it.key.contains(b) } }.map { it.key }
.toList()

(doc["paths"] as ObjectNode).retain(relevantRoutes)
(doc["paths"] as ObjectNode).retain(relevantRoutes)

doc.toPrettyString()
doc.toPrettyString()

}
}
})
})
40 changes: 25 additions & 15 deletions backend/src/main/kotlin/dev/dres/api/rest/ClientSwaggerPlugin.kt
Original file line number Diff line number Diff line change
@@ -1,27 +1,37 @@
package dev.dres.api.rest

import io.javalin.Javalin
import io.javalin.config.JavalinConfig
import io.javalin.openapi.plugin.swagger.SwaggerConfiguration
import io.javalin.openapi.plugin.swagger.SwaggerHandler
import io.javalin.openapi.plugin.swagger.SwaggerPlugin
import io.javalin.plugin.Plugin

class ClientSwaggerPlugin : Plugin {
override fun apply(app: Javalin) {
val swaggerHandler = SwaggerHandler(
title = "DRES Client API",
documentationPath = "/client-oas",
swaggerVersion = SwaggerConfiguration().version,
validatorUrl = "https://validator.swagger.io/validator",
routingPath = app.cfg.routing.contextPath,
basePath = null,
tagsSorter = "'alpha'",
operationsSorter = "'alpha'",
customJavaScriptFiles = emptyList(),
customStylesheetFiles = emptyList()
)
class ClientSwaggerPlugin : SwaggerPlugin() {

override fun name(): String {
return this.javaClass.simpleName
}

override fun onStart(config: JavalinConfig) {
// val swaggerHandler = SwaggerHandler(
// title = "DRES Client API",
// documentationPath = "/client-oas",
// swaggerVersion = SwaggerConfiguration().version,
// validatorUrl = "https://validator.swagger.io/validator",
//// routingPath = app.cfg.routing.contextPath,
// basePath = null,
// tagsSorter = "'alpha'",
// operationsSorter = "'alpha'",
// customJavaScriptFiles = emptyList(),
// customStylesheetFiles = emptyList()
// )
//
// config.router.apiBuilder {
// get("/swagger-client", swaggerHandler)
// }


app.get("/swagger-client", swaggerHandler)
}

}
Loading

0 comments on commit a7085e8

Please sign in to comment.