forked from SteamWar/SteamWar
Simplify Tokens and add Discord OAuth
Signed-off-by: Chaoscaot <max@maxsp.de>
This commit is contained in:
@@ -29,10 +29,14 @@ import io.ktor.server.application.hooks.*
|
||||
import io.ktor.server.auth.*
|
||||
import io.ktor.server.request.*
|
||||
import io.ktor.server.response.*
|
||||
import io.ktor.server.sessions.sessions
|
||||
import io.ktor.util.*
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
@Serializable
|
||||
data class SWUserSession(val userId: Int)
|
||||
|
||||
data class SWAuthPrincipal(val token: Token, val user: SteamwarUser) : Principal
|
||||
data class SWAuthPrincipal(val user: SteamwarUser) : Principal
|
||||
|
||||
class SWAuthConfig {
|
||||
var permission: UserPerm? = null
|
||||
|
||||
@@ -19,6 +19,8 @@
|
||||
|
||||
package de.steamwar.plugins
|
||||
|
||||
import de.steamwar.config
|
||||
import de.steamwar.sql.SteamwarUser
|
||||
import de.steamwar.sql.Token
|
||||
import de.steamwar.util.TokenType
|
||||
import de.steamwar.util.isValid
|
||||
@@ -31,7 +33,13 @@ import io.ktor.server.auth.*
|
||||
import io.ktor.server.plugins.contentnegotiation.*
|
||||
import io.ktor.server.plugins.cors.routing.*
|
||||
import io.ktor.server.plugins.ratelimit.*
|
||||
import io.ktor.server.response.respond
|
||||
import io.ktor.server.sessions.SessionTransportTransformerEncrypt
|
||||
import io.ktor.server.sessions.Sessions
|
||||
import io.ktor.server.sessions.cookie
|
||||
import io.ktor.server.sessions.directorySessionStorage
|
||||
import kotlinx.serialization.json.Json
|
||||
import java.io.File
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
fun Application.configurePlugins() {
|
||||
@@ -46,6 +54,7 @@ fun Application.configurePlugins() {
|
||||
allowHeader(HttpHeaders.ContentType)
|
||||
anyHost()
|
||||
allowXHttpMethodOverride()
|
||||
allowCredentials = true
|
||||
}
|
||||
install(RateLimit) {
|
||||
global {
|
||||
@@ -54,7 +63,7 @@ fun Application.configurePlugins() {
|
||||
it.request.headers["X-Forwarded-For"] ?: it.request.local.remoteHost
|
||||
}
|
||||
requestWeight { applicationCall, _ ->
|
||||
if(!applicationCall.request.headers.contains("X-Forwarded-For")) {
|
||||
if (!applicationCall.request.headers.contains("X-Forwarded-For")) {
|
||||
0
|
||||
} else {
|
||||
1
|
||||
@@ -63,28 +72,50 @@ fun Application.configurePlugins() {
|
||||
}
|
||||
}
|
||||
authentication {
|
||||
bearer("sw-auth") {
|
||||
realm = "SteamWar API"
|
||||
authenticate { call ->
|
||||
val token = Token.getTokenByCode(call.token)
|
||||
if (token == null) {
|
||||
null
|
||||
} else {
|
||||
if (!token.isValid) {
|
||||
token.delete()
|
||||
return@authenticate null
|
||||
}
|
||||
if (token.type == TokenType.REFRESH_TOKEN) {
|
||||
token.delete()
|
||||
}
|
||||
// Disabled, Maybe for API later
|
||||
//bearer("sw-auth") {
|
||||
// realm = "SteamWar API"
|
||||
// authenticate { call ->
|
||||
// val token = Token.getTokenByCode(call.token)
|
||||
// if (token == null) {
|
||||
// null
|
||||
// } else {
|
||||
// if (!token.isValid) {
|
||||
// token.delete()
|
||||
// return@authenticate null
|
||||
// }
|
||||
// if (token.type == TokenType.REFRESH_TOKEN) {
|
||||
// token.delete()
|
||||
// }
|
||||
|
||||
SWAuthPrincipal(token, token.owner)
|
||||
}
|
||||
// SWAuthPrincipal(token.owner)
|
||||
// }
|
||||
// }
|
||||
//}
|
||||
session<SWUserSession>("sw-session") {
|
||||
validate { session ->
|
||||
val steamwarUser = session.userId.let { SteamwarUser.byId(it) }
|
||||
return@validate steamwarUser?.let { SWAuthPrincipal(it) }
|
||||
}
|
||||
challenge {
|
||||
call.respond(HttpStatusCode.Unauthorized)
|
||||
}
|
||||
}
|
||||
}
|
||||
install(Sessions) {
|
||||
cookie<SWUserSession>("sw-session", directorySessionStorage(File("sessions"))) {
|
||||
cookie.path = "/"
|
||||
cookie.maxAgeInSeconds = 60 * 60 * 24 * 7
|
||||
cookie.httpOnly = true
|
||||
cookie.secure = true
|
||||
transform(SessionTransportTransformerEncrypt(
|
||||
config.sessionEncryptSecret.toByteArray(),
|
||||
config.sessionSignSecret.toByteArray()
|
||||
))
|
||||
}
|
||||
}
|
||||
install(ContentNegotiation) {
|
||||
json(Json)
|
||||
}
|
||||
install(ErrorLogger)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user