teichmann@5835: package org.dive4elements.river.client.server.auth.was; bjoern@2956: bjoern@2956: import java.io.IOException; bjoern@2956: import java.io.InputStream; bjoern@3486: import java.util.List; bjoern@2956: bjoern@2956: import org.apache.commons.codec.binary.Base64InputStream; bjoern@2956: bjoern@2956: import org.apache.http.HttpEntity; bjoern@2956: bjoern@2956: import org.apache.log4j.Logger; bjoern@2956: bjoern@2956: import org.jdom.Document; bjoern@2956: import org.jdom.Element; bjoern@2956: import org.jdom.JDOMException; bjoern@2956: import org.jdom.input.SAXBuilder; bjoern@2956: teichmann@5835: import org.dive4elements.river.client.server.auth.Authentication; teichmann@5835: import org.dive4elements.river.client.server.auth.AuthenticationException; bjoern@2956: teichmann@5835: import org.dive4elements.river.client.server.features.Features; bjoern@3486: bjoern@2956: public class Response implements Authentication { bjoern@2956: bjoern@2956: private static Logger logger = Logger.getLogger(Response.class); bjoern@2956: bjoern@2956: private Element root; bjoern@2956: private Assertion assertion; bjoern@2956: private String username; bjoern@2956: private String password; bjoern@3486: private Features features; bjoern@2956: bjoern@2956: bjoern@3486: public Response(HttpEntity entity, String username, String password, Features features) throws AuthenticationException, IOException { bjoern@2956: bjoern@2956: if (entity == null) { bjoern@2956: throw new ServiceException("Invalid response"); bjoern@2956: } bjoern@2956: bjoern@2956: String contenttype = entity.getContentType().getValue(); bjoern@2956: bjoern@2956: try { bjoern@2956: InputStream in = entity.getContent(); bjoern@2956: bjoern@2956: if (!contenttype.equals("application/vnd.ogc.se_xml")) { bjoern@2956: // XXX: Assume base64 encoded content. bjoern@2956: in = new Base64InputStream(in); bjoern@2956: } bjoern@2956: bjoern@2956: SAXBuilder builder = new SAXBuilder(); bjoern@2956: Document doc = builder.build(in); bjoern@2956: Element root = doc.getRootElement(); bjoern@2956: String rname = root.getName(); bjoern@2956: bjoern@2956: if (rname != null && rname.equals("ServiceExceptionReport")) { bjoern@2956: throw new ServiceException(root.getChildText("ServiceException")); bjoern@2956: } bjoern@2956: bjoern@2956: this.root = root; bjoern@2956: this.username = username; bjoern@2956: this.password = password; bjoern@3486: this.features = features; bjoern@2956: bjoern@2956: } bjoern@2956: catch(JDOMException e) { bjoern@2968: throw new AuthenticationException(e); bjoern@2956: } bjoern@2956: } bjoern@2956: bjoern@2956: public Element getRoot() { bjoern@2956: return this.root; bjoern@2956: } bjoern@2956: bjoern@2956: @Override bjoern@2956: public boolean isSuccess() { bjoern@2956: String status = getStatus(); bjoern@2956: return status != null && status.equals("samlp:Success"); bjoern@2956: } bjoern@2956: bjoern@2956: public String getStatus() { bjoern@2956: Element status = this.root.getChild("Status", Namespaces.SAML_NS_PROTO); bjoern@2956: if (status == null) { bjoern@2956: return null; bjoern@2956: } bjoern@2956: Element statuscode = status.getChild("StatusCode", bjoern@2956: Namespaces.SAML_NS_PROTO); bjoern@2956: if (statuscode == null) { bjoern@2956: return null; bjoern@2956: } bjoern@2956: return statuscode.getAttributeValue("Value"); bjoern@2956: } bjoern@2956: bjoern@2956: public Assertion getAssertion() { bjoern@2956: if (this.assertion == null && this.root != null) { bjoern@2956: Element assertion = this.root.getChild("Assertion", bjoern@2956: Namespaces.SAML_NS_ASSERT); bjoern@2956: if (assertion != null) { bjoern@2956: this.assertion = new Assertion(assertion); bjoern@2956: } bjoern@2956: } bjoern@2956: return this.assertion; bjoern@2956: } bjoern@2956: sascha@2959: @Override bjoern@2968: public User getUser() throws AuthenticationException { bjoern@2968: Assertion assertion = this.getAssertion(); bjoern@2968: if (assertion == null) { bjoern@2968: throw new AuthenticationException("Response doesn't contain an assertion"); bjoern@2968: } bjoern@3486: List features = this.features.getFeatures( bjoern@3486: this.assertion.getRoles()); bjoern@3489: logger.debug("User " + this.username + " with features " + features + bjoern@3489: " successfully authenticated."); bjoern@3504: return new User(this.username, this.password, assertion.getNameID(), bjoern@3486: this.assertion.getRoles(), assertion, features); bjoern@2956: } bjoern@2956: } bjoern@2956: // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80: