/** * Helper method that injects an authentication token to send with the method. * @param method method to inject the authentication token into. * @param token authentication token to inject. */ private void injectToken(HttpUriRequest method, AuthenticatedURL.Token token) { String t = token.toString(); if (t != null) { if (!t.startsWith("\"")) { t = "\"" + t + "\""; } method.addHeader(COOKIE, AUTH_COOKIE_EQ + t); } }
private static long getExpirationTime(AuthenticatedURL.Token token) { long expires = 0L; String[] splits = token.toString().split("&"); for (String split : splits) { if (split.startsWith("e=")) { try { expires = Long.parseLong(split.substring(2)); } catch (Exception e) { // token is somehow invalid, assume it expired already break; } } } return expires; }
private static long getExpirationTime(AuthenticatedURL.Token token) { long expires = 0L; String[] splits = token.toString().split("&"); for (String split : splits) { if (split.startsWith("e=")) { try { expires = Long.parseLong(split.substring(2)); } catch (Exception e) { // token is somehow invalid, assume it expired already break; } } } return expires; }
HttpURLConnection conn = aUrl.openConnection(url, token); Assert.assertTrue(connConf.invoked); String tokenStr = token.toString(); if (doPost) { conn.setRequestMethod("POST"); conn.connect(); Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); Assert.assertEquals(tokenStr, token.toString()); } finally { stop();
HttpURLConnection conn = aUrl.openConnection(url, token); Assert.assertTrue(connConf.invoked); String tokenStr = token.toString(); if (doPost) { conn.setRequestMethod("POST"); conn.connect(); Assert.assertEquals(HttpURLConnection.HTTP_OK, conn.getResponseCode()); Assert.assertEquals(tokenStr, token.toString()); } finally { stop();
writer.write(authToken.toString()); writer.close(); Files.move(tmpTokenFile.toPath(), AUTH_TOKEN_CACHE_FILE.toPath(), StandardCopyOption.ATOMIC_MOVE);
writer.write(authToken.toString()); writer.close(); Files.move(tmpTokenFile.toPath(), authTokenCacheFile.toPath(), StandardCopyOption.ATOMIC_MOVE);
@Test public void testExtractTokenOK() throws Exception { HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK); String tokenStr = "foo"; Map<String, List<String>> headers = new HashMap<String, List<String>>(); List<String> cookies = new ArrayList<String>(); cookies.add(AuthenticatedURL.AUTH_COOKIE + "=" + tokenStr); headers.put("Set-Cookie", cookies); Mockito.when(conn.getHeaderFields()).thenReturn(headers); AuthenticatedURL.Token token = new AuthenticatedURL.Token(); AuthenticatedURL.extractToken(conn, token); Assert.assertEquals(tokenStr, token.toString()); }
@Test public void testExtractTokenOK() throws Exception { HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK); String tokenStr = "foo"; Map<String, List<String>> headers = new HashMap<String, List<String>>(); List<String> cookies = new ArrayList<String>(); cookies.add(AuthenticatedURL.AUTH_COOKIE + "=" + tokenStr); headers.put("Set-Cookie", cookies); Mockito.when(conn.getHeaderFields()).thenReturn(headers); AuthenticatedURL.Token token = new AuthenticatedURL.Token(); AuthenticatedURL.extractToken(conn, token); Assert.assertEquals(tokenStr, token.toString()); }
/** * Helper method that injects an authentication token to send with the method. * @param method method to inject the authentication token into. * @param token authentication token to inject. */ private void injectToken(HttpUriRequest method, AuthenticatedURL.Token token) { String t = token.toString(); if (t != null) { if (!t.startsWith("\"")) { t = "\"" + t + "\""; } method.addHeader(COOKIE, AUTH_COOKIE_EQ + t); } }
@Test public void testExtractTokenOK() throws Exception { HttpURLConnection conn = Mockito.mock(HttpURLConnection.class); Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_OK); String tokenStr = "foo"; Map<String, List<String>> headers = new HashMap<String, List<String>>(); List<String> cookies = new ArrayList<String>(); cookies.add(AuthenticatedURL.AUTH_COOKIE + "=" + tokenStr); headers.put("Set-Cookie", cookies); Mockito.when(conn.getHeaderFields()).thenReturn(headers); AuthenticatedURL.Token token = new AuthenticatedURL.Token(); AuthenticatedURL.extractToken(conn, token); Assert.assertEquals(tokenStr, token.toString()); }
@Test public void testToken() throws Exception { AuthenticatedURL.Token token = new AuthenticatedURL.Token(); Assert.assertFalse(token.isSet()); token = new AuthenticatedURL.Token("foo"); Assert.assertTrue(token.isSet()); Assert.assertEquals("foo", token.toString()); }
@Test public void testToken() throws Exception { AuthenticatedURL.Token token = new AuthenticatedURL.Token(); Assert.assertFalse(token.isSet()); token = new AuthenticatedURL.Token("foo"); Assert.assertTrue(token.isSet()); Assert.assertEquals("foo", token.toString()); }
private boolean isTokenKerberos(AuthenticatedURL.Token token) throws AuthenticationException { if (token.isSet()) { AuthToken aToken = AuthToken.parse(token.toString()); if (aToken.getType().equals("kerberos") || aToken.getType().equals("kerberos-dt")) { return true; } } return false; }
@Test public void testToken() throws Exception { AuthenticatedURL.Token token = new AuthenticatedURL.Token(); Assert.assertFalse(token.isSet()); token = new AuthenticatedURL.Token("foo"); Assert.assertTrue(token.isSet()); Assert.assertEquals("foo", token.toString()); }
private boolean isTokenKerberos(AuthenticatedURL.Token token) throws AuthenticationException { if (token.isSet()) { AuthToken aToken = AuthToken.parse(token.toString()); if (aToken.getType().equals("kerberos") || aToken.getType().equals("kerberos-dt")) { return true; } } return false; }
private boolean isTokenKerberos(AuthenticatedURL.Token token) throws AuthenticationException { if (token.isSet()) { AuthToken aToken = AuthToken.parse(token.toString()); if (aToken.getType().equals("kerberos") || aToken.getType().equals("kerberos-dt")) { return true; } } return false; }
URL url = new URL("http://youhost:8080/your-kerberised-resource"); AuthenticatedURL.Token token = new AuthenticatedURL.Token(); HttpURLConnection conn = new AuthenticatedURL().openConnection(url, token); String authorizationTokenString = conn.getRequestProperty("Authorization"); String delegationToken = conn.getRequestProperty("X-Hadoop-Delegation-Token"); ... // do what you have to to get your basic client connection ... myBasicClientConnection.setRequestProperty("Authorization", authorizationTokenString); myBasicClientConnection.setRequestProperty("Cookie", "hadoop.auth=" + token.toString()); myBasicClientConnection.setRequestProperty("X-Hadoop-Delegation-Token", delegationToken);
private boolean isTokenKerberos(AuthenticatedURL.Token token) throws AuthenticationException { if (token.isSet()) { AuthToken aToken = AuthToken.parse(token.toString()); if (aToken.getType().equals("kerberos") || aToken.getType().equals("kerberos-dt")) { return true; } } return false; }
private boolean isTokenKerberos(AuthenticatedURL.Token token) throws AuthenticationException { if (token.isSet()) { AuthToken aToken = AuthToken.parse(token.toString()); if (aToken.getType().equals("kerberos") || aToken.getType().equals("kerberos-dt")) { return true; } } return false; }