From dbd01e18926d0978e1daf518f611df789b0afe76 Mon Sep 17 00:00:00 2001 From: ceikry Date: Sat, 30 Apr 2022 16:28:20 -0500 Subject: [PATCH] auth stuff --- .../kotlin/rs09/auth/InMemoryAuthProvider.kt | 20 +++++++++++ .../main/kotlin/rs09/auth/LoginResponse.kt | 27 +++++++++++++++ .../kotlin/core/auth/AuthenticatorTests.kt | 33 +++++++++++++++++++ 3 files changed, 80 insertions(+) create mode 100644 Server/src/main/kotlin/rs09/auth/InMemoryAuthProvider.kt create mode 100644 Server/src/main/kotlin/rs09/auth/LoginResponse.kt create mode 100644 Server/src/test/kotlin/core/auth/AuthenticatorTests.kt diff --git a/Server/src/main/kotlin/rs09/auth/InMemoryAuthProvider.kt b/Server/src/main/kotlin/rs09/auth/InMemoryAuthProvider.kt new file mode 100644 index 000000000..486599d25 --- /dev/null +++ b/Server/src/main/kotlin/rs09/auth/InMemoryAuthProvider.kt @@ -0,0 +1,20 @@ +package rs09.auth + +import rs09.storage.InMemoryStorageProvider + +class InMemoryAuthProvider : AuthProvider() { + override fun configureFor(provider: InMemoryStorageProvider) { + storageProvider = provider + } + + override fun checkLogin(username: String, password: String): LoginResponse { + val info = storageProvider.getAccountInfo(username) + if(info.online || info.password != password || info.username != username) return LoginResponse.CouldNotLogin + return LoginResponse.Success + } + + override fun createAccountWith(info: UserAccountInfo): Boolean { + storageProvider.store(info) + return true + } +} diff --git a/Server/src/main/kotlin/rs09/auth/LoginResponse.kt b/Server/src/main/kotlin/rs09/auth/LoginResponse.kt new file mode 100644 index 000000000..16d8689d8 --- /dev/null +++ b/Server/src/main/kotlin/rs09/auth/LoginResponse.kt @@ -0,0 +1,27 @@ +package rs09.auth + +enum class LoginResponse { + UnexpectedError, + CouldNotAd, + Success, + InvalidCredentials, + AccountDisabled, + AlreadyOnline, + Updated, + FullWorld, + LoginServerOffline, + LoginLimitExceeded, + BadSessionID, + WeakPassword, + MembersWorld, + CouldNotLogin, + Updating, + TooManyIncorrectLogins, + StandingInMembersArea, + AccountLocked, + ClosedBeta, + InvalidLoginServer, + MovingWorld, + ErrorLoadingProfile, + BannedUser +} diff --git a/Server/src/test/kotlin/core/auth/AuthenticatorTests.kt b/Server/src/test/kotlin/core/auth/AuthenticatorTests.kt new file mode 100644 index 000000000..4d6d5ba37 --- /dev/null +++ b/Server/src/test/kotlin/core/auth/AuthenticatorTests.kt @@ -0,0 +1,33 @@ +package core.auth + +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test +import rs09.auth.InMemoryAuthProvider +import rs09.auth.LoginResponse +import rs09.auth.UserAccountInfo +import rs09.storage.InMemoryStorageProvider + +class AuthenticatorTests { + private val authProvider = InMemoryAuthProvider() + private val storageProvider = InMemoryStorageProvider() + + init { + authProvider.configureFor(storageProvider) + } + + @Test fun shouldAllowCheckingIfAccountExists() { + val info = UserAccountInfo.createDefault() + info.username = "Billy" + Assertions.assertEquals(true, authProvider.canCreateAccountWith(info)) + authProvider.createAccountWith(info) + Assertions.assertEquals(false, authProvider.canCreateAccountWith(info)) + } + + @Test fun loginWithValidAccountInfoReturnsSuccess() { + Assertions.assertEquals(LoginResponse.Success, authProvider.checkLogin("Billy", "")) + } + + @Test fun loginWithInvalidAccountInfoReturnsFailure() { + Assertions.assertNotEquals(LoginResponse.Success, authProvider.checkLogin("Bilbo", "ebbeb")) + } +} \ No newline at end of file