Added pinmanager and message AUTH event.
This commit is contained in:
parent
08d14e1122
commit
bf527e31d2
3 changed files with 146 additions and 1 deletions
72
src/me/mctp/managers/PinManager.java
Normal file
72
src/me/mctp/managers/PinManager.java
Normal file
|
@ -0,0 +1,72 @@
|
|||
package me.mctp.managers;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.UUID;
|
||||
import java.util.WeakHashMap;
|
||||
|
||||
public class PinManager {
|
||||
private static WeakHashMap<UUID, String> pins = new WeakHashMap<>();
|
||||
|
||||
/**
|
||||
* Get the pin of a player
|
||||
*
|
||||
* @param pUUID The player UUID
|
||||
*
|
||||
* @return The pin
|
||||
*/
|
||||
public static String getPIN(UUID pUUID) {
|
||||
if (pUUID == null) return null;
|
||||
|
||||
if (!pins.containsKey(pUUID)) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
SecureRandom random = new SecureRandom();
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
builder.append(random.nextInt(9));
|
||||
}
|
||||
String pin = builder.toString();
|
||||
|
||||
pins.put(pUUID, pin);
|
||||
return pin;
|
||||
} else {
|
||||
return pins.get(pUUID);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a player has a pin
|
||||
*
|
||||
* @param pUUID The player UUID
|
||||
*
|
||||
* @return The pin
|
||||
*/
|
||||
public static boolean hasPin(UUID pUUID) {
|
||||
return pUUID != null && pins.containsKey(pUUID);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the pin is correct
|
||||
*
|
||||
* @param pUUID The player UUID
|
||||
* @param pin The pin
|
||||
*
|
||||
* @return true/false
|
||||
*/
|
||||
public static boolean checkPin(UUID pUUID, String pin) {
|
||||
if (pUUID == null || pin == null || pin.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return pins.containsKey(pUUID) && pin.equals(pins.get(pUUID));
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the pin
|
||||
*
|
||||
* @param pUUID The player UUID
|
||||
*/
|
||||
public static void removePin(UUID pUUID) {
|
||||
if (pUUID != null) {
|
||||
pins.remove(pUUID);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,13 +1,16 @@
|
|||
package me.mctp.socket;
|
||||
|
||||
import me.mctp.Main;
|
||||
import me.mctp.managers.PinManager;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
import org.json.simple.JSONObject;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.UUID;
|
||||
|
||||
public class Client {
|
||||
private String url;
|
||||
|
@ -48,7 +51,36 @@ public class Client {
|
|||
|
||||
@Override
|
||||
public void onMessage(String s) {
|
||||
//TODO Add message handler
|
||||
JSONObject json = JSONUtil.parse(s);
|
||||
if (json == null) return;
|
||||
|
||||
String str = JSONUtil.getValue(json, "action");
|
||||
if (str == null || str.isEmpty()) return;
|
||||
|
||||
if (str.equals("VERIFY")) {
|
||||
String uuid = JSONUtil.getValue(json, "uuid");
|
||||
if (uuid == null || uuid.isEmpty()) return;
|
||||
|
||||
String pin = JSONUtil.getValue(json, "pin");
|
||||
if (pin == null || pin.isEmpty()) return;
|
||||
|
||||
UUID pUUID = UUID.fromString(JSONUtil.fixUUID(uuid));
|
||||
boolean verified = true;
|
||||
Player player = Bukkit.getPlayer(uuid);
|
||||
if (player == null || !player.isOnline()) {
|
||||
verified = false;
|
||||
}
|
||||
|
||||
if (!PinManager.checkPin(pUUID, pin)) {
|
||||
verified = false;
|
||||
}
|
||||
|
||||
JSONObject reply = new JSONObject();
|
||||
reply.put("action", "AUTHENTICATION");
|
||||
reply.put("verified", verified);
|
||||
reply.put("uuid", uuid);
|
||||
this.send(reply.toJSONString());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
41
src/me/mctp/socket/JSONUtil.java
Normal file
41
src/me/mctp/socket/JSONUtil.java
Normal file
|
@ -0,0 +1,41 @@
|
|||
package me.mctp.socket;
|
||||
|
||||
import org.json.simple.JSONObject;
|
||||
import org.json.simple.parser.JSONParser;
|
||||
import org.json.simple.parser.ParseException;
|
||||
|
||||
public class JSONUtil {
|
||||
public static JSONObject parse(String string) {
|
||||
try {
|
||||
JSONParser parser = new JSONParser();
|
||||
return (JSONObject) parser.parse(string);
|
||||
} catch (ParseException ignored) { }
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String getValue(JSONObject object, String string) {
|
||||
if (object != null && !object.isEmpty()) {
|
||||
Object obj = object.get(string);
|
||||
String str = null;
|
||||
if (obj != null) str = String.valueOf(obj);
|
||||
return str;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static String fixUUID(String uuid) {
|
||||
if (uuid != null && !uuid.isEmpty()) {
|
||||
StringBuilder sb = new StringBuilder(uuid);
|
||||
sb.insert(8, "-");
|
||||
sb = new StringBuilder(sb.toString());
|
||||
sb.insert(13, "-");
|
||||
sb = new StringBuilder(sb.toString());
|
||||
sb.insert(18, "-");
|
||||
sb = new StringBuilder(sb.toString());
|
||||
sb.insert(23, "-");
|
||||
return sb.toString();
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue