[java-idp-testbed] branch master updated: Add mock CAS controller.

Tom Zeller tzeller at dragonacea.biz
Wed Nov 11 20:56:47 EST 2015


This is an automated email from the git hooks/post-receive script.

tzeller pushed a commit to branch master
in repository java-idp-testbed.

The following commit(s) were added to refs/heads/master by this push:
       new  5de52bb   Add mock CAS controller.
5de52bb is described below

commit 5de52bbfa90c6fdbb07cee012e3cd03695979297
Author: Tom Zeller <tzeller at dragonacea.biz>
AuthorDate: Wed Nov 11 19:55:46 2015 -0600

    Add mock CAS controller.
---
 src/main/java/sp/CASController.java | 129 ++++++++++++++++++++++++++++++++++++
 1 file changed, 129 insertions(+)

diff --git a/src/main/java/sp/CASController.java b/src/main/java/sp/CASController.java
new file mode 100644
index 0000000..1c46608
--- /dev/null
+++ b/src/main/java/sp/CASController.java
@@ -0,0 +1,129 @@
+/*
+ * Licensed to the University Corporation for Advanced Internet Development, 
+ * Inc. (UCAID) under one or more contributor license agreements.  See the 
+ * NOTICE file distributed with this work for additional information regarding
+ * copyright ownership. The UCAID licenses this file to You under the Apache 
+ * License, Version 2.0 (the "License"); you may not use this file except in 
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package sp;
+
+import java.net.MalformedURLException;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.stereotype.Controller;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+
+import net.shibboleth.idp.cas.protocol.ProtocolParam;
+import net.shibboleth.utilities.java.support.collection.Pair;
+import net.shibboleth.utilities.java.support.net.HttpServletSupport;
+import net.shibboleth.utilities.java.support.net.URLBuilder;
+
+ at Controller
+ at RequestMapping("/CAS")
+public class CASController {
+
+    private Logger log = LoggerFactory.getLogger(CASController.class);
+
+    public String idpCASEndpointPath = "/idp/profile/cas";
+
+    public String casSPServicePath = "/sp/CAS/Service";
+
+    /**
+     * Init SSO by redirecting to the CAS login service.
+     * 
+     * @param servletRequest
+     * @param servletResponse
+     * @throws Exception
+     */
+    @RequestMapping(value = "/InitSSO", method = RequestMethod.GET)
+    public void initLogin(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
+
+        final String baseUrl = getBaseUrl(servletRequest);
+
+        final String endpointURL = baseUrl + idpCASEndpointPath + "/login";
+
+        final URLBuilder urlBuilder = new URLBuilder(endpointURL);
+
+        final List<Pair<String, String>> queryParams = urlBuilder.getQueryParams();
+
+        queryParams.add(new Pair<String, String>(ProtocolParam.Service.id(), baseUrl + casSPServicePath));
+
+        final String redirectURL = urlBuilder.buildURL();
+
+        HttpServletSupport.addNoCacheHeaders(servletResponse);
+        HttpServletSupport.setUTF8Encoding(servletResponse);
+
+        log.debug("Sending redirect to '{}'", redirectURL);
+        servletResponse.sendRedirect(redirectURL);
+    }
+
+    /**
+     * Produce a page displaying a link to the CAS validation endpoint.
+     * 
+     * @param servletRequest
+     * @param servletResponse
+     * @return HTML displaying a link to the CAS validation endpoint
+     * @throws Exception
+     */
+    @RequestMapping(value = "/Service", method = RequestMethod.GET)
+    public ResponseEntity<String> handleCASResponse(HttpServletRequest servletRequest, HttpServletResponse servletResponse) throws Exception {
+
+        final String baseUrl = getBaseUrl(servletRequest);
+
+        final String endpointURL = baseUrl + idpCASEndpointPath + "/serviceValidate";
+
+        final URLBuilder urlBuilder = new URLBuilder(endpointURL);
+
+        final String ticket = servletRequest.getParameter(ProtocolParam.Ticket.id());
+
+        final List<Pair<String, String>> queryParams = urlBuilder.getQueryParams();
+        queryParams.add(new Pair<String, String>(ProtocolParam.Service.id(), baseUrl + casSPServicePath));
+        queryParams.add(new Pair<String, String>(ProtocolParam.Ticket.id(), ticket));
+
+        final String redirectURL = urlBuilder.buildURL();
+
+        final String html = "<html><body><a id=cas-service-validate href=\"" + redirectURL
+                + "\">CAS Service Validate</a></body></html>";
+        log.trace("Returning html '{}'", html);
+
+        final HttpHeaders headers = new HttpHeaders();
+        headers.add("Content-Type", "text/html");
+
+        return new ResponseEntity<>(html, headers, HttpStatus.OK);
+    }
+
+    private String getBaseUrl(HttpServletRequest servletRequest) {
+        String requestUrl = servletRequest.getRequestURL().toString();
+        try {
+            URLBuilder urlBuilder = new URLBuilder(requestUrl);
+            urlBuilder.setUsername(null);
+            urlBuilder.setPassword(null);
+            urlBuilder.setPath(null);
+            urlBuilder.getQueryParams().clear();
+            urlBuilder.setFragment(null);
+            return urlBuilder.buildURL();
+        } catch (MalformedURLException e) {
+            log.error("Couldn't parse request URL, reverting to internal default base URL: {}", requestUrl);
+            return "http://localhost:8080";
+        }
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
the administrator of this repository.


More information about the commits mailing list