use of org.infinispan.server.test.core.InfinispanServerDriver in project infinispan by infinispan.
the class RestAuthentication method testStaticResourcesAnonymously.
@Test
public void testStaticResourcesAnonymously() {
InfinispanServerDriver serverDriver = SERVERS.getServerDriver();
InetSocketAddress serverAddress = serverDriver.getServerSocket(0, 11222);
RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder().followRedirects(false);
builder.addServer().host(serverAddress.getHostName()).port(serverAddress.getPort());
RestClient restClient = RestClient.forConfiguration(builder.build());
RestResponse response = sync(restClient.raw().get("/"));
// The root resource redirects to the console
assertEquals(307, response.getStatus());
}
use of org.infinispan.server.test.core.InfinispanServerDriver in project infinispan by infinispan.
the class RestAuthentication method testMalformedDigestHeader.
@Test
public void testMalformedDigestHeader() throws Exception {
assumeTrue(mechanism.startsWith("DIGEST"));
InfinispanServerDriver serverDriver = SERVERS.getServerDriver();
InetSocketAddress serverAddress = serverDriver.getServerSocket(0, 11222);
RestClientConfigurationBuilder builder = new RestClientConfigurationBuilder().followRedirects(false);
builder.addServer().host(serverAddress.getHostName()).port(serverAddress.getPort());
RestClient restClient = RestClient.forConfiguration(builder.build());
RestResponse response = sync(restClient.raw().get("/rest/v2/caches"));
assertEquals(401, response.getStatus());
String auth = response.headers().get("Www-Authenticate").stream().filter(h -> h.startsWith("Digest")).findFirst().get();
HashMap<String, byte[]> parameters = DigestUtil.parseResponse(auth.substring(7).getBytes(UTF_8), UTF_8, false, httpDigest);
final String realm = new String(parameters.get("realm"), UTF_8);
final String nonce = new String(parameters.get("nonce"), UTF_8);
final String opaque = new String(parameters.get("opaque"), UTF_8);
final String algorithm = new String(parameters.get("algorithm"), UTF_8);
final String charset = StandardCharsets.ISO_8859_1.name();
final MessageDigest digester = MessageDigest.getInstance(algorithm);
final String nc = "00000001";
final String cnonce = "00000000";
final String username = "h4ck0rz";
final String password = "letmein";
final String uri = "/backdoor";
final String s1 = username + ':' + realm + ':' + password;
final String s2 = "GET:" + uri;
final String hasha1 = toHexString(digester.digest(s1.getBytes(charset)));
final String h2 = toHexString(digester.digest(s2.getBytes(charset)));
final String digestValue = hasha1 + ':' + nonce + ':' + nc + ':' + cnonce + ":auth:" + h2;
final String digest = toHexString(digester.digest(digestValue.getBytes(StandardCharsets.US_ASCII.toString())));
String authz = String.format("Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\", qop=auth, nc=%s, cnonce=%s, algorithm=%s, opaque=\"%s\"", username, realm, nonce, uri, digest, nc, cnonce, algorithm, opaque);
response = sync(restClient.raw().get("/rest/v2/caches", Collections.singletonMap("Authorization", authz)));
assertEquals(400, response.getStatus());
}
Aggregations