@Inject
private SecurityRealm securityRealm;
public View process(HttpServletRequest request, HttpServletResponse response) throws ServletException {
PebbleUserDetails userDetails = SecurityUtils.getUserDetails();
ValidationContext validationContext = new ValidationContext();
AbstractBlog blog = (AbstractBlog)getModel().get(Constants.BLOG_KEY);
String identity = request.getParameter("openid.identity");
// No identity, assume this is an add request
if (identity == null || identity.length() == 0) {
String claimedIdentity = request.getParameter("openid_identifier");
try {
String returnToUrl = request.getRequestURL().toString();
String realm = PebbleContext.getInstance().getConfiguration().getUrl();
String openIdUrl = openIDConsumer.beginConsumption(request, claimedIdentity, returnToUrl, realm);
return new RedirectView(openIdUrl);
} catch (OpenIDConsumerException oice) {
log.error("Error adding OpenID", oice);
validationContext.addError("Error adding OpenID " + oice.getMessage());
}
} else {
try {
OpenIDAuthenticationToken token = openIDConsumer.endConsumption(request);
if (token.getStatus() == OpenIDAuthenticationStatus.SUCCESS) {
// Check that the OpenID isn't already mapped
String openId = token.getIdentityUrl();
if (securityRealm.getUserForOpenId(openId) != null) {
validationContext.addError("The OpenID supplied is already mapped to a user.");
} else {
// Add it
securityRealm.addOpenIdToUser(userDetails, openId);
return new RedirectView(blog.getUrl() + "/editUserPreferences.secureaction");
}
} else {
validationContext.addError(StringUtils.transformHTML(token.getMessage()));
}
} catch (OpenIDConsumerException oice) {
log.error("Error in consumer", oice);
validationContext.addError("Error adding OpenID " + oice.getMessage());
} catch (SecurityRealmException sre) {
log.error("Error looking up user by security realm", sre);
}
}