Skip to content

Instantly share code, notes, and snippets.

@nathancyam
Created June 16, 2013 06:47
Show Gist options
  • Save nathancyam/5791125 to your computer and use it in GitHub Desktop.
Save nathancyam/5791125 to your computer and use it in GitHub Desktop.
Auth Http Handler
public class AuthHttpHandler {
private String URI_GET_AUTH = "http://melfridinbot.appspot.com/mobile/checkauth";
protected void authRequest(String mobilePassword, String userEmail) throws NoSuchAlgorithmException, IOException {
HttpClient client = new DefaultHttpClient();
HttpGet getRequest = new HttpGet(URI_GET_AUTH);
getRequest.addHeader("token", getMD5(mobilePassword));
getRequest.addHeader("tokenDate", getDate());
getRequest.addHeader("email", userEmail);
HttpResponse response = null;
response = client.execute(getRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode());
}
try {
InputStream inputStream = response.getEntity().getContent();
} catch (IOException e) {
e.printStackTrace();
}
client.getConnectionManager().shutdown();
}
public String getMD5(String mobilePassword) throws NoSuchAlgorithmException {
MessageDigest md = MessageDigest.getInstance("MD5");
String fullHash = mobilePassword + getDate();
md.update(fullHash.getBytes());
byte byteData[] = md.digest();
StringBuffer hexString = new StringBuffer();
for (int i=0;i<byteData.length;i++) {
String hex=Integer.toHexString(0xff & byteData[i]);
if(hex.length()==1) hexString.append('0');
hexString.append(hex);
}
return hexString.toString();
}
private String getDate(){
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
format.setTimeZone(TimeZone.getTimeZone("UTC"));
Date date = new Date();
return format.format(date);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment