diff --git a/src/main/java/us/camin/JoinListener.java b/src/main/java/us/camin/JoinListener.java index 4331efa..55350ad 100644 --- a/src/main/java/us/camin/JoinListener.java +++ b/src/main/java/us/camin/JoinListener.java @@ -18,54 +18,48 @@ package us.camin; */ -import java.io.BufferedReader; -import java.io.BufferedInputStream; -import java.io.InputStreamReader; -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.net.HttpURLConnection; import java.util.logging.Logger; -import java.util.Scanner; - import org.bukkit.entity.Player; import org.bukkit.event.Listener; import org.bukkit.event.EventHandler; import org.bukkit.event.player.PlayerLoginEvent; import org.bukkit.event.player.PlayerJoinEvent; +import org.bukkit.permissions.PermissionAttachment; +import java.io.IOException; -import org.json.JSONArray; -import org.json.JSONObject; -import org.json.JSONException; +import us.camin.api.ValidationResponse; public class JoinListener implements Listener { Logger log = Logger.getLogger("Caminus.Join"); private String m_url; + private Plugin m_plugin; - public JoinListener() { + public JoinListener(Plugin p) { + m_plugin = p; } - public void setURL(String url) { - m_url = url; - } - - @EventHandler public void onPlayerLogin(PlayerLoginEvent event) { Player p = event.getPlayer(); - if (p.hasPermission("caminus.whitelisted")) - return; + ValidationResponse resp = null; try { - if (!isUserAuthed(p.getName())) { + resp = m_plugin.api().validatePlayer(p.getName()); + if (!resp.valid) { event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "An active camin.us account is required."); + return; } - } catch (MalformedURLException e) { - event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Auth URL is invalid!"); } catch (IOException e) { - event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Camin.us auth server seems down."); - } catch (JSONException e) { - event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Bad auth server response."); + event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "Camin.us auth server seems down."); + return; } + PermissionAttachment att = p.addAttachment(m_plugin); + for(String perm : resp.permissions) { + log.info("Granting "+perm); + att.setPermission(perm, true); + } + p.recalculatePermissions(); + log.info(p.hasPermission("caminus.whitelisted")+" "+p.hasPermission("bukkit.command.op.give")); + log.info(p.hasPermission("permissions.build")+""); } @EventHandler @@ -73,13 +67,9 @@ public class JoinListener implements Listener { Player p = event.getPlayer(); String[] motd = null; try { - motd = fetchMOTD(p.getName()); - } catch (MalformedURLException e) { - p.sendMessage("Could not fetch MOTD: Bad URL"); + motd = m_plugin.api().fetchMOTD(p.getName()); } catch (IOException e) { p.sendMessage("Could not fetch MOTD: Communication error"); - } catch (JSONException e) { - p.sendMessage("Could not fetch MOTD: Bad JSON"); } if (motd != null) { for(String msg : motd) { @@ -87,45 +77,4 @@ public class JoinListener implements Listener { } } } - - public String[] fetchMOTD(String user) throws IOException, MalformedURLException, JSONException { - URL motdService = new URL(m_url+"motd/"+user); - log.info("Fetching MOTD for "+user+" from "+motdService); - HttpURLConnection conn = (HttpURLConnection)motdService.openConnection(); - BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); - String jsonStr; - // Stupid scanner trick. \A means "beginning of input boundary". - try { - jsonStr = new java.util.Scanner(in).useDelimiter("\\A").next(); - } catch (java.util.NoSuchElementException e) { - jsonStr = ""; - } - in.close(); - JSONObject jsonObj = new JSONObject(jsonStr); - JSONArray motd = jsonObj.getJSONArray("motd"); - String[] ret = new String[motd.length()]; - for (int i = 0;i. + */ + + +import java.io.BufferedReader; +import java.io.BufferedInputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URL; +import java.net.HttpURLConnection; +import java.util.logging.Logger; +import java.util.Scanner; + +import org.json.JSONArray; +import org.json.JSONObject; +import org.json.JSONException; + +public class Server { + Logger log = Logger.getLogger("Caminus.API"); + private String m_url; + + public ValidationResponse validatePlayer(String name) throws IOException { + ValidationResponse resp = new ValidationResponse(); + URL authServer = new URL(m_url+"validate/"+name); + log.info("Authing "+name+" against "+authServer); + HttpURLConnection conn = (HttpURLConnection)authServer.openConnection(); + BufferedInputStream in = new BufferedInputStream(conn.getInputStream()); + String jsonStr; + try { + jsonStr = new java.util.Scanner(in).useDelimiter("\\A").next(); + } catch (java.util.NoSuchElementException e) { + jsonStr = ""; + } + in.close(); + try { + JSONObject jsonObj = new JSONObject(jsonStr); + resp.valid = jsonObj.optBoolean("valid"); + if (!resp.valid) + return resp; + JSONArray perms = jsonObj.getJSONArray("permissions"); + resp.permissions = new String[perms.length()]; + for (int i = 0;i. + */ + + +public class ValidationResponse { + public boolean valid = false; + public String[] permissions = new String[0]; +} + diff --git a/src/test/java/us/camin/APIServer.java b/src/test/java/us/camin/APIServer.java index c43aa5b..08dee0f 100644 --- a/src/test/java/us/camin/APIServer.java +++ b/src/test/java/us/camin/APIServer.java @@ -52,9 +52,9 @@ public class APIServer { ServletOutputStream out = resp.getOutputStream(); if (req.getPathInfo().equals("/TestUser")) - out.println("{valid: true, error: ''}"); + out.println("{valid: true, error: '', permissions: ['*']}"); else - out.println("{valid: false, error: 'Test Failure'}"); + out.println("{valid: false, error: 'Test Failure', permissions: []}"); } } diff --git a/src/test/java/us/camin/JoinTest.java b/src/test/java/us/camin/JoinTest.java index be3b70b..cc7d8b3 100644 --- a/src/test/java/us/camin/JoinTest.java +++ b/src/test/java/us/camin/JoinTest.java @@ -25,16 +25,16 @@ import static org.junit.Assert.*; import java.io.IOException; -import us.camin.JoinListener; +import us.camin.api.Server; +import us.camin.api.ValidationResponse; public class JoinTest { - private JoinListener listener; + private Server api; private APIServer server; @Before public void setup() throws Exception { server = new APIServer(); server.start(); - listener = new JoinListener(); - listener.setURL("http://localhost:8001/api/"); + api = new Server("http://localhost:8001/api/"); } @After public void teardown() throws Exception { @@ -42,16 +42,22 @@ public class JoinTest { } @Test public void validUser() throws IOException, JSONException { - assertTrue(listener.isUserAuthed("TestUser")); + ValidationResponse resp = api.validatePlayer("TestUser"); + assertTrue(resp.valid); + assertNotNull(resp.permissions); + assertTrue(resp.permissions.length>0); } @Test public void invaliduser() throws IOException, JSONException { - assertFalse(listener.isUserAuthed("InvalidUser")); + ValidationResponse resp = api.validatePlayer("InvalidUser"); + assertFalse(resp.valid); + assertNotNull(resp.permissions); + assertEquals(resp.permissions.length, 0); } @Test public void motd() throws IOException, JSONException { String[] goodMOTD = {"Test MOTD"}; - String[] motd = listener.fetchMOTD("TestUser"); + String[] motd = api.fetchMOTD("TestUser"); assertArrayEquals(null, goodMOTD, motd); } }