Mercurial > dive4elements > river
annotate flys-client/src/main/java/de/intevation/flys/client/server/was/Response.java @ 2943:7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
request and response. If the authentication is successful the WAS
responses with a base64 encoded Security Assertion Markup Language.
The current implementation of the saml response simplifies the protocol
and misses validation.
flys-client/trunk@4909 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Bjoern Ricks <bjoern.ricks@intevation.de> |
---|---|
date | Tue, 10 Jul 2012 10:49:18 +0000 |
parents | |
children | 927a3bd932d5 |
rev | line source |
---|---|
2943
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
1 package de.intevation.flys.client.server.was; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
2 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
3 import java.io.ByteArrayInputStream; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
4 import java.io.IOException; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
5 import java.io.InputStream; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
6 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
7 import org.apache.commons.codec.binary.Base64; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
8 import org.apache.commons.io.IOUtils; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
9 import org.apache.http.HttpEntity; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
10 import org.apache.log4j.Logger; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
11 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
12 import org.jdom.Document; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
13 import org.jdom.Element; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
14 import org.jdom.JDOMException; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
15 import org.jdom.Namespace; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
16 import org.jdom.input.SAXBuilder; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
17 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
18 public class Response { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
19 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
20 private static Logger logger = Logger.getLogger(Response.class); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
21 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
22 private Element root; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
23 private Assertion assertion; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
24 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
25 public Response(HttpEntity entity) throws ServiceException { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
26 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
27 if (entity == null) { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
28 throw new ServiceException("Invalid response"); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
29 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
30 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
31 String contenttype = entity.getContentType().getValue(); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
32 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
33 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
34 try{ |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
35 InputStream in = entity.getContent(); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
36 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
37 if (!contenttype.equals("application/vnd.ogc.se_xml")) { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
38 // assume base64 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
39 byte[] content = IOUtils.toByteArray(entity.getContent()); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
40 in = new ByteArrayInputStream(Base64.decodeBase64(content)); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
41 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
42 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
43 SAXBuilder builder = new SAXBuilder(); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
44 Document doc = builder.build(in); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
45 Element root = doc.getRootElement(); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
46 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
47 if (root.getName() == "ServiceExceptionReport") { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
48 throw new ServiceException(root.getChildText("ServiceException")); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
49 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
50 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
51 this.root = root; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
52 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
53 catch(JDOMException e) { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
54 logger.error(e); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
55 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
56 catch(IOException e) { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
57 logger.error(e); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
58 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
59 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
60 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
61 public Element getRoot() { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
62 return this.root; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
63 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
64 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
65 public Boolean isSuccess() { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
66 return this.getStatus() == "samlp:Success"; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
67 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
68 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
69 public String getStatus() { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
70 Element status = this.root.getChild("Status", Namespaces.SAML_NS_PROTO); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
71 if (status == null) { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
72 return null; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
73 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
74 Element statuscode = status.getChild("StatusCode", |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
75 Namespaces.SAML_NS_PROTO); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
76 if (statuscode == null) { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
77 return null; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
78 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
79 return statuscode.getAttributeValue("Value"); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
80 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
81 |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
82 public Assertion getAssertion() { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
83 if (this.assertion == null && this.root != null) { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
84 Element assertion = this.root.getChild("Assertion", |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
85 Namespaces.SAML_NS_ASSERT); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
86 if (assertion != null) { |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
87 this.assertion = new Assertion(assertion); |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
88 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
89 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
90 return this.assertion; |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
91 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
92 } |
7683d4e43afa
Implement class representation of a Web Authentication Service (WAS)
Bjoern Ricks <bjoern.ricks@intevation.de>
parents:
diff
changeset
|
93 // vim: set si et fileencoding=utf-8 ts=4 sw=4 tw=80: |