@Override public Boolean call() throws Exception { return authClient.getAccessToken() != null; } }, "Unable to connect to Authentication service to obtain access token, Connection info : "
/** * Constructs new instance. */ public BasicAuthenticationClient() { super(); credentials = ImmutableList.of(new Credential(USERNAME_PROP_NAME, "Username for basic authentication.", false), new Credential(PASSWORD_PROP_NAME, "Password for basic authentication.", true)); }
private Map<String, String> getAuthHeaders(AccessToken accessToken) { Map<String, String> headers = ImmutableMap.of(); if (accessToken != null) { headers = ImmutableMap.of(HttpHeaders.AUTHORIZATION, accessToken.getTokenType() + " " + accessToken.getValue()); } return headers; }
for (Credential credential : authenticationClient.getRequiredCredentials()) { String prompt = "Please, specify " + credential.getDescription() + "> "; String credentialValue; if (credential.isSecret()) { credentialValue = reader.readLine(prompt, '*'); } else { credentialValue = reader.readLine(prompt); properties.put(credential.getName(), credentialValue); if (credential.getName().contains("username")) { username = credentialValue; authenticationClient.configure(properties); AccessToken accessToken = authenticationClient.getAccessToken(); UserAccessToken userToken = new UserAccessToken(accessToken, username); if (accessToken != null) {
protected AccessToken fetchAccessToken(String username, String password) throws IOException, TimeoutException, InterruptedException { Properties properties = new Properties(); properties.setProperty("security.auth.client.username", username); properties.setProperty("security.auth.client.password", password); properties.setProperty("security.auth.client.verify.ssl.cert", Boolean.toString(getClientConfig().isVerifySSLCert())); final AuthenticationClient authClient = new BasicAuthenticationClient(); authClient.configure(properties); ConnectionConfig connectionConfig = getClientConfig().getConnectionConfig(); authClient.setConnectionInfo(connectionConfig.getHostname(), connectionConfig.getPort(), connectionConfig.isSSLEnabled()); checkServicesWithRetry(new Callable<Boolean>() { @Override public Boolean call() throws Exception { return authClient.getAccessToken() != null; } }, "Unable to connect to Authentication service to obtain access token, Connection info : " + connectionConfig); return authClient.getAccessToken(); }
private AuthenticationClient getAuthenticationClient(ConnectionConfig connectionInfo) { AuthenticationClient authenticationClient = new BasicAuthenticationClient(); authenticationClient.setConnectionInfo(connectionInfo.getHostname(), connectionInfo.getPort(), connectionInfo.isSSLEnabled()); return authenticationClient; }
@Override public AccessToken getAccessToken() throws IOException { if (!isAuthEnabled()) { return null; } if (accessToken == null || isTokenExpired()) { long requestTime = System.currentTimeMillis(); accessToken = fetchAccessToken(); expirationTime = requestTime + TimeUnit.SECONDS.toMillis(accessToken.getExpiresIn()) - SPARE_TIME_IN_MILLIS; LOG.debug("Received the access token successfully. Expiration date is {}.", new Date(expirationTime)); } return accessToken; }
private AccessToken fetchAccessToken() throws IOException { LOG.debug("Authentication is enabled in the gateway server. Authentication URI {}.", getAuthURI()); return execute(HttpRequest.get(getAuthURI().toURL()) .addHeaders(getAuthenticationHeaders()) .build() ); }
@Override public String get() { if (config.getAccessToken() != null) { return config.getAccessToken().getValue(); } return null; } };
/** * Executes fetch access token request. * * @param request the http request to fetch access token from the authentication server * @return {@link AccessToken} object containing the access token * @throws IOException IOException in case of a problem or the connection was aborted or if the access token is not * received successfully from the authentication server */ private AccessToken execute(HttpRequest request) throws IOException { HttpResponse response = HttpRequests.execute(request, getHttpRequestConfig()); LOG.debug("Got response {} - {} from {}", response.getResponseCode(), response.getResponseMessage(), pingURI); if (response.getResponseCode() != HttpURLConnection.HTTP_OK) { throw new HttpFailureException(response.getResponseMessage(), response.getResponseCode()); } Map<String, String> responseMap = ObjectResponse.fromJsonBody(response, ACCESS_TOKEN_RESPONSE_TYPE_TOKEN).getResponseObject(); String tokenValue = responseMap.get(ACCESS_TOKEN_KEY); String tokenType = responseMap.get(TOKEN_TYPE_KEY); String expiresInStr = responseMap.get(EXPIRES_IN_KEY); LOG.debug("Response map from auth server: {}", responseMap); if (StringUtils.isEmpty(tokenValue) || StringUtils.isEmpty(tokenType) || StringUtils.isEmpty(expiresInStr)) { throw new IOException("Unexpected response was received from the authentication server."); } return new AccessToken(tokenValue, Long.valueOf(expiresInStr), tokenType); }
private boolean isAuthenticationEnabled(ConnectionConfig connectionInfo) throws IOException { return getAuthenticationClient(connectionInfo).isAuthEnabled(); }
@Override public AccessToken get() { try { return getAccessToken(); } catch (IOException e) { throw Throwables.propagate(e); } }
@Override public boolean isAuthEnabled() throws IOException { if (authEnabled == null) { String strAuthURI = fetchAuthURI(); authEnabled = StringUtils.isNotEmpty(strAuthURI); if (authEnabled) { authURI = URI.create(strAuthURI); } } return authEnabled; }
private HttpRequestConfig getHttpRequestConfig() { return new HttpRequestConfig(0, 0, isVerifySSLCert()); } }
/** * Fetches the available authentication server URL, if authentication is enabled in the gateway server, * otherwise, empty string will be returned. * * @return string value of the authentication server URL * @throws IOException IOException in case of a problem or the connection was aborted or if url list is empty */ private String fetchAuthURI() throws IOException { if (pingURI == null) { throw new IllegalStateException("Connection information not set!"); } LOG.debug("Try to get the authentication URI from the gateway server: {}.", pingURI); HttpResponse response = HttpRequests.execute(HttpRequest.get(pingURI.toURL()).build(), getHttpRequestConfig()); LOG.debug("Got response {} - {} from {}", response.getResponseCode(), response.getResponseMessage(), pingURI); if (response.getResponseCode() != HttpURLConnection.HTTP_UNAUTHORIZED) { return ""; } Map<String, List<String>> responseMap = ObjectResponse.fromJsonBody(response, AUTH_URL_RESPONSE_TYPE_TOKEN).getResponseObject(); LOG.debug("Response map from gateway server: {}", responseMap); String result; List<String> uriList = responseMap.get(AUTH_URI_KEY); if (uriList != null && !uriList.isEmpty()) { result = uriList.get(RANDOM.nextInt(uriList.size())); } else { throw new IOException("Authentication servers list is empty."); } return result; }
for (Credential credential : authenticationClient.getRequiredCredentials()) { String prompt = "Please, specify " + credential.getDescription() + "> "; String credentialValue; if (credential.isSecret()) { credentialValue = reader.readLine(prompt, '*'); } else { credentialValue = reader.readLine(prompt); properties.put(credential.getName(), credentialValue); if (credential.getName().contains("username")) { username = credentialValue; authenticationClient.configure(properties); AccessToken accessToken = authenticationClient.getAccessToken(); UserAccessToken userToken = new UserAccessToken(accessToken, username); if (accessToken != null) {
private AuthenticationClient getAuthenticationClient(ConnectionConfig connectionInfo) { AuthenticationClient authenticationClient = new BasicAuthenticationClient(); authenticationClient.setConnectionInfo(connectionInfo.getHostname(), connectionInfo.getPort(), connectionInfo.isSSLEnabled()); return authenticationClient; }
@Override public Connection getQueryClient(NamespaceId namespace) throws Exception { Map<String, String> connParams = new HashMap<>(); connParams.put(ExploreConnectionParams.Info.NAMESPACE.getName(), namespace.getNamespace()); AccessToken accessToken = clientConfig.getAccessToken(); if (accessToken != null) { connParams.put(ExploreConnectionParams.Info.EXPLORE_AUTH_TOKEN.getName(), accessToken.getValue()); } connParams.put(ExploreConnectionParams.Info.SSL_ENABLED.getName(), Boolean.toString(clientConfig.getConnectionConfig().isSSLEnabled())); connParams.put(ExploreConnectionParams.Info.VERIFY_SSL_CERT.getName(), Boolean.toString(clientConfig.isVerifySSLCert())); ConnectionConfig connConfig = clientConfig.getConnectionConfig(); String url = String.format("%s%s:%d?%s", Constants.Explore.Jdbc.URL_PREFIX, connConfig.getHostname(), connConfig.getPort(), Joiner.on("&").withKeyValueSeparator("=").join(connParams)); return new ExploreDriver().connect(url, new Properties()); }
private boolean isAuthenticationEnabled(ConnectionConfig connectionInfo) throws IOException { return getAuthenticationClient(connectionInfo).isAuthEnabled(); }