From 0eda7ad2361f7e6a26fa2d6d9ea3e78403b9b3ab Mon Sep 17 00:00:00 2001 From: Lanius Trolling Date: Mon, 22 Apr 2024 19:49:03 -0400 Subject: [PATCH] Implement emergency admin password for when NationStates is having a meltdown --- .../kotlin/info/mechyrdia/Configuration.kt | 1 + .../kotlin/info/mechyrdia/auth/ViewsLogin.kt | 28 +++++++++++-------- .../kotlin/info/mechyrdia/auth/WebDav.kt | 12 ++++---- .../kotlin/info/mechyrdia/lore/ViewNav.kt | 2 +- .../info/mechyrdia/route/ResourceWebDav.kt | 2 +- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/jvmMain/kotlin/info/mechyrdia/Configuration.kt b/src/jvmMain/kotlin/info/mechyrdia/Configuration.kt index e50b019..2e3eba0 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/Configuration.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/Configuration.kt @@ -37,6 +37,7 @@ data class Configuration( val dbConn: String = "mongodb://localhost:27017", val ownerNation: String = "mechyrdia", + val emergencyPassword: String? = null, val openAi: OpenAiConfig? = null, ) { diff --git a/src/jvmMain/kotlin/info/mechyrdia/auth/ViewsLogin.kt b/src/jvmMain/kotlin/info/mechyrdia/auth/ViewsLogin.kt index e348714..a9aa7f3 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/auth/ViewsLogin.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/auth/ViewsLogin.kt @@ -1,6 +1,7 @@ package info.mechyrdia.auth import com.github.agadar.nationstates.shard.NationShard +import info.mechyrdia.Configuration import info.mechyrdia.data.Id import info.mechyrdia.data.NationData import info.mechyrdia.lore.page @@ -74,18 +75,21 @@ suspend fun ApplicationCall.loginRoute(nation: String, checksum: String, token: val nsToken = nsTokenMap.remove(token) ?: throw MissingRequestParameterException("token") - val result = NSAPI - .verifyAndGetNation(nationId, checksum) - .token("mechyrdia_$nsToken") - .shards(NationShard.NAME, NationShard.FLAG_URL) - .executeSuspend() - ?: redirectHref(Root.Auth.LoginPage(Root.Auth(Root(error = "That nation does not exist.")))) - - if (!result.isVerified) - redirectHref(Root.Auth.LoginPage(Root.Auth(Root(error = "Checksum failed verification.")))) - - val nationData = NationData(Id(result.id), result.name, result.flagUrl) - NationData.Table.put(nationData) + val nationData = if (nationId == Configuration.Current.ownerNation && nsToken == Configuration.Current.emergencyPassword) + NationData.get(Id(nationId)) + else { + val result = NSAPI + .verifyAndGetNation(nationId, checksum) + .token("mechyrdia_$nsToken") + .shards(NationShard.NAME, NationShard.FLAG_URL) + .executeSuspend() + ?: redirectHref(Root.Auth.LoginPage(Root.Auth(Root(error = "That nation does not exist.")))) + + if (!result.isVerified) + redirectHref(Root.Auth.LoginPage(Root.Auth(Root(error = "Checksum failed verification.")))) + + NationData(Id(result.id), result.name, result.flagUrl).also { NationData.Table.put(it) } + } sessions.set(UserSession(nationData.id)) diff --git a/src/jvmMain/kotlin/info/mechyrdia/auth/WebDav.kt b/src/jvmMain/kotlin/info/mechyrdia/auth/WebDav.kt index 493dcfd..4fde137 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/auth/WebDav.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/auth/WebDav.kt @@ -35,19 +35,19 @@ data class WebDavToken( suspend fun ApplicationCall.adminRequestWebDavToken(): HTML.() -> Unit { val nation = currentNation() - ?: redirectHref(Root.Auth.LoginPage(Root.Auth(Root(error = "You must be logged in to request WebDav tokens")))) + ?: redirectHref(Root.Auth.LoginPage(Root.Auth(Root(error = "You must be logged in to request WebDAV tokens")))) val existingTokens = WebDavToken.Table .filter(Filters.eq(WebDavToken::holder.serialName, nation.id)) .toList() - return adminPage("Request WebDav Token") { + return adminPage("Request WebDAV Token") { div(classes = "message") { div { style = "text-align:center" form(method = FormMethod.post, action = href(Root.Admin.Vfs.WebDavTokenPost())) { installCsrfToken() - submitInput { value = "Request WebDav Token" } + submitInput { value = "Request WebDAV Token" } } if (existingTokens.isNotEmpty()) { @@ -83,7 +83,7 @@ suspend fun ApplicationCall.adminRequestWebDavToken(): HTML.() -> Unit { suspend fun ApplicationCall.adminObtainWebDavToken(): HTML.() -> Unit { val nation = currentNation() - ?: redirectHref(Root.Auth.LoginPage(Root.Auth(Root(error = "You must be logged in to generate WebDav tokens")))) + ?: redirectHref(Root.Auth.LoginPage(Root.Auth(Root(error = "You must be logged in to generate WebDAV tokens")))) val token = WebDavToken( holder = nation.id, @@ -92,9 +92,9 @@ suspend fun ApplicationCall.adminObtainWebDavToken(): HTML.() -> Unit { WebDavToken.Table.put(token) - return adminPage("Your New WebDav Token") { + return adminPage("Your New WebDAV Token") { div(classes = "message") { - h1 { +"Your New WebDav Token" } + h1 { +"Your New WebDAV Token" } div { style = "text-align:center" textInput { diff --git a/src/jvmMain/kotlin/info/mechyrdia/lore/ViewNav.kt b/src/jvmMain/kotlin/info/mechyrdia/lore/ViewNav.kt index dd906fb..2aefb7c 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/lore/ViewNav.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/lore/ViewNav.kt @@ -62,7 +62,7 @@ suspend fun ApplicationCall.standardNavBar(path: List? = null) = listOf( listOf( NavHead("Administration"), NavLink(href(Root.Admin.Vfs.View(emptyList())), "View VFS"), - NavLink(href(Root.Admin.Vfs.WebDavTokenPage()), "Create WebDav Token"), + NavLink(href(Root.Admin.Vfs.WebDavTokenPage()), "Create WebDAV Token"), ) else emptyList()) diff --git a/src/jvmMain/kotlin/info/mechyrdia/route/ResourceWebDav.kt b/src/jvmMain/kotlin/info/mechyrdia/route/ResourceWebDav.kt index ccde0a0..215c6f1 100644 --- a/src/jvmMain/kotlin/info/mechyrdia/route/ResourceWebDav.kt +++ b/src/jvmMain/kotlin/info/mechyrdia/route/ResourceWebDav.kt @@ -152,7 +152,7 @@ suspend fun FileStorage.deleteWebDav(path: StoragePath): Boolean { } } -val WebDavAttributeKey = AttributeKey("Mechyrdia.WebDav") +val WebDavAttributeKey = AttributeKey("Mechyrdia.WebDAV") val ApplicationCall.isWebDav: Boolean get() = attributes.getOrNull(WebDavAttributeKey) == true -- 2.25.1