|
17 | 17 |
|
18 | 18 | package com.lambda.network |
19 | 19 |
|
20 | | -import com.lambda.Lambda.gson |
21 | 20 | import com.lambda.Lambda.mc |
22 | 21 | import com.lambda.config.Configurable |
23 | 22 | import com.lambda.config.configurations.UserConfig |
24 | 23 | import com.lambda.core.Loadable |
25 | 24 | import com.lambda.network.api.v1.models.Authentication |
26 | 25 | 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 |
30 | 29 | import java.util.* |
31 | 30 |
|
32 | 31 | object NetworkManager : Configurable(UserConfig), Loadable { |
33 | 32 | override val name = "network" |
34 | 33 |
|
35 | | - var accessToken by setting("authentication", ""); private set |
| 34 | + var accessToken by setting("access_token", ""); private set |
36 | 35 |
|
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) |
39 | 40 |
|
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 |
45 | 44 |
|
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>() |
53 | 47 |
|
54 | | - private var deserialized: Data? = null |
| 48 | + return@updatableLazy if (System.currentTimeMillis() < data.expirationDate) null |
| 49 | + else data |
| 50 | + } |
55 | 51 |
|
56 | 52 | fun updateToken(resp: Authentication) { |
57 | 53 | accessToken = resp.accessToken |
58 | | - decodeAuth(accessToken) |
| 54 | + auth.update() |
59 | 55 | } |
60 | 56 |
|
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 | | - } |
65 | 57 |
|
| 58 | + override fun load(): String { |
| 59 | + auth.update() |
66 | 60 |
|
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" |
69 | 64 | } |
70 | 65 | } |
0 commit comments