Skip to content

Commit

Permalink
fix auth not actually blocking requests (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
AriaMoradi authored Apr 6, 2022
1 parent f51818b commit 5a32ccf
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions server/src/main/kotlin/suwayomi/tachidesk/server/JavalinSetup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ object JavalinSetup {
}

config.enableCorsForAllOrigins()

config.accessManager { handler, ctx, _ ->
fun credentialsValid(): Boolean {
val (username, password) = ctx.basicAuthCredentials()
return username == serverConfig.basicAuthUsername && password == serverConfig.basicAuthPassword
}

if (serverConfig.basicAuthEnabled && !(ctx.basicAuthCredentialsExist() && credentialsValid())) {
ctx.header("WWW-Authenticate", "Basic")
ctx.status(401).json("Unauthorized")
} else {
handler.handle(ctx)
}
}
}.events { event ->
event.serverStarted {
if (serverConfig.initialOpenInBrowserEnabled) {
Expand Down Expand Up @@ -83,18 +97,6 @@ object JavalinSetup {
ctx.result(e.message ?: "Internal Server Error")
}

app.before { ctx ->
fun credentialsValid(): Boolean {
val (username, password) = ctx.basicAuthCredentials()
return username == serverConfig.basicAuthUsername && password == serverConfig.basicAuthPassword
}

if (serverConfig.basicAuthEnabled && !(ctx.basicAuthCredentialsExist() && credentialsValid())) {
ctx.header("WWW-Authenticate", "Basic")
ctx.status(401).json("Unauthorized")
}
}

app.routes {
path("api/v1/") {
GlobalAPI.defineEndpoints()
Expand Down

0 comments on commit 5a32ccf

Please sign in to comment.