Skip to content

Commit

Permalink
implement auth
Browse files Browse the repository at this point in the history
  • Loading branch information
wagyourtail committed Mar 25, 2024
1 parent 7ef688e commit 4d11c23
Show file tree
Hide file tree
Showing 11 changed files with 305 additions and 11 deletions.
3 changes: 3 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,9 @@ dependencies {

implementation("org.eclipse.jgit:org.eclipse.jgit:5.13.2.202306221912-r")

implementation("com.github.javakeyring:java-keyring:1.0.3")
implementation("net.raphimc:MinecraftAuth:4.0.0")

compileOnly("org.jetbrains.kotlinx:kotlinx-metadata-jvm:0.4.2") {
isTransitive = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import org.gradle.api.tasks.SourceSet
import org.gradle.api.tasks.TaskContainer
import org.gradle.jvm.toolchain.JavaLanguageVersion
import org.gradle.jvm.toolchain.JavaToolchainService
import xyz.wagyourtail.unimined.api.runs.auth.AuthConfig
import xyz.wagyourtail.unimined.util.XMLBuilder
import xyz.wagyourtail.unimined.util.withSourceSet
import java.io.File
Expand All @@ -31,7 +32,7 @@ data class RunConfig(
var workingDir: File,
val env: MutableMap<String, String>,
val runFirst: MutableList<Task> = mutableListOf(),
var disabled : Boolean = false,
var disabled : Boolean = false
) {

fun createIdeaRunConfig() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package xyz.wagyourtail.unimined.api.runs
import groovy.lang.Closure
import groovy.lang.DelegatesTo
import org.jetbrains.annotations.ApiStatus
import xyz.wagyourtail.unimined.api.runs.auth.AuthConfig
import xyz.wagyourtail.unimined.util.FinalizeOnRead

abstract class RunsConfig {
/**
* just a flag to disable all.
*/
var off: Boolean by FinalizeOnRead(false)
abstract val auth: AuthConfig

fun config(
config: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package xyz.wagyourtail.unimined.api.runs.auth

import org.jetbrains.annotations.ApiStatus
import java.util.*

/**
* Configuration for authentication.
* it is recommended to use the gradle properties (possibly globally) instead of setting these values in the build.gradle
*
* @since 1.2.0
*/
interface AuthConfig {

/**
* If enabled, unimined will use https://github.com/RaphiMC/MinecraftAuth to authenticate client runs with Mojang.
* value can also be set by the `unimined.auth.enabled` gradle property. but this overrides the value set there.
*/
var enabled: Boolean

/**
* If enabled, unimined will store the credentials to global gradle cache/unimined/auth.json
* this value can also be set by the `unimined.auth.storeCredentials` gradle property.
* if enabled, it will default to the first account in the store, or the `unimined.auth.username` gradle property.
* if the username specified by the property is not found, it will prompt for login.
*
* to make not enabling this less annoying, unimined will always store the credentials in-memory for the gradle daemon.
*/
var storeCredentials: Boolean

/**
* when enabled, unimined will store a key for decrypting the credential file to the os's keychain using java-keyring.
* if disabled, the tokens will be stored in plain text!!!
* this value can also be set by the `unimined.auth.encryptStoredCredentials` gradle property.
*
* do note, that due to OS limitations, the keyring may be fully accessible to other java applications,
* or even other applications entirely. see: https://github.com/javakeyring/java-keyring?tab=readme-ov-file#security-concerns
*/
var encryptStoredCredentials: Boolean

@get:ApiStatus.Internal
@set:ApiStatus.Experimental
var authInfo: AuthInfo?

data class AuthInfo(
val username: String,
val uuid: UUID,
val accessToken: String,
)

}
5 changes: 5 additions & 0 deletions src/api/kotlin/xyz/wagyourtail/unimined/util/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ object OSUtils {
"x86_64" -> "64"
else -> "unknown"
}

const val WINDOWS = "windows"
const val LINUX = "linux"
const val OSX = "osx"
const val UNKNOWN = "unknown"
}

fun testSha1(size: Long, sha1: String, path: Path): Boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ object InterfaceInjectionMinecraftTransformer {
reader.accept(node, 0)

if (node.interfaces == null) {
node.interfaces = arrayListOf();
node.interfaces = arrayListOf()
}

for (injected in injections[target]!!) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,8 @@ class MinecraftProvider(project: Project, sourceSet: SourceSet) : MinecraftConfi
minecraftData.metadata.getGameArgs(
"Dev",
workingDirectory.toPath(),
assetsDir
assetsDir,
runs.auth.authInfo
),
(minecraftData.metadata.getJVMArgs(
workingDirectory.resolve("libraries").toPath(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.google.gson.JsonArray
import com.google.gson.JsonElement
import com.google.gson.JsonObject
import org.gradle.api.JavaVersion
import xyz.wagyourtail.unimined.api.runs.auth.AuthConfig
import xyz.wagyourtail.unimined.util.OSUtils
import xyz.wagyourtail.unimined.util.consumerApply
import java.net.MalformedURLException
Expand Down Expand Up @@ -145,6 +146,7 @@ data class VersionData(
username: String,
gameDir: Path,
assets: Path,
authInfo: AuthConfig.AuthInfo?
): MutableList<String> {
val args = getArgsRecursive()
return applyGameArgs(
Expand All @@ -154,7 +156,8 @@ data class VersionData(
assets,
assetIndex?.id ?: "",
id,
type!!
type!!,
authInfo
)
}
}
Expand All @@ -166,22 +169,24 @@ fun applyGameArgs(
assets: Path,
assetIndex: String,
id: String,
type: String
type: String,
authInfo: AuthConfig.AuthInfo?
): MutableList<String> {
return args.mapNotNull { e: String ->
if (e == "--uuid" || e == "\${auth_uuid}") null
else e.replace("\${auth_player_name}", username)
return args.asSequence().mapNotNull { e: String ->
if (authInfo == null && (e == "--uuid" || e == "\${auth_uuid}")) null
else e.replace("\${auth_player_name}", authInfo?.username ?: username)
.replace("\${version_name}", id)
.replace("\${game_directory}", gameDir.toAbsolutePath().toString())
.replace("\${assets_root}", assets.toString())
.replace("\${game_assets}", gameDir.resolve("resources").toString())
.replace("\${assets_index_name}", assetIndex)
.replace("\${assets_index}", assetIndex)
.replace("\${auth_access_token}", "0")
.replace("\${clientid}", "0")
.replace("\${auth_access_token}", authInfo?.accessToken ?: "0")
.replace("\${clientid}", "unimined")
.replace("\${user_type}", "msa")
.replace("\${version_type}", type)
.replace("\${user_properties}", "{}")
.replace("\${auth_uuid}", authInfo?.uuid.toString())
}.toMutableList()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import xyz.wagyourtail.unimined.api.minecraft.MinecraftConfig
import xyz.wagyourtail.unimined.api.runs.RunConfig
import xyz.wagyourtail.unimined.api.runs.RunsConfig
import xyz.wagyourtail.unimined.api.unimined
import xyz.wagyourtail.unimined.internal.runs.auth.AuthProvider
import xyz.wagyourtail.unimined.util.defaultedMapOf
import xyz.wagyourtail.unimined.util.sourceSets
import xyz.wagyourtail.unimined.util.withSourceSet

class RunsProvider(val project: Project, val minecraft: MinecraftConfig) : RunsConfig() {
private var freeze = false

override val auth = AuthProvider(this)
private val runConfigs = mutableMapOf<String, RunConfig>()
private val transformers = defaultedMapOf<String, RunConfig.() -> Unit> { {} }

Expand Down
Loading

0 comments on commit 4d11c23

Please sign in to comment.