-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use token generated at first launch for authentication
- Loading branch information
1 parent
1dac317
commit 1df0678
Showing
18 changed files
with
236 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package dev.schlaubi.gtakiller.common | ||
|
||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class UserUpdateRequest(val name: String) | ||
|
||
@Serializable | ||
data class UserCreateRequest(val name: String) | ||
|
||
@Serializable | ||
data class JWTUser(val id: String, val name: String, val token: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package dev.schlaubi.gtakiller | ||
|
||
import com.auth0.jwt.JWT | ||
import com.auth0.jwt.algorithms.Algorithm | ||
import dev.schlaubi.gtakiller.common.JWTUser | ||
import dev.schlaubi.gtakiller.common.Route.SignUp | ||
import dev.schlaubi.gtakiller.common.UserCreateRequest | ||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.auth.jwt.* | ||
import io.ktor.server.request.* | ||
import io.ktor.server.resources.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.Route | ||
import io.ktor.util.* | ||
|
||
private val algorithm = Algorithm.HMAC256("secret") | ||
|
||
private val verifier = JWT.require(algorithm).build() | ||
|
||
data class JWTUserData(val id: String, val name: String) | ||
|
||
fun Application.installAuth() { | ||
install(Authentication) { | ||
jwt { | ||
verifier { verifier } | ||
validate { JWTUserData(it.payload.subject!!, it.getClaim("user", String::class)!!) } | ||
} | ||
} | ||
} | ||
|
||
val ApplicationCall.user: JWTUserData | ||
get() { | ||
val token = principal<JWTUserData>()!! | ||
|
||
return token | ||
} | ||
|
||
fun createToken(name: String, id: String = generateNonce()): JWTUser { | ||
val token = JWT.create().withSubject(id).withClaim("user", name).sign(algorithm) | ||
|
||
return JWTUser(id, name, token) | ||
} | ||
|
||
fun Route.signUpRoute() { | ||
post<SignUp> { | ||
val (name) = call.receive<UserCreateRequest>() | ||
call.respond(createToken(name)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package dev.schlaubi.gtakiller | ||
|
||
import dev.schlaubi.gtakiller.common.Event | ||
import dev.schlaubi.gtakiller.common.Route.Events | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import io.ktor.server.resources.* | ||
import io.ktor.server.routing.* | ||
import io.ktor.server.websocket.* | ||
|
||
data class AuthenticatedServerWebSocketSession(val id: String, val session: DefaultWebSocketServerSession) : | ||
DefaultWebSocketServerSession by session | ||
|
||
private val sessions = mutableListOf<AuthenticatedServerWebSocketSession>() | ||
|
||
private val LOG = KotlinLogging.logger { } | ||
|
||
fun Route.eventHandler() = resource<Events> { | ||
webSocket { | ||
val (userId) = call.user | ||
val session = AuthenticatedServerWebSocketSession(userId, this) | ||
sessions += session | ||
|
||
LOG.info { "User $userId opened WebSocket connection" } | ||
|
||
for (frame in incoming) { | ||
LOG.trace { "Received frame: $frame" } | ||
} | ||
|
||
LOG.info { "User $userId closed WebSocket connection" } | ||
sessions -= session | ||
} | ||
} | ||
|
||
suspend fun broadcastEvent(event: Event, predicate: (AuthenticatedServerWebSocketSession) -> Boolean = { true }) = | ||
sessions.forEach { | ||
if (predicate(it)) try { | ||
LOG.trace { "Sending event $event to $it" } | ||
it.sendSerialized(event) | ||
} catch (e: Exception) { | ||
LOG.warn(e) { "Error while sending event to $it" } | ||
sessions -= it | ||
} | ||
} | ||
|
||
suspend fun broadcastEventExceptForUser(event: Event, userId: String) = broadcastEvent(event) { it.id != userId } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package dev.schlaubi.gtakiller | ||
|
||
import dev.schlaubi.gtakiller.common.KillGtaEvent | ||
import dev.schlaubi.gtakiller.common.Route.Kill | ||
import dev.schlaubi.gtakiller.common.UpdateKillCounterEvent | ||
import io.ktor.http.* | ||
import io.ktor.server.response.* | ||
import io.ktor.server.routing.Route | ||
import io.ktor.server.resources.post | ||
import kotlinx.datetime.Clock | ||
import kotlinx.datetime.Instant | ||
import kotlin.time.Duration.Companion.seconds | ||
import dev.schlaubi.gtakiller.common.Kill as KillObject | ||
|
||
private val lastKill = Instant.DISTANT_PAST | ||
|
||
fun Route.killGTARoute() { | ||
post<Kill> { | ||
val (userId, name) = call.user | ||
|
||
broadcastEventExceptForUser(KillGtaEvent, userId) | ||
|
||
val killTime = Clock.System.now() | ||
val timeSinceLastKill = killTime - lastKill | ||
if (timeSinceLastKill > 10.seconds) { | ||
val kill = KillObject(killTime, name, userId) | ||
|
||
writeStats(stats.copy(kills = stats.kills + kill)) | ||
|
||
broadcastEvent(UpdateKillCounterEvent(stats.kills.size, kill)) | ||
} | ||
|
||
call.respond(HttpStatusCode.Accepted) | ||
} | ||
} |
Oops, something went wrong.