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

View File

@@ -25,6 +25,7 @@ dependencies {
implementation("io.ktor:ktor-server-core:$ktor_version")
implementation("io.ktor:ktor-server-netty:$ktor_version")
implementation("io.ktor:ktor-server-sessions:${ktor_version}")
implementation("io.ktor:ktor-server-auth:${ktor_version}")
}
tasks.test {

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 {

View File

@@ -7,7 +7,7 @@
<link rel="stylesheet" href="/resources/style.css">
</head>
<body>
<body style="margin: 0; height: 100%; overflow: hidden;">
<div class="main-flexbox">
<header class="navbar">
@@ -53,8 +53,8 @@
</h2>
<p>
Powered by an incredibly abstract, high level language, our backend is built from the ground up with privacy in mind. Our developer, after trying
to use more than 1 Gradle dependency in the backend realized, that not storing user data in a database is a good thing, actually. Very marketable.
Powered by the incredibly abstract, high level Kotlin language, our backend is built from the ground up with privacy in mind. Our developer, after trying
to use more than 1 Gradle dependency in the backend realized, that not storing user messages in a database is a good thing, actually. Very marketable.
</p>
<br>

View File

@@ -57,7 +57,7 @@
})
.then(data => {
document.getElementById("messages").innerHTML = data + '<footer> <div class="textbox"><input class="text_input" name="valami" type="text" id="promptText" placeholder="answ"><button class="send_button" onclick="send_data()">send</button><button class="send_button" onclick="open_message(' + answid + ')">reload</button></div></footer>';
document.getElementById("messages").innerHTML = '<footer> <div class="textbox"><input class="text_input" name="valami" type="text" id="promptText" placeholder="answ"><button class="send_button" onclick="send_data()">send</button><button class="send_button" onclick="open_message(' + answid + ')">reload</button></div></footer>' + data;
console.log(data.body)
})

View File

@@ -7,13 +7,14 @@
<link rel="stylesheet" href="/resources/style.css">
</head>
<body onload="javascript:init()">
<body onload="javascript:init()" style="margin: 0; height: 100%; overflow: hidden;">
<div class="main-flexbox">
<header class="navbar">
<a href="https://sc0tt.org/" style="float: left" target="_blank">Sc0tt főoldal</a>
<a href="/about" style="float: left">About</a>
<button onclick="genZ()" style="float: left;">Gen Z compatible site</button>
<img src="resources/logo.png" alt=logo" style="float: right;">
@@ -24,9 +25,16 @@
</a>
</header>
<iframe width="420" height="315"
src="https://www.youtube.com/embed/mn-Tlb_wfjc"
id="funniez"
style="display: none;">
</iframe>
<div id="messages" class="messages">
</div>
<footer>
<div class="textbox">
<input class="text_input" name="valami" type="text" id="promptText" placeholder="Kérdezz az OSI modellről... vagy idk...">
@@ -36,9 +44,21 @@
</div>
<script>
let id;
function genZ() {
var x = document.getElementById("funniez");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
@@ -113,7 +133,7 @@
}
});
//document.getElementById("promptText").value = "";
document.getElementById("promptText").value = "";
await sleep(10);
fetch_data("messages", false);

View File

@@ -2,7 +2,6 @@ body {
background: #212121;
color: white;
font-family: Roboto, sans-serif;
margin: 0; height: 100%; overflow: hidden;
}
.about-content {
@@ -16,6 +15,11 @@ body {
font-size: 50px;
}
.username {
font-size: 80%;
font-weight: bold;
}
.main-content {
/*
display: flex;
@@ -51,11 +55,11 @@ body {
}
.message-user {
background: black;
background: #383838;
}
.message-scott {
background: grey;
background: #5e5e5e;
}
.messages {
@@ -142,25 +146,21 @@ body {
.navbar {
width: 100%;
z-index: 5;
position: fixed;
padding: 10px;
background: #212121;
top: 0;
flex-grow: 1;
}
footer {
width: 100%;
position: fixed;
padding: 10px;
background: #212121;
bottom: 0;
}
.navbar a {
.navbar a, button {
border: none;
background: none;
display: block;
color: white;
text-align: center;
@@ -170,14 +170,17 @@ footer {
border-radius: 5px;
}
.navbar img {
height: 32px;
margin: auto 5px;
}
.navbar a:hover {
.navbar a:hover, button:hover {
background-color: #545454;
}
.navbar a.icon {
display: none;
}