public final class SampleAuthorizationClient extends Object
Sample code to get an access token using AuthorizationClientHelper.
Use AuthorizationClientHelper.getAccessToken() to obtain an AccessToken for a client. (maps to OAuth2.0 Client Credentials grant type)
Use AuthorizationClientHelper.getAccessTokenForUser(httpServletRequest) to obtain an AccessToken for a user. (maps to OAuth2.0 Authorization Code grant type)
Use AuthorizationClientHelper.getAccessTokenForUser(userName, userPass) to obtain an AccessToken for a user. (maps to OAuth2.0 ROPC grant type)
AccessToken.toString() returns the token string.
AccessToken.getExpiresIn() returns the token's expiry in seconds. This can be used to cache the token for a specific lease time.
AccessToken.getScopes() returns a list of scopes associated with the token.
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.avaya.collaboration.authorization.AuthorizationHelperException;
import com.avaya.collaboration.authorization.client.AccessToken;
import com.avaya.collaboration.authorization.client.AuthorizationClientHelper;
import com.avaya.collaboration.authorization.client.HttpResponseException;
import com.avaya.collaboration.util.logger.Logger;
public class SampleAuthorizationClient
{
private static final Logger logger = Logger.getLogger(SampleAuthorizationClient.class);
// Calls AuthorizationClientHelper to get an access token for a user.
public void getAccessTokenForUser(String userName, String userPass)
{
AccessToken response = null;
try
{
response = AuthorizationClientHelper.getAccessTokenForUser(userName, userPass);
if (response != null)
{
// Return the obtained token back to the user.
}
}
catch (HttpResponseException e)
{
// use this to notify users as to what exactly was the error code
// and the reason associated
// return
// Response.status(e.getStatusCode()).entity(e.getStatusMessage()).build();
}
catch (AuthorizationHelperException e)
{
logger.warn("Caught AuthorizationHelperException: " + e);
}
}
// Calls AuthorizationClientHelper to get a token for a client
public void getAccessToken() throws IOException
{
AccessToken aToken = null;
try
{
aToken = AuthorizationClientHelper.getAccessToken();
if (aToken != null)
{
// Pass the token to getResource() to make a resource request
getResource(aToken);
}
}
catch (HttpResponseException e)
{
logger.warn("Caught HTTP Response exception: " + e);
}
catch (AuthorizationHelperException e)
{
logger.warn("Caught AuthorizationHelperException: " + e);
}
}
// Populates AccessToken in the Authorization header to make a resource
// request.
private final void getResource(AccessToken accessToken) throws IOException
{
try (CloseableHttpClient httpclient = HttpClients.createDefault())
{
HttpGet httpGet = new HttpGet("https://targethost/resource");
// Add the Authorization header with value as the Bearer token got from
// Authorization Service
httpGet.setHeader("Authorization", "Bearer " + accessToken);
try (CloseableHttpResponse response = httpclient.execute(httpGet))
{
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
HttpEntity entity = response.getEntity();
// perform logic with the resource retrieved
EntityUtils.consume(entity);
}
}
}
}
}
Copyright © 2023 Avaya. All rights reserved.