Search in sources :

Example 1 with AccessTokenRepresentation

use of org.vcell.api.common.AccessTokenRepresentation 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.isInfoEnabled()) {
        lg.info("Executing request to retrieve access_token " + httpget.getRequestLine());
    }
    String responseBody = httpclient.execute(httpget, new VCellStringResponseHandler("authenticate()", httpget));
    String accessTokenJson = responseBody;
    if (lg.isInfoEnabled()) {
        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

Gson (com.google.gson.Gson)1 AuthScope (org.apache.http.auth.AuthScope)1 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)1 AuthCache (org.apache.http.client.AuthCache)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 HttpGet (org.apache.http.client.methods.HttpGet)1 BasicScheme (org.apache.http.impl.auth.BasicScheme)1 BasicAuthCache (org.apache.http.impl.client.BasicAuthCache)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1 AccessTokenRepresentation (org.vcell.api.common.AccessTokenRepresentation)1