update deps, increase jwt expiration date

This commit is contained in:
creeper123123321 2022-05-15 09:16:01 -03:00
parent 11ff7db38d
commit 9655f8c0ff
4 changed files with 61 additions and 31 deletions

View File

@ -57,15 +57,15 @@ dependencies {
implementation(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))
val vvVer = "4.3.0-22w18a-SNAPSHOT"
val vbVer = "4.3.0-22w18a-SNAPSHOT"
val vvVer = "4.3.0-22w19a-SNAPSHOT"
val vbVer = "4.3.0-22w19a-SNAPSHOT"
val vrVer = "d189537"
implementation("com.viaversion:viaversion:$vvVer") { isTransitive = false }
implementation("com.viaversion:viabackwards:$vbVer") { isTransitive = false }
implementation("com.github.ViaVersion.ViaRewind:viarewind-all:$vrVer") { isTransitive = false }
implementation("io.netty:netty-all:4.1.76.Final")
implementation("io.netty:netty-tcnative-boringssl-static:2.0.51.Final")
implementation("io.netty:netty-all:4.1.77.Final")
implementation("io.netty:netty-tcnative-boringssl-static:2.0.52.Final")
implementation("io.netty.incubator:netty-incubator-transport-native-io_uring:0.0.13.Final:linux-x86_64")
implementation("com.google.guava:guava:31.1-jre")
@ -101,7 +101,7 @@ dependencies {
implementation("io.ktor:ktor-client-logging-jvm:$ktorVersion")
testImplementation("io.ktor:ktor-server-test-host-jvm:$ktorVersion")
implementation("com.auth0:java-jwt:3.19.1")
implementation("com.auth0:java-jwt:3.19.2")
}
val run: JavaExec by tasks

View File

@ -93,7 +93,7 @@ class WebLogin : WebState {
if (check["valid"].asBoolean) {
val mcIdUser = check["username"].asString
val uuid = check["uuid"]?.asString?.let { parseUndashedId(it.replace("-", "")) }
?: webClient.server.usernameIdCache[mcIdUser].await()
?: webClient.server.usernameToIdCache[mcIdUser].await()
?: throw StacklessException("Failed to get UUID from minecraft.id")
val token = webClient.server.generateToken(uuid, mcIdUser)

View File

@ -43,7 +43,7 @@ class WebServer {
return JWT.create()
.withIssuedAt(Date())
.withNotBefore(Date())
.withExpiresAt(Date.from(Instant.now().plus(Duration.ofDays(30))))
.withExpiresAt(Date.from(Instant.now().plus(Duration.ofDays(180))))
.withSubject(account.toString())
.withClaim("name", username)
.withAudience("viaaas_listen")
@ -51,7 +51,17 @@ class WebServer {
.sign(jwtAlgorithm)
}
data class UserInfo(val id: UUID, val name: String?)
// todo implement token renewal
suspend fun renewToken(token: String): String? {
val info = parseToken(token) ?: return null
var name = info.name ?: "(unknown)"
if (info.id.version() == 4) { // random
name = idToProfileCache[info.id].await()?.get("name")?.asString ?: name
}
return generateToken(info.id, name)
}
data class UserInfo(val id: UUID, val name: String?, val expiration: Date)
fun parseToken(token: String): UserInfo? {
return try {
@ -59,7 +69,11 @@ class WebServer {
.withAnyOfAudience("viaaas_listen")
.build()
.verify(token)
UserInfo(UUID.fromString(verified.subject), verified.getClaim("name").asString())
UserInfo(
UUID.fromString(verified.subject),
verified.getClaim("name").asString(),
verified.expiresAt
)
} catch (e: JWTVerificationException) {
null
}
@ -72,7 +86,7 @@ class WebServer {
.hashSetValues()
.build<UUID, WebClient>()
)
val usernameIdCache = CacheBuilder.newBuilder()
val usernameToIdCache = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.build<String, CompletableFuture<UUID?>>(CacheLoader.from { name ->
CoroutineScope(Dispatchers.IO).async {
@ -82,6 +96,16 @@ class WebServer {
}.asCompletableFuture()
})
val idToProfileCache = CacheBuilder.newBuilder()
.expireAfterWrite(1, TimeUnit.HOURS)
.build<UUID, CompletableFuture<JsonObject?>>(CacheLoader.from { id ->
CoroutineScope(Dispatchers.IO).async {
AspirinServer.httpClient
.get("https://sessionserver.mojang.com/session/minecraft/profile/$id")
.body<JsonObject?>()
}.asCompletableFuture()
})
val sessionHashCallbacks = CacheBuilder.newBuilder()
.expireAfterWrite(30, TimeUnit.SECONDS)
.build<String, CompletableFuture<Unit>>(CacheLoader.from { _ -> CompletableFuture() })
@ -99,7 +123,7 @@ class WebServer {
suspend fun requestAddressInfo(frontName: String): CompletableFuture<AddressInfo> {
var onlineId: UUID? = null
try {
onlineId = usernameIdCache[frontName].await()
onlineId = usernameToIdCache[frontName].await()
} catch (e: java.lang.Exception) {
webLogger.debug("Couldn't get online uuid for $frontName", e)
}

View File

@ -716,26 +716,32 @@ function handleJoinRequest(parsed) {
function onSocketMsg(event) {
let parsed = JSON.parse(event.data);
if (parsed.action === "ad_login_methods") {
listenVisible = true;
renderActions();
} else if (parsed.action === "login_result") {
if (!parsed.success) {
addToast("Couldn't verify Minecraft account", "VIAaaS returned failed response");
} else {
listen(parsed.token);
saveToken(parsed.token);
}
} else if (parsed.action === "listen_login_requests_result") {
if (parsed.success) {
addListeningList(parsed.user, parsed.username, parsed.token);
} else {
removeToken(parsed.token);
}
} else if (parsed.action === "session_hash_request") {
handleJoinRequest(parsed);
} else if (parsed.action === "parameters_request") {
handleParametersRequest(parsed);
switch (parsed.action) {
case "ad_login_methods":
listenVisible = true;
renderActions();
break;
case "login_result":
if (!parsed.success) {
addToast("Couldn't verify Minecraft account", "VIAaaS returned failed response");
} else {
listen(parsed.token);
saveToken(parsed.token);
}
break;
case "listen_login_requests_result":
if (parsed.success) {
addListeningList(parsed.user, parsed.username, parsed.token);
} else {
removeToken(parsed.token);
}
break;
case "session_hash_request":
handleJoinRequest(parsed);
break;
case "parameters_request":
handleParametersRequest(parsed);
break;
}
}