Optional<ConsumerManager> consumerManagerOptional = InMemoryOpenIDCache.INSTANCE.getConsumerManager(sessionToken);
if (!consumerManagerOptional.isPresent()) {
log.debug("Authentication failed due to no consumer manager matching session token {}", rawToken);
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
ConsumerManager consumerManager = consumerManagerOptional.get();
// Attempt to locate the user by the session token
Optional<User> tempUserOptional = InMemoryUserCache.INSTANCE.getBySessionToken(sessionToken);
if (!tempUserOptional.isPresent()) {
log.debug("Authentication failed due to no temp User matching session token {}", rawToken);
throw new WebApplicationException(Response.Status.UNAUTHORIZED);
}
User tempUser = tempUserOptional.get();
// Retrieve the discovery information
final DiscoveryInformationMemento memento = tempUser.getOpenIDDiscoveryInformationMemento();
Identifier identifier = new Identifier() {
@Override
public String getIdentifier() {
return memento.getClaimedIdentifier();
}
};
DiscoveryInformation discovered;
try {
discovered = new DiscoveryInformation(
URI.create(memento.getOpEndpoint()).toURL(),
identifier,
memento.getDelegate(),
memento.getVersion(),
memento.getTypes()
);
} catch (DiscoveryException e) {
throw new WebApplicationException(e, Response.Status.UNAUTHORIZED);
} catch (MalformedURLException e) {
throw new WebApplicationException(e, Response.Status.UNAUTHORIZED);
}
// Extract the receiving URL from the HTTP request
StringBuffer receivingURL = request.getRequestURL();
String queryString = request.getQueryString();
if (queryString != null && queryString.length() > 0) {
receivingURL.append("?").append(request.getQueryString());
}
log.debug("Receiving URL = '{}", receivingURL.toString());
// Extract the parameters from the authentication response
// (which comes in as a HTTP request from the OpenID provider)
ParameterList parameterList = new ParameterList(request.getParameterMap());
try {
// Verify the response
// ConsumerManager needs to be the same (static) instance used
// to place the authentication request
// This could be tricky if this service is load-balanced
VerificationResult verification = consumerManager.verify(
receivingURL.toString(),
parameterList,
discovered);
// Examine the verification result and extract the verified identifier