Repository reorganisation
Unified kotlin and java into just src/main Unified the rs09 and core packages Took all content out of the core package, and placed it into the new content package Reorganised all source code relating to content to be easier to find and explore
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package core.auth
|
||||
|
||||
import core.ServerConstants
|
||||
import core.storage.AccountStorageProvider
|
||||
import core.storage.InMemoryStorageProvider
|
||||
import core.storage.SQLStorageProvider
|
||||
|
||||
object Auth {
|
||||
lateinit var authenticator: AuthProvider<*>
|
||||
lateinit var storageProvider: AccountStorageProvider
|
||||
|
||||
fun configure() {
|
||||
storageProvider = if (ServerConstants.PERSIST_ACCOUNTS)
|
||||
SQLStorageProvider()
|
||||
else
|
||||
InMemoryStorageProvider()
|
||||
|
||||
authenticator = if (ServerConstants.USE_AUTH)
|
||||
ProductionAuthenticator().also { it.configureFor(storageProvider) }
|
||||
else
|
||||
DevelopmentAuthenticator().also { it.configureFor(storageProvider) }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package core.auth
|
||||
|
||||
import core.game.node.entity.player.Player
|
||||
import core.storage.AccountStorageProvider
|
||||
|
||||
abstract class AuthProvider<T: AccountStorageProvider> {
|
||||
lateinit var storageProvider: T
|
||||
|
||||
abstract fun configureFor(provider: T)
|
||||
|
||||
fun canCreateAccountWith(info: UserAccountInfo) : Boolean {
|
||||
return !storageProvider.checkUsernameTaken(info.username)
|
||||
}
|
||||
|
||||
abstract fun createAccountWith(info: UserAccountInfo) : Boolean
|
||||
|
||||
abstract fun checkLogin(username: String, password: String) : Pair<AuthResponse, UserAccountInfo?>
|
||||
|
||||
abstract fun checkPassword(player: Player, password: String) : Boolean
|
||||
|
||||
abstract fun updatePassword(username: String, newPassword: String)
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package core.auth
|
||||
|
||||
enum class AuthResponse {
|
||||
UnexpectedError,
|
||||
CouldNotAd,
|
||||
Success,
|
||||
InvalidCredentials,
|
||||
AccountDisabled,
|
||||
AlreadyOnline,
|
||||
Updated,
|
||||
FullWorld,
|
||||
LoginServerOffline,
|
||||
LoginLimitExceeded,
|
||||
BadSessionID,
|
||||
WeakPassword,
|
||||
MembersWorld,
|
||||
CouldNotLogin,
|
||||
Updating,
|
||||
TooManyIncorrectLogins,
|
||||
StandingInMembersArea,
|
||||
AccountLocked,
|
||||
ClosedBeta,
|
||||
InvalidLoginServer,
|
||||
MovingWorld,
|
||||
ErrorLoadingProfile,
|
||||
BannedUser
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package core.auth
|
||||
|
||||
import core.game.node.entity.player.Player
|
||||
import core.ServerConstants
|
||||
import core.storage.AccountStorageProvider
|
||||
|
||||
class DevelopmentAuthenticator : AuthProvider<AccountStorageProvider>() {
|
||||
override fun configureFor(provider: AccountStorageProvider) {
|
||||
storageProvider = provider
|
||||
}
|
||||
|
||||
override fun checkLogin(username: String, password: String): Pair<AuthResponse, UserAccountInfo?> {
|
||||
val info: UserAccountInfo
|
||||
if(!storageProvider.checkUsernameTaken(username.toLowerCase())) {
|
||||
info = UserAccountInfo.createDefault()
|
||||
info.username = username
|
||||
createAccountWith(info)
|
||||
} else {
|
||||
info = storageProvider.getAccountInfo(username.toLowerCase())
|
||||
}
|
||||
return Pair(AuthResponse.Success, storageProvider.getAccountInfo(username))
|
||||
}
|
||||
|
||||
override fun createAccountWith(info: UserAccountInfo): Boolean {
|
||||
info.username = info.username.toLowerCase()
|
||||
if (ServerConstants.NOAUTH_DEFAULT_ADMIN)
|
||||
info.rights = 2
|
||||
storageProvider.store(info)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun checkPassword(player: Player, password: String): Boolean {
|
||||
return password == player.details.password
|
||||
}
|
||||
|
||||
override fun updatePassword(username: String, newPassword: String) {
|
||||
val info = storageProvider.getAccountInfo(username)
|
||||
info.password = newPassword
|
||||
storageProvider.update(info)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package core.auth
|
||||
|
||||
import core.game.node.entity.player.Player
|
||||
import core.game.system.SystemManager
|
||||
import core.ServerConstants
|
||||
import core.storage.AccountStorageProvider
|
||||
import core.storage.SQLStorageProvider
|
||||
import java.sql.SQLDataException
|
||||
import java.sql.Timestamp
|
||||
|
||||
class ProductionAuthenticator : AuthProvider<AccountStorageProvider>() {
|
||||
override fun configureFor(provider: AccountStorageProvider) {
|
||||
storageProvider = provider
|
||||
if (provider is SQLStorageProvider) {
|
||||
provider.configure(ServerConstants.DATABASE_ADDRESS!!, ServerConstants.DATABASE_NAME!!, ServerConstants.DATABASE_USER!!, ServerConstants.DATABASE_PASS!!)
|
||||
}
|
||||
}
|
||||
|
||||
override fun createAccountWith(info: UserAccountInfo): Boolean {
|
||||
try {
|
||||
info.password = SystemManager.getEncryption().hashPassword(info.password)
|
||||
info.joinDate = Timestamp(System.currentTimeMillis())
|
||||
storageProvider.store(info)
|
||||
} catch (e: SQLDataException) {
|
||||
return false
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
override fun checkLogin(username: String, password: String): Pair<AuthResponse, UserAccountInfo?> {
|
||||
val info: UserAccountInfo
|
||||
try {
|
||||
if (!storageProvider.checkUsernameTaken(username.toLowerCase())) {
|
||||
return Pair(AuthResponse.InvalidCredentials, null)
|
||||
}
|
||||
info = storageProvider.getAccountInfo(username.toLowerCase())
|
||||
val passCorrect = SystemManager.getEncryption().checkPassword(password, info.password)
|
||||
if(!passCorrect || info.password.isEmpty())
|
||||
return Pair(AuthResponse.InvalidCredentials, null)
|
||||
if(info.banEndTime > System.currentTimeMillis())
|
||||
return Pair(AuthResponse.AccountDisabled, null)
|
||||
if(info.online)
|
||||
return Pair(AuthResponse.AlreadyOnline, null)
|
||||
} catch (e: Exception) {
|
||||
e.printStackTrace()
|
||||
return Pair(AuthResponse.CouldNotLogin, null)
|
||||
}
|
||||
return Pair(AuthResponse.Success, info)
|
||||
}
|
||||
|
||||
override fun checkPassword(player: Player, password: String): Boolean {
|
||||
return SystemManager.getEncryption().checkPassword(password, player.details.password)
|
||||
}
|
||||
|
||||
override fun updatePassword(username: String, newPassword: String) {
|
||||
val info = storageProvider.getAccountInfo(username)
|
||||
info.password = SystemManager.getEncryption().hashPassword(newPassword)
|
||||
storageProvider.update(info)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package core.auth
|
||||
|
||||
import java.sql.Timestamp
|
||||
|
||||
class UserAccountInfo(
|
||||
var username: String,
|
||||
var password: String,
|
||||
var uid: Int,
|
||||
var rights: Int,
|
||||
var credits: Int,
|
||||
var ip: String,
|
||||
var lastUsedIp: String,
|
||||
var muteEndTime: Long,
|
||||
var banEndTime: Long,
|
||||
var contacts: String,
|
||||
var blocked: String,
|
||||
var clanName: String,
|
||||
var currentClan: String,
|
||||
var clanReqs: String,
|
||||
var timePlayed: Long,
|
||||
var lastLogin: Long,
|
||||
var online: Boolean,
|
||||
var joinDate: Timestamp
|
||||
) {
|
||||
companion object {
|
||||
val default = createDefault()
|
||||
@JvmStatic fun createDefault() : UserAccountInfo {
|
||||
return UserAccountInfo("", "", 0, 0, 0, "", "", 0L, 0L, "", "", "", "", "1,0,8,9", 0L, 0L, false, joinDate = Timestamp(System.currentTimeMillis())).also { it.setInitialReferenceValues() }
|
||||
}
|
||||
}
|
||||
|
||||
lateinit var initialValues: Array<Any>
|
||||
|
||||
fun setInitialReferenceValues() {
|
||||
initialValues = toArray()
|
||||
}
|
||||
|
||||
fun getChangedFields(): Pair<ArrayList<Int>, Array<Any>> {
|
||||
val current = toArray()
|
||||
val changed = ArrayList<Int>()
|
||||
|
||||
for(i in current.indices) {
|
||||
if (current[i] != initialValues[i]) changed.add(i)
|
||||
}
|
||||
|
||||
return Pair(changed, current)
|
||||
}
|
||||
|
||||
fun toArray(): Array<Any> {
|
||||
return arrayOf(username, password, uid, rights, credits, ip, lastUsedIp, muteEndTime, banEndTime, contacts, blocked, clanName, currentClan, clanReqs, timePlayed, lastLogin, online, joinDate)
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return "USER:$username,PASS:$password,UID:$uid,RIGHTS:$rights,CREDITS:$credits,IP:$ip,LASTIP:$lastUsedIp"
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
if (this === other) return true
|
||||
if (javaClass != other?.javaClass) return false
|
||||
|
||||
other as UserAccountInfo
|
||||
|
||||
if (username != other.username) return false
|
||||
if (password != other.password) return false
|
||||
if (uid != other.uid) return false
|
||||
if (rights != other.rights) return false
|
||||
if (credits != other.credits) return false
|
||||
if (ip != other.ip) return false
|
||||
if (lastUsedIp != other.lastUsedIp) return false
|
||||
if (muteEndTime != other.muteEndTime) return false
|
||||
if (banEndTime != other.banEndTime) return false
|
||||
if (contacts != other.contacts) return false
|
||||
if (blocked != other.blocked) return false
|
||||
if (clanName != other.clanName) return false
|
||||
if (currentClan != other.currentClan) return false
|
||||
if (clanReqs != other.clanReqs) return false
|
||||
if (timePlayed != other.timePlayed) return false
|
||||
if (lastLogin != other.lastLogin) return false
|
||||
if (online != other.online) return false
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
override fun hashCode(): Int {
|
||||
var result = username.hashCode()
|
||||
result = 31 * result + password.hashCode()
|
||||
result = 31 * result + uid
|
||||
result = 31 * result + rights
|
||||
result = 31 * result + credits
|
||||
result = 31 * result + ip.hashCode()
|
||||
result = 31 * result + lastUsedIp.hashCode()
|
||||
result = 31 * result + muteEndTime.hashCode()
|
||||
result = 31 * result + banEndTime.hashCode()
|
||||
result = 31 * result + contacts.hashCode()
|
||||
result = 31 * result + blocked.hashCode()
|
||||
result = 31 * result + clanName.hashCode()
|
||||
result = 31 * result + currentClan.hashCode()
|
||||
result = 31 * result + clanReqs.hashCode()
|
||||
result = 31 * result + timePlayed.hashCode()
|
||||
result = 31 * result + lastLogin.hashCode()
|
||||
result = 31 * result + online.hashCode()
|
||||
return result
|
||||
}
|
||||
|
||||
fun isDefault() : Boolean {
|
||||
return this == default
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user