Compare commits

...

2 commits

Author SHA1 Message Date
moehreag 76daadaebe move to correct package 2024-06-17 13:03:46 +02:00
moehreag 3b8c103ec1 add logging for failed authentication 2024-06-17 13:00:45 +02:00
12 changed files with 40 additions and 40 deletions

View file

@ -14,7 +14,7 @@ group = "dev.frogmc"
version = "0.0.1" version = "0.0.1"
application { application {
mainClass.set("dev.frogmc.ApplicationKt") mainClass.set("dev.frogmc.meta.ApplicationKt")
val isDevelopment: Boolean = project.ext.has("development") val isDevelopment: Boolean = project.ext.has("development")
applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment") applicationDefaultJvmArgs = listOf("-Dio.ktor.development=$isDevelopment")

View file

@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
zipStoreBase=GRADLE_USER_HOME zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

View file

@ -1,6 +1,8 @@
package dev.frogmc package dev.frogmc.meta
import dev.frogmc.plugins.* import dev.frogmc.meta.plugins.configureHTTP
import dev.frogmc.meta.plugins.configureRouting
import dev.frogmc.meta.plugins.configureSerialization
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.engine.* import io.ktor.server.engine.*
import io.ktor.server.netty.* import io.ktor.server.netty.*

View file

@ -1,4 +1,4 @@
package dev.frogmc package dev.frogmc.meta
object Config { object Config {
val POSTGRES_DATABASE = getEnv("DATABASE", "frogmc") val POSTGRES_DATABASE = getEnv("DATABASE", "frogmc")

View file

@ -1,6 +1,8 @@
package dev.frogmc package dev.frogmc.meta
import dev.frogmc.types.* import dev.frogmc.meta.types.LibraryVersion
import dev.frogmc.meta.types.LibraryVersions
import dev.frogmc.meta.types.LoaderVersions
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.transactions.transaction
@ -21,8 +23,7 @@ object DB {
transaction(db) { transaction(db) {
SchemaUtils.create( SchemaUtils.create(
LoaderVersions, LoaderVersions,
LibraryVersions, LibraryVersions
File
) )
} }
if (db == null) { if (db == null) {

View file

@ -0,0 +1,18 @@
package dev.frogmc.meta.plugins
import dev.frogmc.meta.Config
import dev.frogmc.meta.logger
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import java.security.MessageDigest
val authPlugin = createRouteScopedPlugin("auth") {
onCall {
val authorization = it.request.headers["Authorization"]
if (authorization.isNullOrEmpty() || !MessageDigest.isEqual(authorization.toByteArray(), Config.UPLOAD_SECRET)) {
logger.info("Authentication Failed: provided: ${authorization?.toByteArray()}; expected: ${Config.UPLOAD_SECRET}")
it.respond(HttpStatusCode.Unauthorized)
}
}
}

View file

@ -1,9 +1,8 @@
package dev.frogmc.plugins package dev.frogmc.meta.plugins
import io.ktor.http.* import io.ktor.http.*
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.plugins.cors.routing.* import io.ktor.server.plugins.cors.routing.*
import io.ktor.server.response.*
fun Application.configureHTTP() { fun Application.configureHTTP() {
install(CORS) { install(CORS) {
@ -12,7 +11,5 @@ fun Application.configureHTTP() {
allowMethod(HttpMethod.Delete) allowMethod(HttpMethod.Delete)
allowMethod(HttpMethod.Patch) allowMethod(HttpMethod.Patch)
allowHeader(HttpHeaders.Authorization) allowHeader(HttpHeaders.Authorization)
allowHeader("MyCustomHeader")
anyHost() // @TODO: Don't do this in production if possible. Try to limit it.
} }
} }

View file

@ -1,21 +1,19 @@
package dev.frogmc.plugins package dev.frogmc.meta.plugins
import dev.frogmc.DB import dev.frogmc.meta.DB
import dev.frogmc.types.LoaderVersion import dev.frogmc.meta.types.LoaderVersion
import dev.frogmc.types.PartialLoaderVersion import dev.frogmc.meta.types.PartialLoaderVersion
import dev.frogmc.types.LoaderVersions import dev.frogmc.meta.types.LoaderVersions
import dev.frogmc.types.ModrinthVersion import dev.frogmc.meta.types.ModrinthVersion
import io.ktor.client.* import io.ktor.client.*
import io.ktor.client.call.* import io.ktor.client.call.*
import io.ktor.client.request.* import io.ktor.client.request.*
import io.ktor.http.* import io.ktor.http.*
import io.ktor.server.application.* import io.ktor.server.application.*
import io.ktor.server.html.*
import io.ktor.server.http.content.* import io.ktor.server.http.content.*
import io.ktor.server.request.* import io.ktor.server.request.*
import io.ktor.server.response.* import io.ktor.server.response.*
import io.ktor.server.routing.* import io.ktor.server.routing.*
import kotlinx.html.*
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.jetbrains.exposed.exceptions.ExposedSQLException import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.jetbrains.exposed.sql.* import org.jetbrains.exposed.sql.*

View file

@ -1,4 +1,4 @@
package dev.frogmc.plugins package dev.frogmc.meta.plugins
import io.ktor.serialization.kotlinx.json.* import io.ktor.serialization.kotlinx.json.*
import io.ktor.server.application.* import io.ktor.server.application.*

View file

@ -1,4 +1,4 @@
package dev.frogmc.types package dev.frogmc.meta.types
import kotlinx.datetime.LocalDateTime import kotlinx.datetime.LocalDateTime
import kotlinx.serialization.Serializable import kotlinx.serialization.Serializable

View file

@ -1,4 +1,4 @@
package dev.frogmc.types package dev.frogmc.meta.types
import kotlinx.serialization.json.Json import kotlinx.serialization.json.Json
import org.jetbrains.exposed.sql.Table import org.jetbrains.exposed.sql.Table

View file

@ -1,16 +0,0 @@
package dev.frogmc.plugins
import dev.frogmc.Config
import io.ktor.http.*
import io.ktor.server.application.*
import io.ktor.server.response.*
import java.nio.charset.StandardCharsets
import java.security.MessageDigest
val authPlugin = createRouteScopedPlugin("auth") {
onCall {
val authorization = it.request.headers["Authorization"]
if (authorization.isNullOrEmpty() || !MessageDigest.isEqual(authorization.toByteArray(), Config.UPLOAD_SECRET))
it.respond(HttpStatusCode.Unauthorized);
}
}