summaryrefslogtreecommitdiffstats
path: root/src/main/java/edu/kit/scc/dei/ecplean/ECPIdPAuth.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/edu/kit/scc/dei/ecplean/ECPIdPAuth.java')
-rw-r--r--src/main/java/edu/kit/scc/dei/ecplean/ECPIdPAuth.java82
1 files changed, 82 insertions, 0 deletions
diff --git a/src/main/java/edu/kit/scc/dei/ecplean/ECPIdPAuth.java b/src/main/java/edu/kit/scc/dei/ecplean/ECPIdPAuth.java
new file mode 100644
index 0000000..0eb035b
--- /dev/null
+++ b/src/main/java/edu/kit/scc/dei/ecplean/ECPIdPAuth.java
@@ -0,0 +1,82 @@
+package edu.kit.scc.dei.ecplean;
+
+import java.io.IOException;
+import java.net.URI;
+
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.xpath.XPathConstants;
+import javax.xml.xpath.XPathException;
+
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.w3c.dom.Document;
+import org.w3c.dom.Node;
+import org.xml.sax.SAXException;
+
+public class ECPIdPAuth extends ECPAuthenticatorBase {
+
+ public ECPIdPAuth(String username, String password,
+ URI idpEcpEndpoint) {
+ this(new DefaultHttpClient(), username, password, idpEcpEndpoint);
+ }
+
+ public ECPIdPAuth(DefaultHttpClient client, String username, String password,
+ URI idpEcpEndpoint) {
+ super(client);
+
+ authInfo = new ECPAuthenticationInfo(username, password, idpEcpEndpoint, null);
+ authInfo.setAuthState(ECPAuthState.NOT_STARTED);
+ }
+
+ public String authenticate(String paosMessage) throws ECPAuthenticationException {
+
+ Document initResponse;
+ try {
+ initResponse = buildDocumentFromString(paosMessage);
+ } catch (IOException e) {
+ logger.debug("Parsing SP Request failed");
+ throw new ECPAuthenticationException(e);
+ } catch (ParserConfigurationException e) {
+ logger.debug("Parsing SP Request failed");
+ throw new ECPAuthenticationException(e);
+ } catch (SAXException e) {
+ logger.debug("Parsing SP Request failed");
+ throw new ECPAuthenticationException(e);
+ }
+
+ String relayState;
+ try {
+ relayState = (String) queryDocument(initResponse, "//ecp:RelayState", XPathConstants.STRING);
+ } catch (XPathException e) {
+ logger.debug("Could not find relay state in PAOS answer from SP");
+ throw new ECPAuthenticationException(e);
+ }
+ logger.info("Got relayState: " + relayState);
+ String responseConsumerUrl;
+ try {
+ responseConsumerUrl = (String) queryDocument(initResponse, "/S:Envelope/S:Header/paos:Request/@responseConsumerURL", XPathConstants.STRING);
+ } catch (XPathException e) {
+ logger.debug("Could not find response consumer url in PAOS answer from SP");
+ throw new ECPAuthenticationException(e);
+ }
+ logger.info("Got responseConsumerUrl: " + responseConsumerUrl);
+
+ Node firstChild = initResponse.getDocumentElement().getFirstChild();
+ initResponse.getDocumentElement().removeChild(firstChild);
+
+ Document idpResponse = authenticateIdP(initResponse);
+ idpResponse.getDocumentElement().getFirstChild().getFirstChild().setTextContent(relayState);
+
+ try {
+ return documentToString(idpResponse);
+ } catch (TransformerConfigurationException e) {
+ logger.debug("documentToString failed");
+ throw new ECPAuthenticationException(e);
+ } catch (TransformerException e) {
+ logger.debug("documentToString failed");
+ throw new ECPAuthenticationException(e);
+ }
+ }
+
+}