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-core:$ktor_version")
implementation("io.ktor:ktor-server-netty:$ktor_version") implementation("io.ktor:ktor-server-netty:$ktor_version")
implementation("io.ktor:ktor-server-sessions:${ktor_version}") implementation("io.ktor:ktor-server-sessions:${ktor_version}")
implementation("io.ktor:ktor-server-auth:${ktor_version}")
} }
tasks.test { tasks.test {

View File

@@ -11,11 +11,17 @@ import java.io.File
import kotlin.random.Random import kotlin.random.Random
import kotlin.random.nextInt import kotlin.random.nextInt
import io.ktor.server.auth.*
import io.ktor.server.application.*
//átlagos JAVA alapú fejlesztő //átlagos JAVA alapú fejlesztő
val httpPort: Int = System.getenv("HTTP_PORT")?.toInt() ?: 8080 val httpPort: Int = System.getenv("HTTP_PORT")?.toInt() ?: 8080
val resourcesPath: String = System.getenv("RESOURCES") ?: "src/main/resources/" 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>( val bullshit = listOf<String>(
"That's a great question/opinion/response! ", "That's a great question/opinion/response! ",
"I'm so proud of you for coming up with that. ", "I'm so proud of you for coming up with that. ",
@@ -49,11 +55,19 @@ val bullshit = listOf<String>(
"I eat cement. ", "I eat cement. ",
"Answering these kinds of questions wasn't in my job description btw. ", "Answering these kinds of questions wasn't in my job description btw. ",
"Let's see... ", "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 - 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' I think. ", "Let's see - hold on someone is calling - it's for you, some 'Feri' they say. ",
"Let's see what we have here. ", "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... ", "Really? You have to ask the computer to figure that one out? Alright man I can look into it... ",
"Let's take a look!", "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 { object ConversationHandler {
@@ -75,8 +89,6 @@ object ConversationHandler {
{ {
val stringBuilder = StringBuilder() val stringBuilder = StringBuilder()
for (conversation in conversations) for (conversation in conversations)
{ {
stringBuilder.append(conversation.list()) stringBuilder.append(conversation.list())
@@ -105,7 +117,8 @@ class Conversation(val ID: Int)
fun list(): String fun list(): String
{ {
return """ return if(messages.isEmpty()) " "
else """
<div class="conversation"> <div class="conversation">
<p style="flex-grow: 1;">$ID</p> <p style="flex-grow: 1;">$ID</p>
<p style="flex-grow: 9;">${messages.last().text}</p> <p style="flex-grow: 9;">${messages.last().text}</p>
@@ -160,6 +173,7 @@ class Message(val user: Boolean, messagetext: String)
{ {
return """ return """
<div class="message message-${if (user) "user" else "scott"}"> <div class="message message-${if (user) "user" else "scott"}">
<p class="username">${if (user) "You asked:" else "Sc0tt answered:"}<p>
<p>$text</p> <p>$text</p>
</div> </div>
""".trimIndent() """.trimIndent()
@@ -175,12 +189,21 @@ fun main(args: Array<String>) {
fun runEmbeddedServer() fun runEmbeddedServer()
{ {
embeddedServer(Netty, port = httpPort) { embeddedServer(Netty, port = httpPort) {
routing { install(Authentication) {
staticFiles("/resources", File(resourcesPath)) basic("auth-basic") {
staticFiles("/", File(resourcesPath+"/index.html")) realm = "Access to the '/admin' path"
staticFiles("/admin", File(resourcesPath+"/admin.html")) validate { credentials ->
staticFiles("/about", File(resourcesPath+"/about.html")) if (credentials.name == adminUser && credentials.password == adminPass) {
UserIdPrincipal(credentials.name)
} else {
null
}
}
}
}
routing {
authenticate("auth-basic") {
route("/admin") route("/admin")
{ {
get("/api/all_messages") { get("/api/all_messages") {
@@ -206,6 +229,12 @@ fun runEmbeddedServer()
} }
} }
}
staticFiles("/resources", File(resourcesPath))
staticFiles("/", File(resourcesPath+"/index.html"))
staticFiles("/admin", File(resourcesPath+"/admin.html"))
staticFiles("/about", File(resourcesPath+"/about.html"))
put("/api/write/{id}") put("/api/write/{id}")
{ {

View File

@@ -7,7 +7,7 @@
<link rel="stylesheet" href="/resources/style.css"> <link rel="stylesheet" href="/resources/style.css">
</head> </head>
<body> <body style="margin: 0; height: 100%; overflow: hidden;">
<div class="main-flexbox"> <div class="main-flexbox">
<header class="navbar"> <header class="navbar">
@@ -53,8 +53,8 @@
</h2> </h2>
<p> <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 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 data in a database is a good thing, actually. Very marketable. 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> </p>
<br> <br>

View File

@@ -57,7 +57,7 @@
}) })
.then(data => { .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) console.log(data.body)
}) })

View File

@@ -7,13 +7,14 @@
<link rel="stylesheet" href="/resources/style.css"> <link rel="stylesheet" href="/resources/style.css">
</head> </head>
<body onload="javascript:init()"> <body onload="javascript:init()" style="margin: 0; height: 100%; overflow: hidden;">
<div class="main-flexbox"> <div class="main-flexbox">
<header class="navbar"> <header class="navbar">
<a href="https://sc0tt.org/" style="float: left" target="_blank">Sc0tt főoldal</a> <a href="https://sc0tt.org/" style="float: left" target="_blank">Sc0tt főoldal</a>
<a href="/about" style="float: left">About</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;"> <img src="resources/logo.png" alt=logo" style="float: right;">
@@ -24,9 +25,16 @@
</a> </a>
</header> </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 id="messages" class="messages">
</div> </div>
<footer> <footer>
<div class="textbox"> <div class="textbox">
<input class="text_input" name="valami" type="text" id="promptText" placeholder="Kérdezz az OSI modellről... vagy idk..."> <input class="text_input" name="valami" type="text" id="promptText" placeholder="Kérdezz az OSI modellről... vagy idk...">
@@ -36,9 +44,21 @@
</div> </div>
<script> <script>
let id; 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) { function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)); return new Promise(resolve => setTimeout(resolve, ms));
} }
@@ -113,7 +133,7 @@
} }
}); });
//document.getElementById("promptText").value = ""; document.getElementById("promptText").value = "";
await sleep(10); await sleep(10);
fetch_data("messages", false); fetch_data("messages", false);

View File

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