HmacUtils { public static String createCanonicalRepresentation(ClientRequest clientRequest, Providers providers) { MultivaluedMap<String, Object> originalHeaders = clientRequest.getHeaders(); URI uri = clientRequest.getURI(); String httpMethod = clientRequest.getMethod().toUpperCase(); final StringBuilder canonicalRepresentation = new StringBuilder(""); canonicalRepresentation .append(httpMethod.toUpperCase()) .append("\n"); if (originalHeaders.containsKey(X_HMAC_DATE)) { canonicalRepresentation .append("date:") .append(originalHeaders.getFirst(X_HMAC_DATE)) .append("\n"); } else if (originalHeaders.containsKey(HttpHeaders.DATE)) { canonicalRepresentation .append("date:") .append(originalHeaders.getFirst(HttpHeaders.DATE)) .append("\n"); } if (originalHeaders.containsKey(X_HMAC_NONCE)) { canonicalRepresentation .append("nonce:") .append(originalHeaders.getFirst(X_HMAC_NONCE)) .append("\n"); } else { canonicalRepresentation .append("nonce:") .append("\n"); } canonicalRepresentation.append(uri.toString()); if (isEntityRequired(clientRequest.getEntity(), httpMethod)) { canonicalRepresentation.append("\n"); Object entity = clientRequest.getEntity(); final MessageBodyWriter messageBodyWriter = providers .getMessageBodyWriter(entity.getClass(), entity.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE); final ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (messageBodyWriter == null) { throw new WebApplicationException(Response.Status.BAD_REQUEST); } try { messageBodyWriter.writeTo(entity, entity.getClass(), entity.getClass(), new Annotation[0], MediaType.APPLICATION_JSON_TYPE, originalHeaders, baos); } catch (IOException e) { throw new WebApplicationException(Response.Status.BAD_REQUEST); } canonicalRepresentation.append(baos); } return canonicalRepresentation.toString(); }  static byte[] computeSignature(String algorithm, byte[] data, byte[] sharedSecret); static String createCurlCommand(ClientRequest clientRequest, Providers providers, String canonicalRepresentation, String sharedSecret, String apiKey); static String createCanonicalRepresentation(ClientRequest clientRequest, Providers providers); static String createCanonicalRepresentation(ContainerRequest containerRequest); static final String PREFIX; static final String X_HMAC_NONCE; static final String X_HMAC_DATE; }
@Test public void testCanonicalRepresentation_Client_AllFields_Get() throws Exception { Providers providers = null; OutBoundHeaders headers = new OutBoundHeaders(); headers.add(HttpHeaders.DATE, "Sat, 01 Jan 2000 12:34:56 GMT"); headers.add(HttpHeaders.HOST, "www.example.org"); headers.add(HttpHeaders.USER_AGENT, "curl/7.20.0 (x86_64-pc-linux-gnu) libcurl/7.20.0 OpenSSL/1.0.0a zlib/1.2.3"); headers.add(HmacUtils.X_HMAC_DATE, "Sat, 01 Jan 2000 12:34:57 GMT"); headers.add(HmacUtils.X_HMAC_NONCE, "Thohn2Mohd2zugo"); URI uri = new URI(baseUri + resourcePath + queryParameters); ClientRequest clientRequest = new ClientRequestImpl(uri, "get", null, headers); String representation = HmacUtils.createCanonicalRepresentation(clientRequest, providers); FixtureAsserts.assertStringMatchesStringFixture("GET canonical representation", representation, "/fixtures/hmac/expected-canonical-get.txt"); }
@Test public void testCanonicalRepresentation_Client_AllFields_Post() throws Exception { MessageBodyWriter<String> messageBodyWriter = new StringProvider(); Providers providers = mock(Providers.class); when(providers.getMessageBodyWriter(String.class, String.class, new Annotation[0], MediaType.APPLICATION_JSON_TYPE)).thenReturn(messageBodyWriter); OutBoundHeaders headers = new OutBoundHeaders(); headers.add(HttpHeaders.DATE, "Sat, 01 Jan 2000 12:34:56 GMT"); headers.add(HttpHeaders.HOST, "www.example.org"); headers.add(HttpHeaders.USER_AGENT, "curl/7.20.0 (x86_64-pc-linux-gnu) libcurl/7.20.0 OpenSSL/1.0.0a zlib/1.2.3"); headers.add(HmacUtils.X_HMAC_DATE, "Sat, 01 Jan 2000 12:34:57 GMT"); headers.add(HmacUtils.X_HMAC_NONCE, "Thohn2Mohd2zugo"); URI uri = new URI(baseUri + resourcePath + queryParameters); String entity = "{\"_links\":{\"self\":{\"href\":\"http: ClientRequest clientRequest = new ClientRequestImpl(uri, "post", entity, headers); String representation = HmacUtils.createCanonicalRepresentation(clientRequest, providers); FixtureAsserts.assertStringMatchesStringFixture("POST canonical representation", representation, "/fixtures/hmac/expected-canonical-post.txt"); }
@Test public void testCanonicalRepresentation_Server_AllFields_Get() throws Exception { InBoundHeaders headers = new InBoundHeaders(); headers.add(HttpHeaders.DATE, "Sat, 01 Jan 2000 12:34:56 GMT"); headers.add(HttpHeaders.HOST, "www.example.org"); headers.add(HttpHeaders.USER_AGENT, "curl/7.20.0 (x86_64-pc-linux-gnu) libcurl/7.20.0 OpenSSL/1.0.0a zlib/1.2.3"); headers.add(HmacUtils.X_HMAC_DATE, "Sat, 01 Jan 2000 12:34:57 GMT"); headers.add(HmacUtils.X_HMAC_NONCE, "Thohn2Mohd2zugo"); ContainerRequest containerRequest = mock(ContainerRequest.class); when(containerRequest.getRequestHeaders()).thenReturn(headers); when(containerRequest.getMethod()).thenReturn("GET"); when(containerRequest.getRequestUri()).thenReturn(URI.create(baseUri + resourcePath + queryParameters)); String representation = HmacUtils.createCanonicalRepresentation(containerRequest); FixtureAsserts.assertStringMatchesStringFixture("GET all fields canonical representation", representation, "/fixtures/hmac/expected-canonical-get.txt"); }
@Test public void testCanonicalRepresentation_Server_AllFields_Post() throws Exception { InBoundHeaders headers = new InBoundHeaders(); headers.add(HttpHeaders.DATE, "Sat, 01 Jan 2000 12:34:56 GMT"); headers.add(HttpHeaders.HOST, "www.example.org"); headers.add(HttpHeaders.USER_AGENT, "curl/7.20.0 (x86_64-pc-linux-gnu) libcurl/7.20.0 OpenSSL/1.0.0a zlib/1.2.3"); headers.add(HmacUtils.X_HMAC_DATE, "Sat, 01 Jan 2000 12:34:57 GMT"); headers.add(HmacUtils.X_HMAC_NONCE, "Thohn2Mohd2zugo"); ContainerRequest containerRequest = mock(ContainerRequest.class); when(containerRequest.getRequestHeaders()).thenReturn(headers); when(containerRequest.getMethod()).thenReturn("POST"); when(containerRequest.getRequestUri()).thenReturn(URI.create(baseUri + resourcePath + queryParameters)); ByteArrayInputStream bais = new ByteArrayInputStream("{\"_links\":{\"self\":{\"href\":\"http: when(containerRequest.getEntityInputStream()).thenReturn(bais); String actualCanonicalRepresentation = HmacUtils.createCanonicalRepresentation(containerRequest); FixtureAsserts.assertStringMatchesStringFixture("POST all fields canonical representation", actualCanonicalRepresentation, "/fixtures/hmac/expected-canonical-post.txt"); }