Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
DJtheRedstoner committed Sep 3, 2021
0 parents commit 0538d6f
Show file tree
Hide file tree
Showing 35 changed files with 1,632 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
**/build
.gradle
**/run
.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 DJtheRedstoner

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
36 changes: 36 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import org.gradle.jvm.tasks.Jar

plugins {
`java-library`
}

allprojects {
group = "me.djtheredstoner"
version = "0.1"

repositories {
mavenCentral()
}

tasks.withType<JavaCompile> {
sourceCompatibility = "1.8"
targetCompatibility = "1.8"

options.encoding = "UTF-8"
}

tasks.withType<Jar> {
archiveBaseName.set("${rootProject.name}-${project.name}")

from(rootProject.projectDir.resolve("LICENSE")) {
rename { "LICENSE_DevAuth.txt" }
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
}

println("${project.name} ${archiveClassifier.get()}")
}
}

subprojects {
apply(plugin = "java-library")
}
16 changes: 16 additions & 0 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
repositories {
maven("https://libraries.minecraft.net")
}

dependencies {
// Not provided by minecraft
api("com.electronwill.night-config:toml:3.6.4")

// Provided by minecraft
implementation("com.mojang:authlib:2.3.31") // 1.17.1: 2.3.31 1.12.2: 1.5.25 1.8.9: 1.5.21
implementation("org.apache.logging.log4j:log4j-core:2.14.1") // 1.17.1: 2.14.1 1.12.2: 2.8.1 1.8.9: 2.0-beta9
implementation("com.google.code.gson:gson:2.8.0") // 1.17.1: 2.8.0 1.12.2: 2.8.0 1.8.9: 2.2.4
implementation("commons-io:commons-io:2.5") // 1.17.1: 2.5 1.12.2: 2.5 1.8.9: 2.4
implementation("commons-codec:commons-codec:1.10") // 1.17.1: 1.10 1.12.2: 1.10 1.8.9: 1.9
implementation("org.apache.httpcomponents:httpclient:4.3.3") // 1.17.1: 4.3.3 1.12.2: 4.3.3 1.8.9: 4.3.3
}
101 changes: 101 additions & 0 deletions common/src/main/java/me/djtheredstoner/devauth/common/DevAuth.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package me.djtheredstoner.devauth.common;

import me.djtheredstoner.devauth.common.auth.IAuthProvider;
import me.djtheredstoner.devauth.common.auth.SessionData;
import me.djtheredstoner.devauth.common.config.Account;
import me.djtheredstoner.devauth.common.config.DevAuthConfig;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.*;

public abstract class DevAuth {

private static final Set<String> authOptions =
new HashSet<>(Arrays.asList("--accessToken", "--uuid", "--username", "--userType", "--userProperties"));

private final boolean enabled;
private final Logger logger;
private final DevAuthConfig config = DevAuthConfig.load();

protected DevAuth() {
enabled = Properties.ENABLED.getBooleanValue();
logger = LogManager.getLogger("DevAuth");
}

protected abstract Environment getEnvironment();

public String[] processArguments(String[] args) {
if (!isEnabled()) {
logger.info("DevAuth disabled, not logging in!");
return args;
}

SessionData data = login();

List<String> newArgs = new ArrayList<>();

for (int i = 0; i < args.length; i++) {
String arg = args[i];
if (authOptions.contains(arg)) {
i++;
} else {
newArgs.add(arg);
}
}

newArgs.add("--accessToken=" + data.getAccessToken());
newArgs.add("--uuid=" + data.getUuid());
newArgs.add("--username=" + data.getUsername());
newArgs.add("--userType=" + data.getUserType());
newArgs.add("--userProperties=" + data.getUserProperties());

return newArgs.toArray(new String[0]);
}

public SessionData login() {
Account account = getSelectedAccount();

IAuthProvider authProvider = account.getType().getAuthProviderFactory().create(this);

SessionData session = authProvider.login(account);

logger.info("Successfully logged in as " + session.getUsername());
return session;
}

public Account getSelectedAccount() {
String commandLineAccount = Properties.ACCOUNT.getValue();
String defaultAccount = config.getDefaultAccount();

if (commandLineAccount != null) {
if (config.getAccounts().containsKey(commandLineAccount)) {
return config.getAccounts().get(commandLineAccount);
}
throw new RuntimeException("Account '" + commandLineAccount + "' not found, valid accounts are: " + getValidAccounts());
} else if (defaultAccount != null) {
if (config.getAccounts().containsKey(defaultAccount)) {
return config.getAccounts().get(defaultAccount);
}
throw new RuntimeException("Account '" + defaultAccount + "' not found, valid accounts are: " + getValidAccounts());
}
throw new RuntimeException("No account specified, specify one with the defaultAccount config option or the " + Properties.ACCOUNT.getFullKey() + " property");
}

public boolean isEnabled() {
return enabled;
}

public DevAuthConfig getConfig() {
return config;
}

private String getValidAccounts() {
StringJoiner joiner = new StringJoiner(", ");
for (Account account : config.getAccounts().values()) {
joiner.add(account.getName());
}
return joiner.toString();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package me.djtheredstoner.devauth.common;

public enum Environment {

FABRIC("fabric"),
FORGE("forge");

private final String name;

Environment(String name) {
this.name = name;
}

public String getName() {
return name;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package me.djtheredstoner.devauth.common;

public enum Properties {

ENABLED("enabled", "true"),
CONFIG_DIR("configDir", null),
ACCOUNT("account", null);

private final String key;
private final String defaultValue;

Properties(String key, String defaultValue) {
this.key = key;
this.defaultValue = defaultValue;
}

String getFullKey() {
return "devauth." + key;
}

public String getValue() {
return System.getProperty(getFullKey(), defaultValue);
}

public boolean getBooleanValue() {
return Boolean.parseBoolean(getValue());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package me.djtheredstoner.devauth.common.auth;

import me.djtheredstoner.devauth.common.config.Account;

public interface IAuthProvider {

SessionData login(Account account);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package me.djtheredstoner.devauth.common.auth;

import me.djtheredstoner.devauth.common.DevAuth;

public interface IAuthProviderFactory {

IAuthProviderFactory MOJANG = devAuth -> new MojangAuthProvider();
IAuthProviderFactory MICROSOFT = MicrosoftAuthProvider::new;

IAuthProvider create(DevAuth devAuth);

}
Loading

0 comments on commit 0538d6f

Please sign in to comment.