From 906871dfc22594739024a414d58a8875fe052a38 Mon Sep 17 00:00:00 2001 From: Trever Fischer Date: Mon, 5 Mar 2012 19:19:13 -0500 Subject: [PATCH] Implement new validation api --- src/main/java/us/camin/JoinListener.java | 25 +++++++++++++----------- src/test/java/us/camin/JoinTest.java | 4 ++-- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/src/main/java/us/camin/JoinListener.java b/src/main/java/us/camin/JoinListener.java index 596d05b..1816a4a 100644 --- a/src/main/java/us/camin/JoinListener.java +++ b/src/main/java/us/camin/JoinListener.java @@ -48,14 +48,6 @@ public class JoinListener extends PlayerListener { m_url = url; } - public static void main(String[] args) throws IOException, MalformedURLException { - JoinListener listener = new JoinListener(); - if (listener.isUserAuthed(args[0])) - System.out.println("Yes!"); - else - System.out.println("No!"); - } - public void onPlayerLogin(PlayerLoginEvent event) { Player p = event.getPlayer(); if (p.hasPermission("caminus.whitelisted")) @@ -68,6 +60,8 @@ public class JoinListener extends PlayerListener { 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."); } } @@ -112,12 +106,21 @@ public class JoinListener extends PlayerListener { return ret; } - public boolean isUserAuthed(String user) throws IOException, MalformedURLException { + public boolean isUserAuthed(String user) throws IOException, MalformedURLException, JSONException { URL authServer = new URL(m_url+"validate/"+user); log.info("Authing "+user+" against "+authServer); HttpURLConnection conn = (HttpURLConnection)authServer.openConnection(); - int code = conn.getResponseCode(); - if (code >= 200 && code < 300) + 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(); + JSONObject jsonObj = new JSONObject(jsonStr); + boolean valid = jsonObj.optBoolean("valid"); + if (valid) return true; return false; } diff --git a/src/test/java/us/camin/JoinTest.java b/src/test/java/us/camin/JoinTest.java index 754b1d2..be3b70b 100644 --- a/src/test/java/us/camin/JoinTest.java +++ b/src/test/java/us/camin/JoinTest.java @@ -41,11 +41,11 @@ public class JoinTest { server.stop(); } - @Test public void validUser() throws IOException { + @Test public void validUser() throws IOException, JSONException { assertTrue(listener.isUserAuthed("TestUser")); } - @Test public void invaliduser() throws IOException { + @Test public void invaliduser() throws IOException, JSONException { assertFalse(listener.isUserAuthed("InvalidUser")); }