Implement emergency admin password for when NationStates is having a meltdown
authorLanius Trolling <lanius@laniustrolling.dev>
Mon, 22 Apr 2024 23:49:03 +0000 (19:49 -0400)
committerLanius Trolling <lanius@laniustrolling.dev>
Mon, 22 Apr 2024 23:49:03 +0000 (19:49 -0400)
src/jvmMain/kotlin/info/mechyrdia/Configuration.kt
src/jvmMain/kotlin/info/mechyrdia/auth/ViewsLogin.kt
src/jvmMain/kotlin/info/mechyrdia/auth/WebDav.kt
src/jvmMain/kotlin/info/mechyrdia/lore/ViewNav.kt
src/jvmMain/kotlin/info/mechyrdia/route/ResourceWebDav.kt

index e50b019cdd445b402b27df299573dcad0a46adf9..2e3eba0bd39139eafb0e50108d947b96673645cf 100644 (file)
@@ -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,
 ) {
index e34871475614db709d4954d02f3889f5802cd266..a9aa7f3034f0555fd6e8057a84992a6a802581f7 100644 (file)
@@ -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))
        
index 493dcfd9b37d7058a70a9e7197752d705298bbbe..4fde137d8e520f59086c34ca9d2e6cdd63392a41 100644 (file)
@@ -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 {
index dd906fb6b9377e31606d915775684adf17377baa..2aefb7c4838eba77d0a63cbb3f4103e5a78bcd6a 100644 (file)
@@ -62,7 +62,7 @@ suspend fun ApplicationCall.standardNavBar(path: List<String>? = 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())
 
index ccde0a00a2c6f51bbb82e01e88d55f4b5a9b3e81..215c6f158417ccd97f0817fb4e073841cdbec7ef 100644 (file)
@@ -152,7 +152,7 @@ suspend fun FileStorage.deleteWebDav(path: StoragePath): Boolean {
        }
 }
 
-val WebDavAttributeKey = AttributeKey<Boolean>("Mechyrdia.WebDav")
+val WebDavAttributeKey = AttributeKey<Boolean>("Mechyrdia.WebDAV")
 val ApplicationCall.isWebDav: Boolean
        get() = attributes.getOrNull(WebDavAttributeKey) == true