Skip to content

Commit

Permalink
feat: better web pages & some fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Chicken committed May 28, 2023
1 parent 637ce94 commit 833c6c0
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 100 deletions.
4 changes: 3 additions & 1 deletion Authentication/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ dependencies {
compileOnly ("org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT")
compileOnly ("org.jetbrains:annotations:23.0.0")
implementation ("codes.antti.auth:Common")
implementation ("org.xerial:sqlite-jdbc:3.41.2.1")
}

val javaTarget = 11
Expand Down Expand Up @@ -60,7 +61,8 @@ tasks.shadowJar {
destinationDirectory.set(file("../build"))
archiveClassifier.set("")

relocate ("org.sqlite", "codes.antti.shadow.sqlite")
relocate ("codes.antti.auth.common", "codes.antti.auth.authentication.shadow.common")
relocate ("org.sqlite", "codes.antti.auth.authentication.shadow.sqlite")
}

modrinth {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package codes.antti.auth.authentication;

import codes.antti.auth.common.Database;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Objects;
Expand All @@ -18,7 +19,11 @@ public final class AuthenticationPlugin extends JavaPlugin implements CommandExe
@Override
public void onEnable() {
this.saveDefaultConfig();
if (!getDataFolder().toPath().resolve("login.html").toFile().exists()) this.saveResource("login.html", false);
File webRoot = getDataFolder().toPath().resolve("web").toFile();
if (!webRoot.exists()) {
boolean _ignored = webRoot.mkdirs();
}
if (!webRoot.toPath().resolve("login.html").toFile().exists()) this.saveResource("web/login.html", true);

try {
this.db = new AuthenticationDatabase(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Objects;

public class AuthenticationWebServer {
Expand All @@ -15,10 +16,11 @@ public class AuthenticationWebServer {

private final WebServer http;
private final AuthenticationPlugin plugin;
private String loginPage = null;
private final Path loginPagePath;

public AuthenticationWebServer(@NotNull AuthenticationPlugin plugin) throws IOException {
this.plugin = plugin;
this.loginPagePath = this.plugin.getDataFolder().toPath().resolve("web/login.html");
FileConfiguration config = plugin.getConfig();
this.http = new WebServer(Objects.requireNonNull(config.getString("ip", "0.0.0.0")), config.getInt("port", 8200));
final String root = "/";
Expand Down Expand Up @@ -70,6 +72,8 @@ public AuthenticationWebServer(@NotNull AuthenticationPlugin plugin) throws IOEx
request.respond(200);
});

this.http.serveStatic("/login/", this.plugin.getDataFolder().toPath().resolve("web"));



this.http.handle("/logout", request -> {
Expand Down Expand Up @@ -113,16 +117,15 @@ public AuthenticationWebServer(@NotNull AuthenticationPlugin plugin) throws IOEx
}

private String formatLoginPage(@NotNull String authToken) {
if (this.loginPage == null) {
try {
this.loginPage = Files.readString(this.plugin.getDataFolder().toPath().resolve("login.html"));
} catch (IOException e) {
this.loginPage = "Authenticate using: /auth {{auth_token}}";
this.plugin.getLogger().severe("Couldn't load login page, using a plain one");
e.printStackTrace();
}
String loginPage;
try {
loginPage = Files.readString(this.loginPagePath);
} catch (IOException e) {
loginPage = "Authenticate using: /auth {{auth_token}}";
this.plugin.getLogger().severe("Couldn't load login page, using a plain one");
e.printStackTrace();
}
return this.loginPage.replaceAll("\\{\\{auth_token}}", authToken);
return loginPage.replaceAll("\\{\\{auth_token}}", authToken);
}

public void close() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
package codes.antti.auth.common;

package codes.antti.auth.authentication;

import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
.container {
display: flex;
background-color: #d4d4d8;
border-radius: 3px;
border-radius: 5px;
padding: 12px;
}
.container p {
Expand All @@ -33,7 +33,7 @@
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace;
margin: 0;
padding: 6px;
border-radius: 3px;
border-radius: 5px;
position: relative;
}
#tooltip {
Expand All @@ -45,7 +45,7 @@
transform: translateX(-50%);
z-index: 1;
background-color: #a1a1aa;
border-radius: 3px;
border-radius: 5px;
padding: 8px;
}
</style>
Expand Down
2 changes: 1 addition & 1 deletion Authorization/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ tasks.shadowJar {
destinationDirectory.set(file("../build"))
archiveClassifier.set("")

relocate ("org.sqlite", "codes.antti.shadow.sqlite")
relocate ("codes.antti.auth.common", "codes.antti.auth.authorization.shadow.common")
}

modrinth {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@

import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.io.IOException;

public final class AuthorizationPlugin extends JavaPlugin {
AuthorizationWebServer server;
@Override
public void onEnable() {
this.saveDefaultConfig();
if (!getDataFolder().toPath().resolve("unauthorized.html").toFile().exists()) this.saveResource("unauthorized.html", false);
File webRoot = getDataFolder().toPath().resolve("web").toFile();
if (!webRoot.exists()) {
boolean _ignored = webRoot.mkdirs();
}
if (!webRoot.toPath().resolve("unauthorized.html").toFile().exists()) this.saveResource("web/unauthorized.html", true);

try {
this.server = new AuthorizationWebServer(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.CompletableFuture;

public class AuthorizationWebServer {
private final WebServer http;
private final UserManager userManager;
private String unauthorizedPage = null;
private final Path unauthorizedPagePath;
private final HashMap<String, Boolean> permissionCache = new HashMap<>();
private final HashMap<String, Long> permissionCacheExpiry = new HashMap<>();
private static final long CACHE_SECONDS = 15;
Expand All @@ -26,6 +27,7 @@ public AuthorizationWebServer(AuthorizationPlugin plugin) throws IOException {
this.http = new WebServer(Objects.requireNonNull(config.getString("ip", "0.0.0.0")), config.getInt("port", 8300));
LuckPerms lp = LuckPermsProvider.get();
this.userManager = lp.getUserManager();
this.unauthorizedPagePath = plugin.getDataFolder().toPath().resolve("web/unauthorized.html");

this.http.handle("/auth", request -> {
String host = request.httpExchange.getRequestHeaders().get("host").get(0);
Expand Down Expand Up @@ -58,21 +60,29 @@ public AuthorizationWebServer(AuthorizationPlugin plugin) throws IOException {
return;
}
}

if (this.unauthorizedPage == null) {
try {
this.unauthorizedPage = Files.readString(plugin.getDataFolder().toPath().resolve("unauthorized.html"));
} catch (IOException e) {
this.unauthorizedPage = "You are not authorized to use this application.";
plugin.getLogger().severe("Couldn't load unauthorized page, using a plain one");
e.printStackTrace();
}
String playerName = playerUuid != null ? plugin.getServer().getOfflinePlayer(UUID.fromString(playerUuid)).getName() : "null";

String rawUnauthorizedPage;
try {
rawUnauthorizedPage = Files.readString(this.unauthorizedPagePath);
} catch (IOException e) {
rawUnauthorizedPage = "You are not authorized to use this application.";
plugin.getLogger().severe("Couldn't load unauthorized page, using a plain one");
e.printStackTrace();
}

request.setBody(this.unauthorizedPage, "text/html");
String unauthorizedPage =
rawUnauthorizedPage
.replaceAll("\\{\\{host}}", host)
.replaceAll("\\{\\{permission}}", String.valueOf(permissionNode))
.replaceAll("\\{\\{uuid}}", String.valueOf(playerUuid))
.replaceAll("\\{\\{name}}", String.valueOf(playerName));

request.setBody(unauthorizedPage, "text/html");
request.respond(200);
});

this.http.serveStatic("/unauthorized/", plugin.getDataFolder().toPath().resolve("web"));



this.http.start();
Expand Down
49 changes: 0 additions & 49 deletions Authorization/src/main/resources/unauthorized.html

This file was deleted.

100 changes: 100 additions & 0 deletions Authorization/src/main/resources/web/unauthorized.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Authorization</title>
<style>
html, body {
background-color: #e4e4e7;
display: flex;
justify-content: center;
align-items: center;
height: 100%;
font-family:
-apple-system, BlinkMacSystemFont, avenir next, avenir, segoe ui,
helvetica neue, helvetica, Cantarell, Ubuntu, roboto, noto, arial, sans-serif;
font-size: 1.25em;
line-height: 1.4;
}
.container {
background-color: #d4d4d8;
border-radius: 5px;
padding: 12px;
}
.user-container {
height: 40px;
display: inline-block;
background-color: #e4e4e7;
border-radius: 5px;
}
.user-inner-container {
height: 32px;
display: flex;
gap: 4px;
margin: 4px 8px 4px 4px;
align-items: center;
}
.user-inner-container img {
border-radius: 5px;
width: 32px;
aspect-ratio: 1;
}
.user-inner-container span {
display: inline-block;
height: 40px;
}
.container p {
margin: 2px;
text-align: center;
}
.code {
background-color: #a1a1aa;
font-family: Menlo, Consolas, Monaco, Liberation Mono, Lucida Console, monospace;
margin: 0;
padding: 6px;
border-radius: 5px;
position: relative;
}
button {
all: unset;
background-color: #f87171;
border-color: #e4e4e7;
border-width: 5px;
border-radius: 5px;
color: white;
font-weight: bold;
font-size: 0.75em;
padding: 2px 8px;
margin: 16px 2px 2px;
}
a {
text-decoration: none;
}
</style>
</head>
<body>
<div class="container">
<div class="user-container">
<div class="user-inner-container">
<img src="https://mc-heads.net/avatar/{{uuid}}/32" alt="{{name}}'s player head"/> <span>{{name}}</span>
</div>
</div>
<p>
You are not authorized to use the application at <span class="code">{{host}}</span>.<br>
This requires the <span class="code">{{permission}}</span> permission node. <br>
<a href="/authentication-outpost/logout"><button>Logout</button></a> <a href="/authentication-outpost/logout/all"><button>Logout all browsers</button></a>
</p>
</div>
<script>
setInterval(() => {
fetch("./auth")
.then(res => {
if (res.status === 200) window.location.reload();
})
.catch(() => null)
}, 5 * 1000);
</script>
</body>
</html>
1 change: 0 additions & 1 deletion Common/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ repositories {

dependencies {
compileOnly ("org.jetbrains:annotations:23.0.0")
implementation ("org.xerial:sqlite-jdbc:3.41.2.1")
}

val javaTarget = 11
Expand Down
Loading

0 comments on commit 833c6c0

Please sign in to comment.