public HttpResponse execute(HttpUriRequest req) throws IOException, ClientProtocolException { String digest = req.getLastHeader("Digest").getValue(); String ticket = TICKET; String userId = USER; HttpResponse response = new DefaultHttpResponseFactory() .newHttpResponse(new ProtocolVersion("1.1", 1, 1), 200, null); // validate the ticket try { String checkdigest = LocalDigestValidator.SHA(SHARED_SECRET + ticket, ENCODING, HASH_FUNCTION); if (checkdigest.equals(digest)) { String responsedigest = LocalDigestValidator.SHA(SHARED_SECRET + ticket + userId, ENCODING, HASH_FUNCTION); response.setEntity( new StringEntity(successResponse.replace("${digest}", responsedigest))); } else { // return error response.setEntity(new StringEntity(failResponse)); } } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } return response; }
@Test public void serviceReturnsDigest() throws Exception { String digest = LocalDigestValidator.SHA(SHARED_SECRET + TICKET, ENCODING, HASH_FUNCTION); String result = client.validateDigest(digest, TICKET, "myserviceurl", USER); String responsedigest = LocalDigestValidator.SHA(SHARED_SECRET + TICKET + USER, ENCODING, HASH_FUNCTION); assertEquals(responsedigest, result); }