Package org.picketlink.identity.federation.api.saml.v2.response

Examples of org.picketlink.identity.federation.api.saml.v2.response.SAML2Response


    private String invokeIDPAndGetSAMLResponse(IDPWebBrowserSSOValve idpAuthenticator, String authnRequest)
            throws ConfigurationException, ProcessingException, ParsingException, LifecycleException, IOException,
            ServletException, Exception {
        byte[] base64Decode = PostBindingUtil.base64Decode(authnRequest);

        AuthnRequestType art = new SAML2Request().getAuthnRequestType(new ByteArrayInputStream(base64Decode));

        // now let's send the previous AuthnRequest to the IDP and authenticate an user. The IDP should return a valid and
        // signed SAML Response.
        MockCatalinaResponse response = new MockCatalinaResponse();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        response.setOutputStream(baos);

        String samlAuth = DocumentUtil.getDocumentAsString(new SAML2Request().convert(art));

        String samlMessage = Base64.encodeBytes(samlAuth.getBytes());

        MockCatalinaRealm realm = new MockCatalinaRealm("anil", "test", new Principal() {
            public String getName() {
View Full Code Here


    @SuppressWarnings("deprecation")
    public void testSAML2Post() throws Exception {
        System.setProperty("picketlink.schema.validate", "true");
        String id = IDGenerator.create("ID_");
        SAML2Request saml2Request = new SAML2Request();
        AuthnRequestType art = saml2Request.createAuthnRequestType(id, employee, identity, employee);

        MockCatalinaContext servletContext = new MockCatalinaContext();

        // First we go to the employee application
        MockCatalinaContextClassLoader mclSPEmp = setupTCL(profile + "/sp/employee");
        Thread.currentThread().setContextClassLoader(mclSPEmp);
        SPPostFormAuthenticator spEmpl = new SPPostFormAuthenticator();

        MockCatalinaContext context = new MockCatalinaContext();
        spEmpl.setContainer(context);
        spEmpl.testStart();

        MockCatalinaRequest catalinaRequest = new MockCatalinaRequest();

        MockCatalinaResponse catalinaResponse = new MockCatalinaResponse();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        catalinaResponse.setOutputStream(baos);

        LoginConfig loginConfig = new LoginConfig();
        spEmpl.authenticate(catalinaRequest, catalinaResponse, loginConfig);

        String spResponse = new String(baos.toByteArray());
        Document spHTMLResponse = DocumentUtil.getDocument(spResponse);
        NodeList nodes = spHTMLResponse.getElementsByTagName("INPUT");
        Element inputElement = (Element) nodes.item(0);
        String idpResponse = inputElement.getAttributeNode("VALUE").getValue();
        @SuppressWarnings("unused")
        String relayState = null;
        if (nodes.getLength() > 1)
            relayState = ((Element) nodes.item(1)).getAttributeNode("VALUE").getValue();

        // Lets call the IDPServlet

        MockCatalinaSession session = new MockCatalinaSession();
        servletContext = new MockCatalinaContext();
        session.setServletContext(servletContext);
        IdentityServer server = this.getIdentityServer(session);
        servletContext.setAttribute("IDENTITY_SERVER", server);

        MockCatalinaContextClassLoader mclIDP = setupTCL(profile + "/idp");
        Thread.currentThread().setContextClassLoader(mclIDP);

        MockCatalinaRequest request = new MockCatalinaRequest();
        request.addHeader("Referer", "http://localhost:8080/employee/");

        request.setParameter(GeneralConstants.USERNAME_FIELD, "anil");
        request.setParameter(GeneralConstants.PASS_FIELD, "anil");

        MockCatalinaResponse response = new MockCatalinaResponse();
        baos = new ByteArrayOutputStream();
        response.setOutputStream(baos);

        context = new MockCatalinaContext();
        IDPWebBrowserSSOValve idp = new IDPWebBrowserSSOValve();
        idp.setContainer(context);
        idp.setSignOutgoingMessages(false);
        idp.start();

        String samlAuth = DocumentUtil.getDocumentAsString(saml2Request.convert(art));

        String samlMessage = Base64.encodeBytes(samlAuth.getBytes());

        MockCatalinaRealm realm = new MockCatalinaRealm("anil", "test", new Principal() {
            public String getName() {
View Full Code Here

     * @param signToken
     */
    private void sendAuthenticationRequest(MockCatalinaRequest request, MockCatalinaResponse response, String issuer,
            String assertionConsumerURL, boolean signToken) {
        try {
            SAML2Request samlRequest = new SAML2Request();

            AuthnRequestType authnRequestType = samlRequest.createAuthnRequestType(IDGenerator.create("ID_"),
                  assertionConsumerURL, getAuthenticator().getConfiguration().getIdpOrSP().getIdentityURL(), issuer);

            Document authnRequestDocument = samlRequest.convert(authnRequestType);

            logger.info("AuthRequestType:" + prettyPrintDocument(authnRequestDocument).toString());

            if (signToken) {
                request.setQueryString(RedirectBindingSignatureUtil.getSAMLRequestURLWithSignature(authnRequestType, null,
View Full Code Here

        String redirectStr = response.redirectString;
        String logoutRequest = redirectStr.substring(redirectStr.indexOf(SAML_REQUEST_KEY) + SAML_REQUEST_KEY.length());

        InputStream stream = RedirectBindingUtil.urlBase64DeflateDecode(logoutRequest);

        SAML2Request saml2Request = new SAML2Request();
        LogoutRequestType lor = (LogoutRequestType) saml2Request.getRequestType(stream);
        assertEquals("Match Employee URL", employee, lor.getIssuer().getValue());
    }
View Full Code Here

        String logoutRequest = redirectStr.substring(redirectStr.indexOf(SAML_REQUEST_KEY) + SAML_REQUEST_KEY.length(),
                redirectStr.indexOf(RELAY_STATE_KEY) - 1);

        InputStream stream = RedirectBindingUtil.urlBase64DeflateDecode(logoutRequest);

        SAML2Request saml2Request = new SAML2Request();
        LogoutRequestType lor = (LogoutRequestType) saml2Request.getRequestType(stream);
        assertEquals("Match Employee URL", employee, destination);
        assertEquals("Destination exists", employee, lor.getDestination().toString());

        // IDP has sent a LogOutRequest which we feed to SPRedirectFormAuthenticator for Employee
        MockCatalinaContextClassLoader mclSPEmp = setupTCL(profile + "/sp/employee");
        Thread.currentThread().setContextClassLoader(mclSPEmp);

        MockCatalinaContext context = new MockCatalinaContext();
        context.setRealm(realm);
        session.setServletContext(context);

        SPRedirectFormAuthenticator sp = new SPRedirectFormAuthenticator();
        sp.setContainer(context);
        sp.testStart();
        sp.getConfiguration().setIdpUsesPostBinding(false);

        request = new MockCatalinaRequest();
        request.setSession(session);
        request.setMethod("GET");
        request.setParameter("SAMLRequest", RedirectBindingUtil.urlDecode(logoutRequest));
        request.setParameter("RelayState", relayState);

        MockCatalinaResponse filterResponse = new MockCatalinaResponse();
        ByteArrayOutputStream filterbaos = new ByteArrayOutputStream();
        filterResponse.setWriter(new PrintWriter(filterbaos));

        sp.authenticate(request, response, new LoginConfig());

        redirectStr = response.redirectString;

        destination = redirectStr.substring(0, redirectStr.indexOf(SAML_RESPONSE_KEY) - 1);
        relayState = redirectStr.substring(redirectStr.indexOf(RELAY_STATE_KEY) + RELAY_STATE_KEY.length());
        assertNotNull("RelayState exists", relayState);
        String logoutResponse = redirectStr.substring(redirectStr.indexOf(SAML_RESPONSE_KEY) + SAML_RESPONSE_KEY.length(),
                redirectStr.indexOf(RELAY_STATE_KEY) - 1);

        stream = RedirectBindingUtil.urlBase64DeflateDecode(logoutResponse);
        StatusResponseType statusResponse = (StatusResponseType) saml2Request.getSAML2ObjectFromStream(stream);
        assertEquals("Match IDP URL", IDP, destination);

        // Now the SP (employee app) has logged out and sending a status response to IDP
        Thread.currentThread().setContextClassLoader(mclIDP);

        session.clear();
        request.clear();

        request.setMethod("GET");
        request.setSession(session);
        request.setUserPrincipal(genericPrincipal);
        request.setParameter("SAMLResponse", RedirectBindingUtil.urlDecode(logoutResponse));
        request.setParameter("RelayState", relayState);

        baos = new ByteArrayOutputStream();
        response.setOutputStream(baos);
        response.setWriter(new PrintWriter(baos));
        idp.invoke(request, response);

        destination = redirectStr.substring(0, redirectStr.indexOf(SAML_RESPONSE_KEY) - 1);
        relayState = redirectStr.substring(redirectStr.indexOf(RELAY_STATE_KEY) + RELAY_STATE_KEY.length());
        logoutResponse = redirectStr.substring(redirectStr.indexOf(SAML_RESPONSE_KEY) + SAML_RESPONSE_KEY.length(),
                redirectStr.indexOf(RELAY_STATE_KEY) - 1);

        stream = RedirectBindingUtil.urlBase64DeflateDecode(logoutResponse);

        SAML2Response saml2Response = new SAML2Response();
        statusResponse = (StatusResponseType) saml2Request.getSAML2ObjectFromStream(stream);
        assertEquals("Match IDP URL", IDP, destination);

        // Now we should have got a full success report from IDP
        MockCatalinaContextClassLoader mclSPSales = setupTCL(profile + "/sp/employee");
        Thread.currentThread().setContextClassLoader(mclSPSales);
View Full Code Here

        mcl.setProfile(resource);
        return mcl;
    }

    private String createLogOutRequest(String url) throws Exception {
        SAML2Request samlRequest = new SAML2Request();
        LogoutRequestType lot = samlRequest.createLogoutRequest(url);

        Principal userPrincipal = new Principal() {
            @Override
            public String getName() {
                return "test";
            }
        };
        NameIDType nameID = new NameIDType();
        nameID.setValue(userPrincipal.getName());
        lot.setNameID(nameID);

        StringWriter sw = new StringWriter();
        samlRequest.marshall(lot, sw);
        return sw.toString();
    }
View Full Code Here

* @author Anil.Saldhana@redhat.com
* @since Dec 11, 2008
*/
public class DeflateEncodingDecodingUnitTestCase extends TestCase {
    public void testDeflateEncoding() throws Exception {
        AuthnRequestType authnRequest = (new SAML2Request()).createAuthnRequestType(IDGenerator.create("ID_"), "http://sp",
                "http://localhost:8080/idp", "http://sp");

        StringWriter sw = new StringWriter();
        SAML2Request request = new SAML2Request();
        request.marshall(authnRequest, sw);
        byte[] deflatedMsg = DeflateUtil.encode(sw.toString());

        String base64Request = Base64.encodeBytes(deflatedMsg, Base64.DONT_BREAK_LINES);

        base64Request = URLEncoder.encode(base64Request, "UTF-8");

        // Decode
        String urlDecodedMsg = URLDecoder.decode(base64Request, "UTF-8");
        byte[] decodedMessage = Base64.decode(urlDecodedMsg);
        InputStream is = DeflateUtil.decode(decodedMessage);
        AuthnRequestType decodedRequestType = request.getAuthnRequestType(is);

        assertNotNull(decodedRequestType);
    }
View Full Code Here

        NodeList nodes = spHTMLResponse.getElementsByTagName("INPUT");
        Element inputElement = (Element) nodes.item(0);
        String logoutRequest = inputElement.getAttributeNode("VALUE").getValue();

        byte[] b64Decoded = PostBindingUtil.base64Decode(logoutRequest);
        SAML2Request saml2Request = new SAML2Request();
        LogoutRequestType lor = (LogoutRequestType) saml2Request.getRequestType(new ByteArrayInputStream(b64Decoded));
        assertEquals("Match Employee URL", employee, lor.getIssuer().getValue());
    }
View Full Code Here

        if (nodes.getLength() > 1)
            relayState = ((Element) nodes.item(1)).getAttributeNode("VALUE").getValue();

        String logoutResponse = new String(Base64.decode(logoutOrigResponse));

        SAML2Request samlRequest = new SAML2Request();
        ByteArrayInputStream bis = new ByteArrayInputStream(logoutResponse.getBytes());
        SAML2Object samlObject = samlRequest.getSAML2ObjectFromStream(bis);
        assertTrue(samlObject instanceof LogoutRequestType);

        // Let us feed the LogOutRequest to the SPFilter
        MockContextClassLoader mclSPEmp = setupTCL(profile + "/sp/employee");
        Thread.currentThread().setContextClassLoader(mclSPEmp);
View Full Code Here

        mcl.setProfile(resource);
        return mcl;
    }

    private String createLogOutRequest(String url) throws Exception {
        SAML2Request samlRequest = new SAML2Request();
        LogoutRequestType lot = samlRequest.createLogoutRequest(url);
        StringWriter sw = new StringWriter();
        samlRequest.marshall(lot, sw);
        return sw.toString();
    }
View Full Code Here

TOP

Related Classes of org.picketlink.identity.federation.api.saml.v2.response.SAML2Response

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.