feat(login): log in with a saved token instead of storing the password (#120)
This commit is contained in:
1 parent
457f21c17f
commit
a1453b8db2
19 files changed
+706
-45
No files matched your search
@@ -35,6 +35,7 @@ dependencies {
|
||||
tasks.named<JavaExec>("run") {
|
||||
listOf(
|
||||
"OPENMMO_SESSION_SECRET",
|
||||
"OPENMMO_SESSION_TOKEN_MAX_AGE",
|
||||
"GAME_DB_HOST",
|
||||
"GAME_DB_PORT",
|
||||
"GAME_DB_NAME",
|
||||
|
||||
@@ -11,6 +11,10 @@ object ConfigLoader {
|
||||
val config = ConfigFactory.load()
|
||||
val secret = config.getString("server.sessionSecret")
|
||||
require(secret.isNotEmpty()) { "server.sessionSecret must not be empty" }
|
||||
val tokenMaxAge = config.getDuration("server.sessionTokenMaxAge")
|
||||
require(!tokenMaxAge.isNegative && !tokenMaxAge.isZero) {
|
||||
"server.sessionTokenMaxAge must be positive"
|
||||
}
|
||||
return GameServerConfig(
|
||||
host = config.getString("server.host"),
|
||||
port = config.getInt("server.port"),
|
||||
@@ -19,6 +23,7 @@ object ConfigLoader {
|
||||
rootKey = config.stringOrNull("server.rootKey"),
|
||||
rootKeyFile = config.stringOrNull("server.rootKeyFile"),
|
||||
sessionSecret = secret.toByteArray(Charsets.UTF_8),
|
||||
sessionTokenMaxAge = tokenMaxAge,
|
||||
db =
|
||||
DbConfig(
|
||||
host = config.getString("db.host"),
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package de.fiereu.openmmo.server.game.config
|
||||
|
||||
import java.time.Duration
|
||||
|
||||
data class GameServerConfig(
|
||||
val host: String,
|
||||
val port: Int,
|
||||
val checksumSize: Int,
|
||||
val rootKeyResource: String,
|
||||
val sessionSecret: ByteArray,
|
||||
val sessionTokenMaxAge: Duration = Duration.ofMinutes(5),
|
||||
val db: DbConfig = DbConfig(),
|
||||
val rootKey: String? = null,
|
||||
val rootKeyFile: String? = null,
|
||||
@@ -19,6 +22,7 @@ data class GameServerConfig(
|
||||
rootKey == other.rootKey &&
|
||||
rootKeyFile == other.rootKeyFile &&
|
||||
sessionSecret.contentEquals(other.sessionSecret) &&
|
||||
sessionTokenMaxAge == other.sessionTokenMaxAge &&
|
||||
db == other.db
|
||||
|
||||
override fun hashCode(): Int {
|
||||
@@ -29,6 +33,7 @@ data class GameServerConfig(
|
||||
h = h * 31 + rootKey.hashCode()
|
||||
h = h * 31 + rootKeyFile.hashCode()
|
||||
h = h * 31 + sessionSecret.contentHashCode()
|
||||
h = h * 31 + sessionTokenMaxAge.hashCode()
|
||||
h = h * 31 + db.hashCode()
|
||||
return h
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ object GameServerModule {
|
||||
@Provides
|
||||
@Singleton
|
||||
fun tokenVerifier(config: GameServerConfig): SessionTokenVerifier =
|
||||
SessionTokenVerifier(config.sessionSecret)
|
||||
SessionTokenVerifier(config.sessionSecret, config.sessionTokenMaxAge)
|
||||
|
||||
@Provides
|
||||
@Singleton
|
||||
|
||||
+41
-14
@@ -3,6 +3,7 @@ package de.fiereu.openmmo.server.game.services
|
||||
import de.fiereu.network.PacketEvent
|
||||
import de.fiereu.network.SessionContext
|
||||
import de.fiereu.openmmo.common.CharacterInfo
|
||||
import de.fiereu.openmmo.common.auth.SessionTokenVerifier
|
||||
import de.fiereu.openmmo.common.enums.CharacterGender
|
||||
import de.fiereu.openmmo.common.enums.ChatType
|
||||
import de.fiereu.openmmo.common.enums.Language
|
||||
@@ -75,29 +76,51 @@ constructor(
|
||||
private val characterStore: CharacterStore,
|
||||
private val presenceService: PresenceService,
|
||||
private val mapScriptService: MapScriptService,
|
||||
private val tokenVerifier: SessionTokenVerifier,
|
||||
) {
|
||||
|
||||
fun onJoinGame(event: PacketEvent<JoinPacket>) {
|
||||
val ctx = event.session
|
||||
val join = event.packet
|
||||
log.info { "Player joined the game." }
|
||||
val authData = event.packet.authData
|
||||
|
||||
val authData = join.authData
|
||||
var userId = 0
|
||||
if (authData is NewAuthData) {
|
||||
userId = authData.userId
|
||||
log.info { "User $userId joined with session key (${authData.sessionKey.size} bytes)" }
|
||||
if (authData !is NewAuthData) {
|
||||
log.warn { "Rejected join with unsupported auth data ${authData::class.simpleName}" }
|
||||
rejectJoin(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
if (userId > 0) {
|
||||
ctx.attributes[PLAYER_STATE] = PlayerState(userId = userId)
|
||||
sessionRegistry.register(ctx)
|
||||
log.info { "Session created for user $userId" }
|
||||
val token = tokenVerifier.verify(authData.sessionKey)
|
||||
if (token == null) {
|
||||
log.warn {
|
||||
"Rejected join for claimed userId=${authData.userId}, " +
|
||||
"session token invalid or expired (${authData.sessionKey.size} bytes)"
|
||||
}
|
||||
rejectJoin(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
// The claim in the packet is the client's, the token is the login server's. Compare before
|
||||
// narrowing, so a value that does not fit an Int cannot match by truncation.
|
||||
if (token.userId != authData.userId.toLong() || token.userId <= 0) {
|
||||
log.warn { "Join claimed userId=${authData.userId} but its token says ${token.userId}" }
|
||||
rejectJoin(ctx)
|
||||
return
|
||||
}
|
||||
val userId = token.userId.toInt()
|
||||
|
||||
ctx.attributes[PLAYER_STATE] = PlayerState(userId = userId)
|
||||
sessionRegistry.register(ctx)
|
||||
log.info { "Session created for user $userId" }
|
||||
|
||||
ctx.send(JoinResponsePacket.acceptNow(playtime = 1337, rewardPoints = 420, balance = 187))
|
||||
}
|
||||
|
||||
// Closing is what keeps a refused peer from going on to send packets the handlers would
|
||||
// otherwise answer without a PlayerState. Close only once the refusal itself has been written.
|
||||
private fun rejectJoin(ctx: SessionContext) {
|
||||
ctx.send(JoinResponsePacket.reject()).addListener { ctx.close { "join rejected" } }
|
||||
}
|
||||
|
||||
suspend fun onCreateCharacter(event: PacketEvent<CreateCharacterPacket>) {
|
||||
val ctx = event.session
|
||||
val state = ctx.attributes[PLAYER_STATE]
|
||||
@@ -173,14 +196,18 @@ constructor(
|
||||
suspend fun onCharacterSelected(event: PacketEvent<SelectCharacterPacket>) {
|
||||
val ctx = event.session
|
||||
val charId = event.packet.characterId
|
||||
val state = ctx.attributes[PLAYER_STATE]
|
||||
if (state == null) {
|
||||
log.warn { "No session for channel" }
|
||||
return
|
||||
}
|
||||
val stored = characterStore.getOrLoadCharacter(charId)
|
||||
if (stored == null) {
|
||||
log.warn { "Character $charId not found" }
|
||||
return
|
||||
}
|
||||
val state = ctx.attributes[PLAYER_STATE]
|
||||
if (state == null) {
|
||||
log.warn { "No session for channel" }
|
||||
if (stored.info.userId != state.userId) {
|
||||
log.warn { "User ${state.userId} tried to select character $charId owned by another account" }
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -7,6 +7,8 @@ server {
|
||||
rootKeyFile = ${?OPENMMO_GAME_PRIVATE_KEY_FILE}
|
||||
sessionSecret = "dev-only-secret-do-not-use-in-production"
|
||||
sessionSecret = ${?OPENMMO_SESSION_SECRET}
|
||||
sessionTokenMaxAge = 5m
|
||||
sessionTokenMaxAge = ${?OPENMMO_SESSION_TOKEN_MAX_AGE}
|
||||
}
|
||||
|
||||
db {
|
||||
|
||||
Reference in new issue
Block a user