Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/jarryd229/void
Browse files Browse the repository at this point in the history
  • Loading branch information
jarryd229 committed Aug 4, 2024
2 parents 5e62ddd + e40ddc2 commit 7ddb712
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 28 deletions.
12 changes: 6 additions & 6 deletions data/spawns/npc-spawns.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1221,12 +1221,12 @@
- { id: white_wolf, x: 2872, y: 3445, members: true }
- { id: white_wolf, x: 2870, y: 3438, members: true }
# 11318
- { id: wolf2, x: 2844, y: 3507, members: true }
- { id: wolf2, x: 2839, y: 3506, members: true }
- { id: wolf2, x: 2842, y: 3504, members: true }
- { id: wolf2, x: 2839, y: 3502, members: true }
- { id: wolf2, x: 2837, y: 3499, members: true }
- { id: wolf2, x: 2836, y: 3495, members: true }
- { id: white_wolf_2, x: 2844, y: 3507, members: true }
- { id: white_wolf_2, x: 2839, y: 3506, members: true }
- { id: white_wolf_2, x: 2842, y: 3504, members: true }
- { id: white_wolf_2, x: 2839, y: 3502, members: true }
- { id: white_wolf_2, x: 2837, y: 3499, members: true }
- { id: white_wolf_2, x: 2836, y: 3495, members: true }
- { id: big_wolf_2, x: 2840, y: 3497, members: true }
- { id: white_wolf, x: 2866, y: 3498, members: true }
- { id: white_wolf, x: 2860, y: 3492, members: true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class PlayerAccountLoader(
) : AccountLoader {
private val logger = InlineLogger()

override fun exists(username: String): Boolean {
return storage.exists(username)
}

override fun password(username: String): String? {
return accountDefinitions.get(username)?.passwordHash
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ class AccountDefinitions(
) {

fun add(player: Player) {
displayNames[player.accountName] = player.name
definitions[player.name] = AccountDefinition(player.accountName, player.name, player.previousName, player.passwordHash)
clans[player.name] = Clan(
displayNames[player.accountName.lowercase()] = player.name
definitions[player.name.lowercase()] = AccountDefinition(player.accountName, player.name, player.previousName, player.passwordHash)
clans[player.name.lowercase()] = Clan(
owner = player.accountName,
ownerDisplayName = player.name,
name = player["clan_name", ""],
Expand All @@ -38,30 +38,34 @@ class AccountDefinitions(
}

fun update(accountName: String, newName: String, previousDisplayName: String) {
val definition = definitions.remove(previousDisplayName) ?: return
definitions[newName] = definition
val definition = definitions.remove(previousDisplayName.lowercase()) ?: return
definitions[newName.lowercase()] = definition
definition.displayName = newName
definition.previousName = previousDisplayName
displayNames[accountName] = newName
displayNames[accountName.lowercase()] = newName
}

fun clan(displayName: String) = clans[displayName]
fun clan(displayName: String) = clans[displayName.lowercase()]

fun getByAccount(account: String): AccountDefinition? {
return get(displayNames[account] ?: return null)
return get(displayNames[account.lowercase()] ?: return null)
}

fun get(key: String) = definitions[key]
fun get(key: String) = definitions[key.lowercase()]

fun getValue(key: String) = definitions.getValue(key)
fun getValue(key: String) = definitions.getValue(key.lowercase())

fun load(storage: AccountStorage = get()): AccountDefinitions {
timedLoad("account") {
definitions.putAll(storage.names())
for ((name, definition) in storage.names()) {
definitions[name.lowercase()] = definition
}
for (def in definitions.values) {
displayNames[def.accountName] = def.displayName
displayNames[def.accountName.lowercase()] = def.displayName
}
for ((name, definition) in storage.clans()) {
clans[name.lowercase()] = definition
}
clans.putAll(storage.clans())
definitions.size
}
return this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,6 @@ class FileStorage(
}

companion object {
private fun fileName(name: String) = "${name}.json"
private fun fileName(name: String) = "${name.lowercase()}.json"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import world.gregs.voidps.network.client.Client
import world.gregs.voidps.network.client.Instruction

/**
* Loads and setups account from data on file
* Loads and checks existing accounts
*/
interface AccountLoader {
fun exists(username: String): Boolean

fun password(username: String): String?

suspend fun load(client: Client, username: String, passwordHash: String, displayMode: Int): SendChannel<Instruction>?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,36 @@ import world.gregs.voidps.network.Response
/**
* Checks account credentials are valid
*/
class PasswordManager(private val loader: AccountLoader) {
class PasswordManager(private val account: AccountLoader) {

fun validate(username: String, password: String): Int {
if (username.length > 12) {
return Response.LOGIN_SERVER_REJECTED_SESSION
}
val passwordHash = loader.password(username)
val passwordHash = account.password(username)
if (!account.exists(username)) {
if (passwordHash != null) {
// Failed to find account file despite AccountDefinition exists in memory (aka existed on startup)
return Response.ACCOUNT_DISABLED
}
return Response.SUCCESS
}
try {
if (passwordHash != null && !BCrypt.checkpw(password, passwordHash)) {
return Response.INVALID_CREDENTIALS
if (passwordHash == null) {
// Failed to find accounts password despite account file existing (created since startup)
return Response.ACCOUNT_DISABLED
}
if (BCrypt.checkpw(password, passwordHash)) {
return Response.SUCCESS
}
} catch (e: IllegalArgumentException) {
return Response.COULD_NOT_COMPLETE_LOGIN
}
return Response.SUCCESS
return Response.INVALID_CREDENTIALS
}

fun encrypt(username: String, password: String): String {
val passwordHash = loader.password(username)
val passwordHash = account.password(username)
if (passwordHash != null) {
return passwordHash
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,11 @@ internal class LoginServerTest {
@BeforeEach
fun setup() {
client = null
password = null
password = "\$2a\$10${"$"}iIdTrtrJ5ibgFcJToZW7ueGkymDed2Ws2FoE8JnrXPGiY2YNVa9y6"
instructions = Channel(capacity = 1)
accounts = object : AccountLoader {
override fun exists(username: String) = true

override fun password(username: String) = password

override suspend fun load(client: Client, username: String, passwordHash: String, displayMode: Int): SendChannel<Instruction> {
Expand Down Expand Up @@ -224,13 +226,17 @@ internal class LoginServerTest {
val readChannel = ByteChannel(autoFlush = true)
val writeChannel = ByteChannel(autoFlush = true)
accounts = object : AccountLoader {

override fun exists(username: String) = false

override fun password(username: String) = null

override suspend fun load(client: Client, username: String, passwordHash: String, displayMode: Int): SendChannel<Instruction>? {
client.disconnect(Response.ACCOUNT_ONLINE)
return null
}
}
passwordManager = PasswordManager(accounts)
server = LoginServer(protocol, 123, 10, modulus, BigInteger("10001", 16), accounts, passwordManager)
launch {
server.connect(readChannel, writeChannel, "localhost")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,23 @@ class PasswordManagerTest {
fun `New player with no password is successful`() {
val username = "test"
val password = "wrongPassword"
accountLoader.exists = false

val result = passwordManager.validate(username, password)

assertEquals(Response.SUCCESS, result)
}

@Test
fun `Existing player with no password is disabled`() {
val username = "test"
val password = "password"

val result = passwordManager.validate(username, password)

assertEquals(Response.ACCOUNT_DISABLED, result)
}

@Test
fun `Long username is rejected`() {
val username = "veryLongUsername"
Expand Down Expand Up @@ -91,6 +102,11 @@ class PasswordManagerTest {

private class TestAccountLoader : AccountLoader {
val accountMap = mutableMapOf<String, String>()
var exists = true

override fun exists(username: String): Boolean {
return exists
}

override fun password(username: String): String? {
return accountMap[username]
Expand Down

0 comments on commit 7ddb712

Please sign in to comment.