DelegatingAuthenticator extends Authenticator { @Override public Result authenticate(HttpExchange pHttpExchange) { try { URLConnection connection = delegateURL.openConnection(); String authorization = pHttpExchange.getRequestHeaders() .getFirst("Authorization"); if(authorization == null){ authorization = pHttpExchange.getRequestHeaders() .getFirst(AuthorizationHeaderParser.JOLOKIA_ALTERNATE_AUTHORIZATION_HEADER); } connection.addRequestProperty("Authorization", authorization); connection.setConnectTimeout(2000); connection.connect(); if (connection instanceof HttpURLConnection) { HttpURLConnection httpConnection = (HttpURLConnection) connection; if (connection instanceof HttpsURLConnection) { HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; if (sslSocketFactory != null) { httpsConnection.setSSLSocketFactory(sslSocketFactory); } if (hostnameVerifier != null) { httpsConnection.setHostnameVerifier(hostnameVerifier); } } return httpConnection.getResponseCode() == 200 ? new Success(principalExtractor.extract(connection)) : new Failure(401); } else { return new Failure(401); } } catch (final IOException e) { return prepareFailure(pHttpExchange, "Cannot call delegate url " + delegateURL + ": " + e, 503); } catch (final IllegalArgumentException e) { return prepareFailure(pHttpExchange, "Illegal Argument: " + e, 400); } catch (ParseException e) { return prepareFailure(pHttpExchange, "Invalid JSON response: " + e, 422); } } DelegatingAuthenticator(String pRealm, String pUrl, String pPrincipalSpec, boolean pDisableCertCheck); @Override Result authenticate(HttpExchange pHttpExchange);  }
@Test public void noAuth() { DelegatingAuthenticator authenticator = new DelegatingAuthenticator("jolokia",url,"json:metadata/name",false); Headers respHeader = new Headers(); HttpExchange ex = createHttpExchange(respHeader); Authenticator.Result result = authenticator.authenticate(ex); assertNotNull(result); assertTrue(result instanceof Authenticator.Failure); assertEquals(((Authenticator.Failure) result).getResponseCode(), 401); }
@Test public void invalidProtocol() { DelegatingAuthenticator authenticator = new DelegatingAuthenticator("jolokia","ftp: Authenticator.Result result = authenticator.authenticate(createHttpExchange(new Headers())); Authenticator.Failure failure = (Authenticator.Failure) result; assertEquals(failure.getResponseCode(),401); }
@Test public void ioException() { String wrongUrl = "http: DelegatingAuthenticator authenticator = new DelegatingAuthenticator("jolokia",wrongUrl,null,false); HttpExchange exchange = createHttpExchange(new Headers()); Authenticator.Result result = authenticator.authenticate(exchange); Authenticator.Failure failure = (Authenticator.Failure) result; assertEquals(failure.getResponseCode(),503); String error = exchange.getResponseHeaders().getFirst("X-Error-Details"); assertTrue(error.contains("http: }
@Test public void invalidPath() { String data[] = new String[] { "json:never/find/me", "never", "json:metadata/name/yet/deeper", "deeper" }; for (int i = 0; i < data.length; i +=2) { DelegatingAuthenticator authenticator = new DelegatingAuthenticator("jolokia", url, data[i], false); HttpExchange exchange = createHttpExchange(new Headers(), "Authorization", "Bearer blub"); Authenticator.Result result = authenticator.authenticate(exchange); Authenticator.Failure failure = (Authenticator.Failure) result; assertEquals(failure.getResponseCode(), 400); String error = exchange.getResponseHeaders().getFirst("X-Error-Details"); assertTrue(error.contains(data[i+1])); } }
@Test public void invalidJson() { DelegatingAuthenticator authenticator = new DelegatingAuthenticator("jolokia", url + "/invalid","json:metadata/name", false); HttpExchange exchange = createHttpExchange(new Headers(), "Authorization", "Bearer blub"); Authenticator.Result result = authenticator.authenticate(exchange); Authenticator.Failure failure = (Authenticator.Failure) result; assertEquals(failure.getResponseCode(), 422); String error = exchange.getResponseHeaders().getFirst("X-Error-Details"); assertTrue(error.contains("Invalid JSON")); }