Search in sources :

Example 16 with AuthCache

use of org.apache.http.client.AuthCache in project vcell by virtualcell.

the class VCellApiClient method authenticate.

public AccessTokenRepresentation authenticate(String userid, String password, boolean alreadyDigested) throws ClientProtocolException, IOException {
    // hash the password
    String digestedPassword = (alreadyDigested) ? (password) : createdDigestPassword(password);
    HttpGet httpget = new HttpGet("https://" + httpHost.getHostName() + ":" + httpHost.getPort() + "/access_token?user_id=" + userid + "&user_password=" + digestedPassword + "&client_id=" + clientID);
    if (lg.isLoggable(Level.INFO)) {
        lg.info("Executing request to retrieve access_token " + httpget.getRequestLine());
    }
    String responseBody = httpclient.execute(httpget, responseHandler);
    String accessTokenJson = responseBody;
    if (lg.isLoggable(Level.INFO)) {
        lg.info("returned: " + accessTokenJson);
    }
    Gson gson = new Gson();
    AccessTokenRepresentation accessTokenRep = gson.fromJson(accessTokenJson, AccessTokenRepresentation.class);
    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials(new AuthScope(httpHost.getHostName(), httpHost.getPort()), new UsernamePasswordCredentials("access_token", accessTokenRep.getToken()));
    // Create AuthCache instance
    AuthCache authCache = new BasicAuthCache();
    // Generate BASIC scheme object and add it to the local auth cache
    BasicScheme basicAuth = new BasicScheme();
    authCache.put(httpHost, basicAuth);
    // Add AuthCache to the execution context
    httpClientContext = HttpClientContext.create();
    httpClientContext.setCredentialsProvider(credsProvider);
    httpClientContext.setAuthCache(authCache);
    return accessTokenRep;
}
Also used : BasicScheme(org.apache.http.impl.auth.BasicScheme) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpGet(org.apache.http.client.methods.HttpGet) AuthScope(org.apache.http.auth.AuthScope) AuthCache(org.apache.http.client.AuthCache) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) Gson(com.google.gson.Gson) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) BasicAuthCache(org.apache.http.impl.client.BasicAuthCache) AccessTokenRepresentation(org.vcell.api.common.AccessTokenRepresentation) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Aggregations

AuthCache (org.apache.http.client.AuthCache)16 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)16 BasicScheme (org.apache.http.impl.auth.BasicScheme)15 HttpHost (org.apache.http.HttpHost)12 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)12 AuthScope (org.apache.http.auth.AuthScope)10 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)10 CredentialsProvider (org.apache.http.client.CredentialsProvider)10 HttpClientContext (org.apache.http.client.protocol.HttpClientContext)10 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)5 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)5 IOException (java.io.IOException)3 HttpGet (org.apache.http.client.methods.HttpGet)3 URI (java.net.URI)2 GssClient (com.axway.ats.core.gss.GssClient)1 SPNegoSchemeFactory (com.axway.ats.core.gss.spnego.SPNegoSchemeFactory)1 Gson (com.google.gson.Gson)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 MalformedURLException (java.net.MalformedURLException)1 URISyntaxException (java.net.URISyntaxException)1