Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Merge pull request #13 from ByMartrixx/1.16
Browse files Browse the repository at this point in the history
1.2.0 for 1.16
  • Loading branch information
Dqu1J authored Jul 7, 2021
2 parents 0beadf6 + f74e0c3 commit 296f851
Show file tree
Hide file tree
Showing 14 changed files with 377 additions and 82 deletions.
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ Until player logins, it won't be able to:
- Move or rotate
- Modify their inventory

<br>

**Version Support**
- ![](https://img.shields.io/badge/1.17.x-Supported-green)
- ![](https://img.shields.io/badge/1.16.5-Supported-important)
- ![](https://img.shields.io/badge/1.16.4%3C-Not%20ported-red)

[Modrinth](https://modrinth.com/mod/simpler-auth)
[CurseForge]()
[CurseForge](https://www.curseforge.com/minecraft/mc-mods/simpler-auth)

2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.11.3

# Mod Properties
mod_version = 1.1.3
mod_version = 1.2.0
maven_group = com.dqu.simplerauth
archives_base_name = SimplerAuth

Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/dqu/simplerauth/AuthMod.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
package com.dqu.simplerauth;

import com.dqu.simplerauth.commands.*;
import com.dqu.simplerauth.managers.ConfigManager;
import com.dqu.simplerauth.managers.DbManager;
import com.dqu.simplerauth.managers.LangManager;
import com.dqu.simplerauth.managers.PlayerManager;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v1.CommandRegistrationCallback;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class AuthMod implements ModInitializer {
public static final Logger LOGGER = LogManager.getLogger();
public static PlayerManager playerManager = new PlayerManager();

@Override
public void onInitialize() {
ConfigManager.loadConfig(); // Loads config file
DbManager.loadDatabase(); // Loads password database
LangManager.loadTranslations("en"); // Loads translations
CommandRegistrationCallback.EVENT.register(((dispatcher, dedicated) -> {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/dqu/simplerauth/PlayerObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import net.minecraft.server.network.ServerPlayerEntity;

public class PlayerObject {
private ServerPlayerEntity player;
private final ServerPlayerEntity player;
private boolean authenticated;

public PlayerObject(ServerPlayerEntity player) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package com.dqu.simplerauth.commands;

import com.dqu.simplerauth.DbManager;
import com.dqu.simplerauth.LangManager;
import com.dqu.simplerauth.managers.ConfigManager;
import com.dqu.simplerauth.managers.DbManager;
import com.dqu.simplerauth.managers.LangManager;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.minecraft.server.command.ServerCommandSource;
Expand All @@ -20,14 +21,28 @@ public static void registerCommand(CommandDispatcher<ServerCommandSource> dispat
String newPassword = StringArgumentType.getString(ctx, "newPassword");
ServerPlayerEntity player = ctx.getSource().getPlayer();
String username = player.getEntityName();
String authtype = ConfigManager.getAuthType();

if (authtype.equals("global")) {
if (player.hasPermissionLevel(4)) {
// If a player is an operator, and the password type is global - change the global password
String password = ConfigManager.getString("global-password");
if (password.equals(oldPassword)) {
ConfigManager.setString("global-password", newPassword);
}
}
return 1;
} else if (authtype.equals("none")) {
player.networkHandler.disconnect(LangManager.getLiteralText("config.incorrect"));
}

if (!DbManager.isPlayerRegistered(username)) {
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.changepassword.notregistered"), false);
return 1;
}

if(!DbManager.isPasswordCorrect(username, oldPassword)) {
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.changepassword.wrongpassword"), false);
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.general.notmatch"), false);
return 1;
}

Expand Down
45 changes: 34 additions & 11 deletions src/main/java/com/dqu/simplerauth/commands/LoginCommand.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.dqu.simplerauth.commands;

import com.dqu.simplerauth.AuthMod;
import com.dqu.simplerauth.DbManager;
import com.dqu.simplerauth.LangManager;
import com.dqu.simplerauth.managers.ConfigManager;
import com.dqu.simplerauth.managers.DbManager;
import com.dqu.simplerauth.managers.LangManager;
import com.dqu.simplerauth.PlayerObject;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
Expand All @@ -21,23 +22,45 @@ public static void registerCommand(CommandDispatcher<ServerCommandSource> dispat
String username = ctx.getSource().getPlayer().getEntityName();
ServerPlayerEntity player = ctx.getSource().getPlayer();
PlayerObject playerObject = AuthMod.playerManager.get(player);
String passwordtype = ConfigManager.getAuthType();

if (playerObject.isAuthenticated()) {
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.login.alreadylogged"), false);
return 1;
}

if (!DbManager.isPlayerRegistered(username)) {
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.login.notregistered"), false);
return 1;
}
if (passwordtype.equals("local")) {
// Local Password Authentication

if (!DbManager.isPlayerRegistered(username)) {
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.login.notregistered"), false);
return 1;
}

if (DbManager.isPasswordCorrect(username, password)) {
playerObject.authenticate();
if (!player.isCreative()) player.setInvulnerable(false);
if (ConfigManager.getBoolean("sessions-enabled"))
DbManager.sessionCreate(player.getEntityName(), player.getIp());
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.general.authenticated"), false);
} else {
player.networkHandler.disconnect(LangManager.getLiteralText("command.general.notmatch"));
}
} else if (passwordtype.equals("global")) {
// Global Password Authentication
String globalPassword = ConfigManager.getString("global-password");

if (DbManager.isPasswordCorrect(username, password)) {
playerObject.authenticate();
if (!player.isCreative()) player.setInvulnerable(false);
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.login.success"), false);
if (password.equals(globalPassword)) {
playerObject.authenticate();
if (!player.isCreative()) player.setInvulnerable(false);
if (ConfigManager.getBoolean("sessions-enabled"))
DbManager.sessionCreate(player.getEntityName(), player.getIp());
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.general.authenticated"), false);
} else {
player.networkHandler.disconnect(LangManager.getLiteralText("command.general.notmatch"));
}
} else {
player.networkHandler.disconnect(LangManager.getLiteralText("command.login.wrongpassword"));
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.login.wrongpassword"), false);
}

return 1;
Expand Down
19 changes: 14 additions & 5 deletions src/main/java/com/dqu/simplerauth/commands/RegisterCommand.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.dqu.simplerauth.commands;

import com.dqu.simplerauth.AuthMod;
import com.dqu.simplerauth.DbManager;
import com.dqu.simplerauth.LangManager;
import com.dqu.simplerauth.managers.ConfigManager;
import com.dqu.simplerauth.managers.DbManager;
import com.dqu.simplerauth.managers.LangManager;
import com.dqu.simplerauth.PlayerObject;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
Expand All @@ -19,25 +20,33 @@ public static void registerCommand(CommandDispatcher<ServerCommandSource> dispat
.then(argument("repeatPassword", StringArgumentType.word())
.executes(ctx -> {
String password = StringArgumentType.getString(ctx, "password");
String passwordRepeat = StringArgumentType.getString(ctx, "password");
String passwordRepeat = StringArgumentType.getString(ctx, "repeatPassword");
ServerPlayerEntity player = ctx.getSource().getPlayer();
String username = player.getEntityName();
String authtype = ConfigManager.getAuthType();

if (authtype.equals("global")) {
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.register.globaltype"), false);
return 1;
} else if (authtype.equals("none")) {
player.networkHandler.disconnect(LangManager.getLiteralText("config.incorrect"));
}

if (DbManager.isPlayerRegistered(username)) {
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.register.alreadyregistered"), false);
return 1;
}

if (!password.equals(passwordRepeat)) {
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.register.passwordrepeatwrong"), false);
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.general.notmatch"), false);
return 1;
}

DbManager.addPlayerDatabase(username, password);
PlayerObject playerObject = AuthMod.playerManager.get(player);
playerObject.authenticate();
if (!player.isCreative()) player.setInvulnerable(false);
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.register.success"), false);
ctx.getSource().sendFeedback(LangManager.getLiteralText("command.general.authenticated"), false);
return 1;
})
)
Expand Down
77 changes: 76 additions & 1 deletion src/main/java/com/dqu/simplerauth/listeners/OnPlayerConnect.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,87 @@
package com.dqu.simplerauth.listeners;

import com.dqu.simplerauth.LangManager;
import com.dqu.simplerauth.AuthMod;
import com.dqu.simplerauth.PlayerObject;
import com.dqu.simplerauth.managers.ConfigManager;
import com.dqu.simplerauth.managers.DbManager;
import com.dqu.simplerauth.managers.LangManager;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import net.minecraft.server.network.ServerPlayerEntity;

import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class OnPlayerConnect {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

public static void listen(ServerPlayerEntity player) {
player.setInvulnerable(true);
player.stopRiding();
player.sendMessage(LangManager.getLiteralText("player.connect.authenticate"), false);

boolean skiponline = ConfigManager.getBoolean("skip-online-auth");
if (skiponline && testPlayerOnline(player) && DbManager.isPlayerRegistered(player.getEntityName())) {
PlayerObject playerObject = AuthMod.playerManager.get(player);
playerObject.authenticate();
player.sendMessage(LangManager.getLiteralText("command.general.authenticated"), false);
AuthMod.LOGGER.info(player.getEntityName() + " is using an online account, authenticated automatically.");
return;
}

boolean sessionenabled = ConfigManager.getBoolean("sessions-enabled");
if (sessionenabled) {
if (DbManager.sessionVerify(player.getEntityName(), player.getIp())) {
PlayerObject playerObject = AuthMod.playerManager.get(player);
playerObject.authenticate();
DbManager.sessionCreate(player.getEntityName(), player.getIp());
player.sendMessage(LangManager.getLiteralText("command.general.authenticated"), false);
} else {
DbManager.sessionDestroy(player.getEntityName());
}
}
}

public static boolean testPlayerOnline(ServerPlayerEntity player) {
String uuid = player.getGameProfile().getId().toString().toLowerCase().replace("-", "");

// Check if UUID is valid
Pattern pattern = Pattern.compile("^[a-z0-9]{32}$");
Matcher matcher = pattern.matcher(uuid);
if (!matcher.matches()) return false;

String content = "";
try {
HttpsURLConnection connection = (HttpsURLConnection) new URL("https://api.mojang.com/users/profiles/minecraft/" + player.getEntityName()).openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);

int response = connection.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
InputStream stream = connection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder stringBuilder = new StringBuilder();
String out;
while ((out = bufferedReader.readLine()) != null) {
stringBuilder.append(out);
}
content = stringBuilder.toString();
} else return false;
} catch (Exception e) {
AuthMod.LOGGER.error(e);
return false;
}

JsonObject jsonObject = GSON.fromJson(content, JsonObject.class);
String realUuid = jsonObject.get("id").getAsString().toLowerCase();
return uuid.equals(realUuid);
}
}
Loading

0 comments on commit 296f851

Please sign in to comment.