-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 0538d6f
Showing
35 changed files
with
1,632 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
**/build | ||
.gradle | ||
**/run | ||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
101
common/src/main/java/me/djtheredstoner/devauth/common/DevAuth.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
common/src/main/java/me/djtheredstoner/devauth/common/Environment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
common/src/main/java/me/djtheredstoner/devauth/common/Properties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
common/src/main/java/me/djtheredstoner/devauth/common/auth/IAuthProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
12 changes: 12 additions & 0 deletions
12
common/src/main/java/me/djtheredstoner/devauth/common/auth/IAuthProviderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
Oops, something went wrong.