Skip to content

Commit 801dcf4

Browse files
authored
Implement zander-auth for backend Minecraft-based authentication. (#16) (#17)
* Implement auth plugin into suite. * Added a Server Ping to auth and config option. Bumped pathing package on Proxy
1 parent cd77182 commit 801dcf4

10 files changed

Lines changed: 204 additions & 12 deletions

File tree

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
<modules>
1313
<module>zander-proxy</module>
1414
<module>zander-hub</module>
15+
<module>zander-auth</module>
1516
</modules>
1617

1718
<properties>

zander-auth/pom.xml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>zander</artifactId>
7+
<groupId>org.modularsoft</groupId>
8+
<version>1.0</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>zander-auth</artifactId>
13+
<version>1.0</version>
14+
15+
<repositories>
16+
<!-- PaperMC/WaterFall -->
17+
<repository>
18+
<id>papermc</id>
19+
<url>https://papermc.io/repo/repository/maven-public/</url>
20+
</repository>
21+
</repositories>
22+
23+
<dependencies>
24+
<!-- PaperMC -->
25+
<dependency>
26+
<groupId>io.papermc.paper</groupId>
27+
<artifactId>paper-api</artifactId>
28+
<version>1.20.2-R0.1-SNAPSHOT</version>
29+
<scope>provided</scope>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.googlecode.json-simple</groupId>
33+
<artifactId>json-simple</artifactId>
34+
<version>1.1.1</version>
35+
<scope>compile</scope>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.projectlombok</groupId>
39+
<artifactId>lombok</artifactId>
40+
<version>1.18.22</version>
41+
<scope>compile</scope>
42+
</dependency>
43+
<dependency>
44+
<groupId>io.github.ModularEnigma</groupId>
45+
<artifactId>Requests</artifactId>
46+
<version>1.0.3</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.jayway.jsonpath</groupId>
50+
<artifactId>json-path</artifactId>
51+
<version>2.8.0</version>
52+
</dependency>
53+
<dependency>
54+
<groupId>com.google.code.gson</groupId>
55+
<artifactId>gson</artifactId>
56+
<version>2.8.9</version>
57+
<scope>compile</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
</project>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.modularsoft.zander.auth;
2+
3+
import net.kyori.adventure.text.Component;
4+
import net.kyori.adventure.text.TextComponent;
5+
import net.kyori.adventure.text.format.NamedTextColor;
6+
import org.bukkit.plugin.PluginManager;
7+
import org.bukkit.plugin.java.JavaPlugin;
8+
import org.modularsoft.zander.auth.events.AuthPlayerJoin;
9+
import org.modularsoft.zander.auth.events.UserOnServerPing;
10+
11+
public class ZanderAuthMain extends JavaPlugin {
12+
public static ZanderAuthMain plugin;
13+
14+
public void onEnable() {
15+
plugin = this;
16+
17+
// Init Message
18+
TextComponent enabledMessage = Component.empty()
19+
.color(NamedTextColor.GREEN)
20+
.append(Component.text("\n\nZander Auth has been enabled.\n"))
21+
.append(Component.text("Running Version " + plugin.getDescription().getVersion() + "\n"))
22+
.append(Component.text("GitHub Repository: https://github.com/ModularSoftAU/zander\n"))
23+
.append(Component.text("Created by Modular Software\n\n", NamedTextColor.DARK_PURPLE));
24+
getServer().sendMessage(enabledMessage);
25+
26+
// Event Registry
27+
PluginManager pluginmanager = this.getServer().getPluginManager();
28+
pluginmanager.registerEvents(new AuthPlayerJoin(this), this);
29+
pluginmanager.registerEvents(new UserOnServerPing(), this);
30+
31+
saveConfig();
32+
}
33+
34+
@Override
35+
public void onDisable() {}
36+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package org.modularsoft.zander.auth.events;
2+
3+
import com.jayway.jsonpath.JsonPath;
4+
import io.github.ModularEnigma.Request;
5+
import io.github.ModularEnigma.Response;
6+
import net.md_5.bungee.api.chat.TextComponent;
7+
import org.bukkit.ChatColor;
8+
import org.bukkit.entity.Player;
9+
import org.bukkit.event.EventHandler;
10+
import org.bukkit.event.Listener;
11+
import org.bukkit.event.player.PlayerJoinEvent;
12+
import org.modularsoft.zander.auth.ZanderAuthMain;
13+
import org.modularsoft.zander.auth.model.user.UserAuth;
14+
15+
public class AuthPlayerJoin implements Listener {
16+
ZanderAuthMain plugin;
17+
public AuthPlayerJoin(ZanderAuthMain plugin) {
18+
this.plugin = plugin;
19+
}
20+
21+
@EventHandler
22+
public void onPlayerJoin(PlayerJoinEvent event) {
23+
Player player = event.getPlayer();
24+
25+
try {
26+
//
27+
// Send a user verification request.
28+
//
29+
UserAuth authUser = UserAuth.builder()
30+
.uuid(player.getUniqueId())
31+
.username(player.getDisplayName())
32+
.build();
33+
34+
Request authUserReq = Request.builder()
35+
.setURL(plugin.getConfig().get("BaseAPIURL") + "/user/verify")
36+
.setMethod(Request.Method.POST)
37+
.addHeader("x-access-token", String.valueOf(plugin.getConfig().get("APIKey")))
38+
.setRequestBody(authUser.toString())
39+
.build();
40+
41+
Response authUserRes = authUserReq.execute();
42+
String authUserJson = authUserRes.getBody();
43+
44+
Boolean success = JsonPath.parse(authUserJson).read("$.success");
45+
String message = JsonPath.read(authUserJson, "$.message");
46+
47+
plugin.getLogger().info("Success: " + success);
48+
plugin.getLogger().info("Message: " + message);
49+
50+
if (success) {
51+
event.getPlayer().kickPlayer(ChatColor.GREEN + message);
52+
} else {
53+
event.getPlayer().kickPlayer(ChatColor.RED + message);
54+
}
55+
} catch (Exception e) {
56+
String reason = ChatColor.RED + "An error has occurred. Is the API down?";
57+
player.kickPlayer(reason);
58+
System.out.println(e);
59+
}
60+
}
61+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package org.modularsoft.zander.auth.events;
2+
3+
import org.bukkit.ChatColor;
4+
import org.bukkit.event.EventHandler;
5+
import org.bukkit.event.Listener;
6+
import org.bukkit.event.server.ServerListPingEvent;
7+
8+
import static org.modularsoft.zander.auth.ZanderAuthMain.plugin;
9+
10+
public class UserOnServerPing implements Listener {
11+
@EventHandler
12+
public void onServerPing(ServerListPingEvent event) {
13+
String motd = plugin.getConfig().getString("MOTDTopLine");
14+
event.setMotd(ChatColor.translateAlternateColorCodes('&', motd));
15+
}
16+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.modularsoft.zander.auth.model.user;
2+
3+
import com.google.gson.Gson;
4+
import lombok.Builder;
5+
import lombok.Getter;
6+
7+
import java.util.UUID;
8+
9+
@Builder
10+
public class UserAuth {
11+
12+
@Getter UUID uuid;
13+
@Getter String username;
14+
15+
@Override
16+
public String toString() {
17+
return new Gson().toJson(this);
18+
}
19+
20+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
BaseAPIURL: "http://localhost:8080/api"
2+
APIKey: "KEY"
3+
MOTDTopLine: "&e&lMy &2&nMinecraft&r &6&oAUTH Server"
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
main: org.modularsoft.zander.auth.ZanderAuthMain
2+
name: zander-auth
3+
version: ${project.version}
4+
author: ModularSoft
5+
api-version: 1.19

zander-hub/src/main/java/org/modularsoft/zander/hub/events/HubPlayerJoin.java

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.bukkit.metadata.MetadataValue;
2020

2121
import java.util.List;
22-
import java.util.Random;
2322

2423
public class HubPlayerJoin implements Listener {
2524
ZanderHubMain plugin;
@@ -65,16 +64,6 @@ public void onPlayerJoin(PlayerJoinEvent event) {
6564
event.getPlayer().sendMessage(" "); // Separate between messages
6665
}
6766

68-
// NOTE:: This has been removed due to the MOTD having lack of purpose and content.
69-
70-
// if (player.hasPlayedBefore()) {
71-
// // Dispatch MOTD to user
72-
// List<String> motd = plugin.configurationManager.getmotd().getStringList("motd");
73-
// for (String s : motd) {
74-
// event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', s));
75-
// }
76-
// }
77-
7867
// Play random sound
7968
int randomSoundIndex = (int) (Math.random() * Sound.values().length - 1);
8069
Sound randomChosenSound = Sound.values()[randomSoundIndex];

zander-proxy/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
<dependency>
6969
<groupId>com.jayway.jsonpath</groupId>
7070
<artifactId>json-path</artifactId>
71-
<version>2.8.0</version>
71+
<version>2.9.0</version>
7272
</dependency>
7373
<dependency>
7474
<groupId>com.google.code.gson</groupId>

0 commit comments

Comments
 (0)