@Test public void testToDataToSign() { DefaultAuthenticator authenticator = new DefaultAuthenticator(AuthorizationType.V1HMAC, "apiKeyId", "secretApiKey"); List<RequestHeader> httpHeaders = new ArrayList<RequestHeader>(); httpHeaders.add( new RequestHeader( "X-GCS-ServerMetaInfo", "{\"platformIdentifier\":\"Windows 7/6.1 Java/1.7 (Oracle Corporation; Java HotSpot(TM) 64-Bit Server VM; 1.7.0_45)\",\"sdkIdentifier\":\"1.0\"}")); httpHeaders.add(new RequestHeader("Content-Type", "application/json")); httpHeaders.add(new RequestHeader("X-GCS-ClientMetaInfo", "{\"aap\",\"noot\"}")); httpHeaders.add(new RequestHeader("User-Agent", "Apache-HttpClient/4.3.4 (java 1.5)")); httpHeaders.add(new RequestHeader("Date", "Mon, 07 Jul 2014 12:12:40 GMT")); String dataToSign = authenticator.toDataToSign( "POST", URI.create( "http://localhost:8080/v1/9991/services%20bla/convert/amount?aap=noot&mies=geen%20noot"), httpHeaders); String expectedStart = "POST\n" + "application/json\n"; String expectedEnd = "x-gcs-clientmetainfo:{\"aap\",\"noot\"}\n" + "x-gcs-servermetainfo:{\"platformIdentifier\":\"Windows 7/6.1 Java/1.7 (Oracle Corporation; Java HotSpot(TM) 64-Bit Server VM; 1.7.0_45)\",\"sdkIdentifier\":\"1.0\"}\n" + "/v1/9991/services%20bla/convert/amount?aap=noot&mies=geen noot\n"; String actualStart = dataToSign.substring(0, 22); String actualEnd = dataToSign.substring(52, 308); Assert.assertEquals(expectedStart, actualStart); Assert.assertEquals(expectedEnd, actualEnd); }
@Test public void testToCanonicalizeHeaderValue() { DefaultAuthenticator authenticator = new DefaultAuthenticator(AuthorizationType.V1HMAC, "apiKeyId", "secretApiKey"); Assert.assertEquals("aap noot", authenticator.toCanonicalizeHeaderValue("aap\nnoot ")); Assert.assertEquals("aap noot", authenticator.toCanonicalizeHeaderValue(" aap\r\n noot")); }
@Test public void testCreateAuthenticationSignature2() { DefaultAuthenticator authenticator = new DefaultAuthenticator( AuthorizationType.V1HMAC, "EC36A74A98D21", "6Kj5HT0MQKC6D8eb7W3lTg71kVKVDSt1"); String dataToSign = "GET\n" + "\n" + "Fri, 06 Jun 2014 13:39:43 GMT\n" + "/v1/9991/tokens/123456789\n"; String authenticationSignature = authenticator.createAuthenticationSignature(dataToSign); Assert.assertEquals("9ond5EIN05dBXJGCLRK5om9pxHsyrh/12pZJ7bvmwNM=", authenticationSignature); }
/** * Gets the credentials1. * * @return the credentials1 * @throws Exception the exception */ @Test public void getCredentials1() throws Exception { WebSocket websocket = Mockito.mock(WebSocket.class); DigestAuthenticator digestAuthenticator = new DigestAuthenticator(); digestAuthenticator = PowerMockito.spy(digestAuthenticator); PowerMockito.when(digestAuthenticator, "generateCnonce").thenReturn("0a4f113b"); DefaultAuthenticator authenticator = new DefaultAuthenticator(new BasicAuthenticator(), digestAuthenticator, null); authenticator.init(websocket, new Credentials("Mufasa", "Circle Of Life")); HttpHeader header = new HttpHeader(); header.addHeader("Proxy-Authenticate", "Basic realm=\"[email protected]\""); Assert.assertEquals( "Basic TXVmYXNhOkNpcmNsZSBPZiBMaWZl", authenticator.getCredentials("CONNECT", "host:8080", header, "Proxy-Authenticate")); }
@Test public void testCreateAuthenticationSignature() { DefaultAuthenticator authenticator = new DefaultAuthenticator(AuthorizationType.V1HMAC, "apiKeyId", "secretApiKey"); String dataToSign = "DELETE\n" + "application/json\n" + "Fri, 06 Jun 2014 13:39:43 GMT\n" + "x-gcs-clientmetainfo:processed header value\n" + "x-gcs-customerheader:processed header value\n" + "x-gcs-servermetainfo:processed header value\n" + "/v1/9991/tokens/123456789\n"; String authenticationSignature = authenticator.createAuthenticationSignature(dataToSign); Assert.assertEquals("VfnXpPBQQoHZivTcAg0JvOWkhnzlPnaCPKpTQn/uMJM=", authenticationSignature); }
/** * Gets the credentials2. * * @return the credentials2 * @throws Exception the exception */ @Test public void getCredentials2() throws Exception { WebSocket websocket = Mockito.mock(WebSocket.class); DigestAuthenticator digestAuthenticator = new DigestAuthenticator(); digestAuthenticator = PowerMockito.spy(digestAuthenticator); PowerMockito.when(digestAuthenticator, "generateCnonce").thenReturn("0a4f113b"); DefaultAuthenticator authenticator = new DefaultAuthenticator(new BasicAuthenticator(), digestAuthenticator, null); authenticator.init(websocket, new Credentials("Mufasa", "Circle Of Life")); HttpHeader header = new HttpHeader(); header.addHeader("Proxy-Authenticate", "Basic realm=\"[email protected]\""); header.addHeader( "Proxy-Authenticate", "Digest realm=\"[email protected]\", qop=\"auth,auth-int\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\""); Assert.assertEquals( "Digest username=\"Mufasa\", realm=\"[email protected]\", nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", uri=\"/dir/index.html\", qop=auth, nc=00000001, cnonce=\"0a4f113b\", opaque=\"5ccc069c403ebaf9f0171e9517f40e41\", response=\"6629fae49393a05397450978507c4ef1\"", authenticator.getCredentials("GET", "/dir/index.html", header, "Proxy-Authenticate")); }