Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed setPassword body is compressed #8790

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.unciv.logic.multiplayer.storage

import com.badlogic.gdx.Net
import com.badlogic.gdx.utils.Base64Coder
import com.unciv.ui.screens.savescreens.Gzip
import com.unciv.utils.debug
import kotlin.Exception

Expand Down Expand Up @@ -82,7 +81,7 @@ object UncivServerFileStorage : FileStorage {
return false

var setSuccessful = false
SimpleHttp.sendRequest(Net.HttpMethods.PUT, "$serverUrl/auth", content=Gzip.zip(newPassword), timeout=timeout, header=authHeader) {
SimpleHttp.sendRequest(Net.HttpMethods.PUT, "$serverUrl/auth", content=newPassword, timeout=timeout, header=authHeader) {
success, result, code ->
if (!success) {
debug("Error from UncivServer during password set: %s", result)
Expand Down
32 changes: 21 additions & 11 deletions server/src/com/unciv/app/server/UncivServer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import io.ktor.utils.io.jvm.javaio.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.io.File
import java.util.*
import java.util.concurrent.TimeUnit


Expand Down Expand Up @@ -74,15 +75,11 @@ private class UncivServerRunner : CliktCommand() {
* or the password is correct
*/
private fun validateGameAccess(file: File, authString: String?): Boolean {
if (!authV1Enabled || !file.exists())
if (!file.exists())
return true

// If auth is enabled, an auth string is required
if (authString == null || !authString.startsWith("Basic "))
return false

// Extract the user id and password from the auth string
val (userId, password) = authString.drop(6).split(":")
val (userId, password) = extractAuth(authString) ?: return false

if (authMap[userId] == null || authMap[userId] == password)
return true
Expand All @@ -95,15 +92,28 @@ private class UncivServerRunner : CliktCommand() {
private fun validateAuth(authString: String?): Boolean {
if (!authV1Enabled)
return true
// If auth is enabled a auth string is required
if (authString == null || !authString.startsWith("Basic "))
return false

val (userId, password) = authString.drop(6).split(":")
val (userId, password) = extractAuth(authString) ?: return false
if (authMap[userId] == null || authMap[userId] == password)
return true
return false
}

private fun extractAuth(authString: String?): Pair<String, String>? {
if (!authV1Enabled)
return null

// If auth is enabled a auth string is required
if (authString == null || !authString.startsWith("Basic "))
return null

val decodedString = String(Base64.getDecoder().decode(authString.drop(6)))
val splitAuthString = decodedString.split(":", limit=2)
if (splitAuthString.size != 2)
return null

return splitAuthString.let { it[0] to it[1] }
}
// endregion Auth

private fun serverRun(serverPort: Int, fileFolderName: String) {
Expand Down Expand Up @@ -159,7 +169,7 @@ private class UncivServerRunner : CliktCommand() {
log.info("Received auth password set from ${call.request.local.remoteHost}")
val authHeader = call.request.headers["Authorization"]
if (validateAuth(authHeader)) {
val userId = authHeader?.drop(6)?.split(":")?.get(0)
val (userId, _) = extractAuth(authHeader) ?: Pair(null, null)
if (userId != null) {
authMap[userId] = call.receiveText()
call.respond(HttpStatusCode.OK)
Expand Down