Skip to content

Instantly share code, notes, and snippets.

@garystafford
Created February 17, 2016 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garystafford/357acd4a3540d829b628 to your computer and use it in GitHub Desktop.
Save garystafford/357acd4a3540d829b628 to your computer and use it in GitHub Desktop.
public boolean authenticateJwt(Request request, String baseUrlAndAuthPort) {
String jwt, output, valid = "";
try {
jwt = (request.getHeader("Authorization").split(" "))[1];
} catch (NullPointerException | ArrayIndexOutOfBoundsException e) {
return false;
}
try {
URL url = new URL(baseUrlAndAuthPort + "/jwts/" + jwt);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "application/json");
if (conn.getResponseCode() != 200) {
return false;
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
while ((output = br.readLine()) != null) {
valid = output;
}
conn.disconnect();
} catch (IOException e) {
return false;
}
return Boolean.parseBoolean(valid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment