/**
* Get the access token
*/
protected ServerAccessToken getAccessToken() {
ServerAccessToken accessToken = null;
if (dataProvider == null && tokenHandlers.isEmpty()) {
throw new WebApplicationException(500);
}
// Get the scheme and its data, Bearer only is supported by default
// WWW-Authenticate with the list of supported schemes will be sent back
// if the scheme is not accepted
String[] authParts = AuthorizationUtils.getAuthorizationParts(mc, supportedSchemes);
String authScheme = authParts[0];
String authSchemeData = authParts[1];
// Get the registered handler capable of processing the token
AccessTokenValidator handler = findTokenHandler(authScheme);
if (handler != null) {
try {
// Convert the HTTP Authorization scheme data into a token
accessToken = handler.getAccessToken(authSchemeData);
} catch (OAuthServiceException ex) {
AuthorizationUtils.throwAuthorizationFailure(
Collections.singleton(authScheme));
}
}
// Default processing if no registered providers available
if (accessToken == null && authScheme.equals(DEFAULT_AUTH_SCHEME)) {
try {
accessToken = dataProvider.getAccessToken(authSchemeData);
} catch (OAuthServiceException ex) {
AuthorizationUtils.throwAuthorizationFailure(
Collections.singleton(authScheme));
}
}
if (accessToken == null) {
AuthorizationUtils.throwAuthorizationFailure(supportedSchemes);
}
// Check if token is still valid
if (OAuthUtils.isExpired(accessToken.getIssuedAt(), accessToken.getLifetime())) {
dataProvider.removeAccessToken(accessToken);
AuthorizationUtils.throwAuthorizationFailure(supportedSchemes);
}
return accessToken;
}