Skip to content

Commit 5777845

Browse files
committed
Simplified session storage
1 parent a485e24 commit 5777845

File tree

4 files changed

+44
-33
lines changed

4 files changed

+44
-33
lines changed

common/src/main/kotlin/com/lambda/module/modules/client/Discord.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.lambda.module.modules.client
1919

2020
import com.lambda.Lambda
21+
import com.lambda.Lambda.LOG
2122
import com.lambda.context.SafeContext
2223
import com.lambda.event.EventFlow
2324
import com.lambda.event.events.WorldEvent
@@ -80,8 +81,8 @@ object Discord : Module(
8081
val auth = rpc.applicationManager.authenticate()
8182

8283
linkDiscord(discordToken = auth.accessToken)
83-
.fold(onSuccess = { updateToken(it); discordAuth = auth },
84-
onFailure = { warn("Failed to link the discord account to the minecraft auth") })
84+
.onSuccess { updateToken(it); discordAuth = auth }
85+
.onFailure { LOG.error(it); warn("Failed to link your discord account") }
8586
}
8687

8788
private fun stop() {

common/src/main/kotlin/com/lambda/module/modules/client/Network.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,8 @@ object Network : Module(
8080
// a race condition where the game server haven't acknowledged the packets
8181
// and posted to the sessionserver api
8282
login(mc.session.username, hash ?: return@listenUnsafeConcurrently)
83-
.fold(
84-
onSuccess = { updateToken(it) },
85-
onFailure = { LOG.warn("Unable to authenticate: $it") }
86-
)
83+
.onSuccess { updateToken(it) }
84+
.onFailure { LOG.warn(it) }
8785
}
8886
}
8987

common/src/main/kotlin/com/lambda/network/NetworkManager.kt

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,54 +17,49 @@
1717

1818
package com.lambda.network
1919

20-
import com.lambda.Lambda.gson
2120
import com.lambda.Lambda.mc
2221
import com.lambda.config.Configurable
2322
import com.lambda.config.configurations.UserConfig
2423
import com.lambda.core.Loadable
2524
import com.lambda.network.api.v1.models.Authentication
2625
import com.lambda.network.api.v1.models.Authentication.Data
27-
import com.lambda.util.FolderRegister.capes
28-
import com.lambda.util.reflections.getResources
29-
import java.io.File
26+
import com.lambda.util.StringUtils.base64UrlDecode
27+
import com.lambda.util.StringUtils.json
28+
import com.lambda.util.collections.updatableLazy
3029
import java.util.*
3130

3231
object NetworkManager : Configurable(UserConfig), Loadable {
3332
override val name = "network"
3433

35-
var accessToken by setting("authentication", ""); private set
34+
var accessToken by setting("access_token", ""); private set
3635

37-
val isDiscordLinked: Boolean
38-
get() = deserialized?.data?.discordId != null
36+
val isValid: Boolean
37+
get() = mc.gameProfile.name == auth.value?.data?.name &&
38+
mc.gameProfile.id == auth.value?.data?.uuid &&
39+
System.currentTimeMillis() > (auth.value?.expirationDate ?: Long.MAX_VALUE)
3940

40-
/**
41-
* Returns whether the auth has expired
42-
*/
43-
val isExpired: Boolean
44-
get() = (deserialized?.expirationDate ?: 0) < System.currentTimeMillis()
41+
private val auth = updatableLazy {
42+
val parts = accessToken.split(".")
43+
if (parts.size != 3) return@updatableLazy null
4544

46-
/**
47-
* Returns whether the auth token is invalid or not
48-
*/
49-
val isValid: Boolean
50-
get() = mc.gameProfile.name == deserialized?.data?.name &&
51-
mc.gameProfile.id == deserialized?.data?.uuid &&
52-
!isExpired
45+
val payload = parts[1]
46+
val data = payload.base64UrlDecode().json<Data>()
5347

54-
private var deserialized: Data? = null
48+
return@updatableLazy if (System.currentTimeMillis() < data.expirationDate) null
49+
else data
50+
}
5551

5652
fun updateToken(resp: Authentication) {
5753
accessToken = resp.accessToken
58-
decodeAuth(accessToken)
54+
auth.update()
5955
}
6056

61-
private fun decodeAuth(token: String) {
62-
val payload = token.split(".").getOrNull(1) ?: return
63-
deserialized = gson.fromJson(String(Base64.getUrlDecoder().decode(payload)), Data::class.java)
64-
}
6557

58+
override fun load(): String {
59+
auth.update()
6660

67-
init {
68-
decodeAuth(accessToken)
61+
return auth.value
62+
?.let { "Logged you in as ${it.data.name} (${it.data.uuid})" }
63+
?: "You are not authenticated"
6964
}
7065
}

common/src/main/kotlin/com/lambda/util/StringUtils.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@
1717

1818
package com.lambda.util
1919

20+
import com.google.gson.Gson
21+
import com.lambda.Lambda.gson
2022
import java.security.MessageDigest
23+
import kotlin.io.encoding.Base64
24+
import kotlin.io.encoding.ExperimentalEncodingApi
2125

2226
object StringUtils {
2327
/**
@@ -92,6 +96,19 @@ object StringUtils {
9296
return cost[len0 - 1]
9397
}
9498

99+
/**
100+
* Takes the receiver string and decodes it to the input type
101+
*
102+
* @return Instance of [T]
103+
*/
104+
inline fun <reified T : Any> String.json() = gson.fromJson(this, T::class.java)
105+
106+
/**
107+
* @see kotlin.io.encoding.Base64.decode
108+
*/
109+
@OptIn(ExperimentalEncodingApi::class)
110+
fun String.base64UrlDecode() = Base64.UrlSafe.decode(this).decodeToString()
111+
95112
/**
96113
* See [MessageDigest section](https://docs.oracle.com/en/java/javase/11/docs/specs/security/standard-names.html#messagedigest-algorithms) of the Java Security Standard Algorithm Names Specification
97114
*

0 commit comments

Comments
 (0)