Add basic auth

This commit is contained in:
2026-01-20 01:30:28 +01:00
parent c7b93842d3
commit 376630b68d
6 changed files with 101 additions and 48 deletions
+60 -31
View File
@@ -11,11 +11,17 @@ import java.io.File
import kotlin.random.Random
import kotlin.random.nextInt
import io.ktor.server.auth.*
import io.ktor.server.application.*
//átlagos JAVA alapú fejlesztő
val httpPort: Int = System.getenv("HTTP_PORT")?.toInt() ?: 8080
val resourcesPath: String = System.getenv("RESOURCES") ?: "src/main/resources/"
val adminUser: String = System.getenv("ADMIN_USER") ?: "foo"
val adminPass: String = System.getenv("ADMIN_PASSWORD") ?: "bar"
val bullshit = listOf<String>(
"That's a great question/opinion/response! ",
"I'm so proud of you for coming up with that. ",
@@ -49,11 +55,19 @@ val bullshit = listOf<String>(
"I eat cement. ",
"Answering these kinds of questions wasn't in my job description btw. ",
"Let's see... ",
"Let's see - hold on someone is calling - holy shit. A Second one? Oh my. Oh god. Anyways where were we? Right. ",
"Let's see - hold on someone is calling - it's for you, some 'Feri' I think. ",
"Let's see - hold on someone is calling - holy shit. A second one? Oh my. Oh god. That's horrible. Anyways where were we? Right. ",
"Let's see - hold on someone is calling - it's for you, some 'Feri' they say. ",
"Let's see what we have here. ",
"Really? You have to ask the computer to figure that one out? Alright man I can look into it... ",
"Let's take a look!",
"You should talk about this to a medical professional, but for now here is what I think: ",
"You kick Sc0tt? You kick his body like the Football? Jail for User! Jail for User for one thousand years! ",
"You're on the right track! ",
"You've almost got it! ",
"That is almost correct! ",
"Here are my 2 cents on this topic: ",
"I will piss on the moon. ",
"Here is what I think: ",
)
object ConversationHandler {
@@ -75,8 +89,6 @@ object ConversationHandler {
{
val stringBuilder = StringBuilder()
for (conversation in conversations)
{
stringBuilder.append(conversation.list())
@@ -105,7 +117,8 @@ class Conversation(val ID: Int)
fun list(): String
{
return """
return if(messages.isEmpty()) " "
else """
<div class="conversation">
<p style="flex-grow: 1;">$ID</p>
<p style="flex-grow: 9;">${messages.last().text}</p>
@@ -160,6 +173,7 @@ class Message(val user: Boolean, messagetext: String)
{
return """
<div class="message message-${if (user) "user" else "scott"}">
<p class="username">${if (user) "You asked:" else "Sc0tt answered:"}<p>
<p>$text</p>
</div>
""".trimIndent()
@@ -175,38 +189,53 @@ fun main(args: Array<String>) {
fun runEmbeddedServer()
{
embeddedServer(Netty, port = httpPort) {
install(Authentication) {
basic("auth-basic") {
realm = "Access to the '/admin' path"
validate { credentials ->
if (credentials.name == adminUser && credentials.password == adminPass) {
UserIdPrincipal(credentials.name)
} else {
null
}
}
}
}
routing {
authenticate("auth-basic") {
route("/admin")
{
get("/api/all_messages") {
call.respondText(ConversationHandler.listAllConversations())
}
get("/api/all_messages/{id}") {
ConversationHandler.conversations.find {
it.ID == call.parameters["id"]?.toInt()
}.also{ call.respondText(it?.sendAll(true) ?: "") }
}
put("/api/write/{id}")
{
try {
ConversationHandler.conversations.find {
it.ID == call.parameters["id"]?.toInt()
}?.write(Message(false, call.receiveText()))
call.respond(HttpStatusCode.OK)
} catch (e: Exception) { call.respond(HttpStatusCode.NotFound) }
}
}
}
staticFiles("/resources", File(resourcesPath))
staticFiles("/", File(resourcesPath+"/index.html"))
staticFiles("/admin", File(resourcesPath+"/admin.html"))
staticFiles("/about", File(resourcesPath+"/about.html"))
route("/admin")
{
get("/api/all_messages") {
call.respondText(ConversationHandler.listAllConversations())
}
get("/api/all_messages/{id}") {
ConversationHandler.conversations.find {
it.ID == call.parameters["id"]?.toInt()
}.also{ call.respondText(it?.sendAll(true) ?: "") }
}
put("/api/write/{id}")
{
try {
ConversationHandler.conversations.find {
it.ID == call.parameters["id"]?.toInt()
}?.write(Message(false, call.receiveText()))
call.respond(HttpStatusCode.OK)
} catch (e: Exception) { call.respond(HttpStatusCode.NotFound) }
}
}
put("/api/write/{id}")
{
try {