Implement new validation api

This commit is contained in:
Trever Fischer 2012-03-05 19:19:13 -05:00
parent 47ab875a7a
commit 906871dfc2
2 changed files with 16 additions and 13 deletions

View File

@ -48,14 +48,6 @@ public class JoinListener extends PlayerListener {
m_url = url; 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) { public void onPlayerLogin(PlayerLoginEvent event) {
Player p = event.getPlayer(); Player p = event.getPlayer();
if (p.hasPermission("caminus.whitelisted")) if (p.hasPermission("caminus.whitelisted"))
@ -68,6 +60,8 @@ public class JoinListener extends PlayerListener {
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Auth URL is invalid!"); event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Auth URL is invalid!");
} catch (IOException e) { } catch (IOException e) {
event.disallow(PlayerLoginEvent.Result.KICK_WHITELIST, "Camin.us auth server seems down."); 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; 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); URL authServer = new URL(m_url+"validate/"+user);
log.info("Authing "+user+" against "+authServer); log.info("Authing "+user+" against "+authServer);
HttpURLConnection conn = (HttpURLConnection)authServer.openConnection(); HttpURLConnection conn = (HttpURLConnection)authServer.openConnection();
int code = conn.getResponseCode(); BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
if (code >= 200 && code < 300) 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 true;
return false; return false;
} }

View File

@ -41,11 +41,11 @@ public class JoinTest {
server.stop(); server.stop();
} }
@Test public void validUser() throws IOException { @Test public void validUser() throws IOException, JSONException {
assertTrue(listener.isUserAuthed("TestUser")); assertTrue(listener.isUserAuthed("TestUser"));
} }
@Test public void invaliduser() throws IOException { @Test public void invaliduser() throws IOException, JSONException {
assertFalse(listener.isUserAuthed("InvalidUser")); assertFalse(listener.isUserAuthed("InvalidUser"));
} }