Skip to content

Commit

Permalink
Initial placeholders support for backend servers
Browse files Browse the repository at this point in the history
  • Loading branch information
UserNugget committed Jun 20, 2024
1 parent b93122d commit 7f9a8d3
Show file tree
Hide file tree
Showing 10 changed files with 450 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ publishing {
javadoc {
getOptions().setEncoding("UTF-8")
getOptions().setCharSet("UTF-8")
getOptions().setSource("11")
getOptions().setSource("17")
getOptions().links("https://docs.oracle.com/en/java/javase/11/docs/api/")

// Remove "undefined" from search paths when generating javadoc for a non-modular project. (JDK-8215291)
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/net/elytrium/limboauth/LimboAuth.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
import net.elytrium.limboauth.floodgate.FloodgateApiHolder;
import net.elytrium.limboauth.handler.AuthSessionHandler;
import net.elytrium.limboauth.listener.AuthListener;
import net.elytrium.limboauth.listener.BackendEndpointsListener;
import net.elytrium.limboauth.model.RegisteredPlayer;
import net.elytrium.limboauth.model.SQLRuntimeException;
import net.kyori.adventure.text.Component;
Expand Down Expand Up @@ -405,6 +406,11 @@ public void reload() {
EventManager eventManager = this.server.getEventManager();
eventManager.unregisterListeners(this);
eventManager.register(this, new AuthListener(this, this.playerDao, this.floodgateApi));
if (Settings.IMP.MAIN.BACKEND_API.ENABLED) {
eventManager.register(this, new BackendEndpointsListener(this));
} else {
this.server.getChannelRegistrar().unregister(BackendEndpointsListener.API_CHANNEL);
}

if (this.purgeCacheTask != null) {
this.purgeCacheTask.cancel();
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/net/elytrium/limboauth/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,27 @@ public Title.Times toTimes() {
}
}

@Create
public Settings.MAIN.BACKEND_API BACKEND_API;

public static class BACKEND_API {

@Comment({
"Should backend API be enabled?",
"Required for PlaceholderAPI expansion to work (https://github.com/UserNugget/LimboAuth-Expansion)"
})
public boolean ENABLED = true;

@Comment({
"Available endpoints:",
" premium_state, hash, totp_token, login_date, reg_date, token_issued_at,",
" uuid, premium_uuid, ip, login_ip, token_issued_at"
})
public List<String> ENABLED_ENDPOINTS = List.of(
"premium_state", "login_date", "reg_date", "uuid", "premium_uuid", "token_issued_at"
);
}

@Create
public MAIN.COMMAND_PERMISSION_STATE COMMAND_PERMISSION_STATE;

Expand Down
35 changes: 35 additions & 0 deletions src/main/java/net/elytrium/limboauth/backend/Endpoint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2021 - 2024 Elytrium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.elytrium.limboauth.backend;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import net.elytrium.limboauth.LimboAuth;

public abstract class Endpoint {

protected final LimboAuth plugin;

public Endpoint(LimboAuth plugin) {
this.plugin = plugin;
}

public abstract void write(ByteArrayDataOutput output);

public abstract void read(ByteArrayDataInput input);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2021 - 2024 Elytrium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.elytrium.limboauth.backend.type;

import java.util.function.Function;
import net.elytrium.limboauth.LimboAuth;
import net.elytrium.limboauth.handler.AuthSessionHandler;
import net.elytrium.limboauth.model.RegisteredPlayer;

public class LongDatabaseEndpoint extends LongEndpoint {

public LongDatabaseEndpoint(LimboAuth plugin, String type, String username, long value) {
super(plugin, type, username, value);
}

public LongDatabaseEndpoint(LimboAuth plugin, String type, Function<RegisteredPlayer, Long> function) {
super(plugin, type, username -> {
RegisteredPlayer player = AuthSessionHandler.fetchInfo(plugin.getPlayerDao(), username);
if (player == null) {
return Long.MIN_VALUE;
} else {
return function.apply(player);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2021 - 2024 Elytrium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.elytrium.limboauth.backend.type;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import java.util.function.Function;
import net.elytrium.limboauth.LimboAuth;
import net.elytrium.limboauth.Settings;
import net.elytrium.limboauth.backend.Endpoint;

public class LongEndpoint extends Endpoint {

private String type;
private Function<String, Long> function;
private String username;
private long value;

public LongEndpoint(LimboAuth plugin, String type, Function<String, Long> function) {
super(plugin);
this.type = type;
this.function = function;
}

public LongEndpoint(LimboAuth plugin, String type, String username, long value) {
super(plugin);
this.type = type;
this.username = username;
this.value = value;
}

@Override
public void write(ByteArrayDataOutput output) {
output.writeUTF(this.type);
if (!Settings.IMP.MAIN.BACKEND_API.ENABLED_ENDPOINTS.contains(this.type)) {
output.writeInt(-1);
output.writeUTF(this.username);
return;
}

output.writeInt(0);
output.writeUTF(this.username);
output.writeLong(this.value);
}

@Override
public void read(ByteArrayDataInput input) {
int version = input.readInt();
if (version != 0) {
throw new IllegalStateException("unsupported '" + this.type + "' endpoint version: " + version);
}

this.username = input.readUTF();
this.value = this.function.apply(this.username);
}

@Override
public String toString() {
return "LongEndpoint{"
+ "username='" + this.username + '\''
+ ", value=" + this.value
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright (C) 2021 - 2024 Elytrium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.elytrium.limboauth.backend.type;

import java.util.function.Function;
import net.elytrium.limboauth.LimboAuth;
import net.elytrium.limboauth.handler.AuthSessionHandler;
import net.elytrium.limboauth.model.RegisteredPlayer;

public class StringDatabaseEndpoint extends StringEndpoint {

public StringDatabaseEndpoint(LimboAuth plugin, String type, String username, String value) {
super(plugin, type, username, value);
}

public StringDatabaseEndpoint(LimboAuth plugin, String type, Function<RegisteredPlayer, String> function) {
super(plugin, type, username -> {
RegisteredPlayer player = AuthSessionHandler.fetchInfo(plugin.getPlayerDao(), username);
if (player == null) {
return "";
} else {
return function.apply(player);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2021 - 2024 Elytrium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.elytrium.limboauth.backend.type;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import java.util.function.Function;
import net.elytrium.limboauth.LimboAuth;
import net.elytrium.limboauth.Settings;
import net.elytrium.limboauth.backend.Endpoint;

public class StringEndpoint extends Endpoint {

private String type;
private Function<String, String> function;
private String username;
private String value;

public StringEndpoint(LimboAuth plugin, String type, Function<String, String> function) {
super(plugin);
this.type = type;
this.function = function;
}

public StringEndpoint(LimboAuth plugin, String type, String username, String value) {
super(plugin);
this.type = type;
this.username = username;
this.value = value;
}

@Override
public void write(ByteArrayDataOutput output) {
output.writeUTF(this.type);
if (!this.type.equals("available_endpoints") && !Settings.IMP.MAIN.BACKEND_API.ENABLED_ENDPOINTS.contains(this.type)) {
output.writeInt(-1);
output.writeUTF(this.username);
return;
}

output.writeInt(0);
output.writeUTF(this.username);
output.writeUTF(this.value);
}

@Override
public void read(ByteArrayDataInput input) {
int version = input.readInt();
if (version != 0) {
throw new IllegalStateException("unsupported '" + this.type + "' endpoint version: " + version);
}

this.username = input.readUTF();
this.value = this.function.apply(this.username);
}

@Override
public String toString() {
return "StringEndpoint{"
+ "username='" + this.username + '\''
+ ", value=" + this.value
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2021 - 2024 Elytrium
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package net.elytrium.limboauth.backend.type;

import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import net.elytrium.limboauth.LimboAuth;
import net.elytrium.limboauth.backend.Endpoint;

public class UnknownEndpoint extends Endpoint {

private String type;

public UnknownEndpoint(LimboAuth plugin) {
super(plugin);
}

public UnknownEndpoint(LimboAuth plugin, String type) {
super(plugin);
this.type = type;
}

@Override
public void write(ByteArrayDataOutput output) {
output.writeUTF(this.type);
output.writeInt(-2);
}

@Override
public void read(ByteArrayDataInput input) {
throw new UnsupportedOperationException();
}

@Override
public String toString() {
return "UnknownEndpoint{"
+ "type='" + this.type + '\''
+ '}';
}
}
Loading

0 comments on commit 7f9a8d3

Please sign in to comment.