Package org.picketlink.test.identity.federation.web.mock

Examples of org.picketlink.test.identity.federation.web.mock.MockHttpSession


public class IdentityServerUnitTestCase extends TestCase {
    public void testActiveSessionCount() {
        IdentityServer server = new IdentityServer();
        assertEquals(0, server.getActiveSessionCount());

        MockHttpSession session = new MockHttpSession();
        session.setServletContext(new MockServletContext());
        HttpSessionEvent event = new HttpSessionEvent(session);
        server.sessionCreated(event);
        assertEquals(1, server.getActiveSessionCount());

        server.sessionDestroyed(event);
View Full Code Here


        // Initialize the handler
        handler.initChainConfig(chainConfig);
        handler.initHandlerConfig(handlerConfig);

        // Create a Protocol Context
        MockHttpSession session = new MockHttpSession();
        MockServletContext servletContext = new MockServletContext();
        MockHttpServletRequest servletRequest = new MockHttpServletRequest(session, "POST");
        MockHttpServletResponse servletResponse = new MockHttpServletResponse();
        HTTPContext httpContext = new HTTPContext(servletRequest, servletResponse, servletContext);

        SAML2Object saml2Object = new SAML2Object() {
        };

        SAMLDocumentHolder docHolder = new SAMLDocumentHolder(saml2Object, null);
        IssuerInfoHolder issuerInfo = new IssuerInfoHolder("http://localhost:8080/idp/");
        SAML2HandlerRequest request = new DefaultSAML2HandlerRequest(httpContext, issuerInfo.getIssuer(), docHolder,
                SAML2Handler.HANDLER_TYPE.IDP);
        SAML2HandlerResponse response = new DefaultSAML2HandlerResponse();

        session.setAttribute(GeneralConstants.PRINCIPAL_ID, new Principal() {
            public String getName() {
                return name;
            }
        });
        handler.handleRequestType(request, response);

        Map<String, Object> attribs = (Map<String, Object>) session.getAttribute(GeneralConstants.ATTRIBUTES);
        assertNotNull("Attributes are not null", attribs);
        assertEquals(email, attribs.get(X500SAMLProfileConstants.EMAIL.getFriendlyName()));
    }
View Full Code Here

     */
    @Test
    public void testSPFilterLogOutRequestGeneration() throws Exception {
        tcl = Thread.currentThread().getContextClassLoader();

        MockHttpSession session = new MockHttpSession();
        session.setAttribute(GeneralConstants.PRINCIPAL_ID, new Principal() {
            public String getName() {
                return "anil";
            }
        });
        List<String> rolesList = new ArrayList<String>();
        rolesList.add("manager");
        session.setAttribute(GeneralConstants.ROLES_ID, rolesList);

        ServletContext servletContext = new MockServletContext();
        session.setServletContext(servletContext);

        // Let us feed the LogOutRequest to the SPFilter
        MockContextClassLoader mclSPEmp = setupTCL(profile + "/sp/employee");
        Thread.currentThread().setContextClassLoader(mclSPEmp);
        SPFilter spEmpl = new SPFilter();
        MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
        filterConfig.addInitParameter(GeneralConstants.IGNORE_SIGNATURES, "true");

        spEmpl.init(filterConfig);

        MockHttpSession filterSession = new MockHttpSession();
        MockHttpServletRequest filterRequest = new MockHttpServletRequest(filterSession, "POST");
        filterRequest.addParameter(GeneralConstants.GLOBAL_LOGOUT, "true");

        MockHttpServletResponse filterResponse = new MockHttpServletResponse();
        ByteArrayOutputStream filterbaos = new ByteArrayOutputStream();
View Full Code Here

        // Initialize the handler
        handler.initChainConfig(chainConfig);
        handler.initHandlerConfig(handlerConfig);

        // Create a Protocol Context
        MockHttpSession session = new MockHttpSession();
        MockServletContext servletContext = new MockServletContext();
        MockHttpServletRequest servletRequest = new MockHttpServletRequest(session, "POST");
        MockHttpServletResponse servletResponse = new MockHttpServletResponse();
        HTTPContext httpContext = new HTTPContext(servletRequest, servletResponse, servletContext);

        ResponseType saml2Object = new SAML2Response().createResponseType("fake_id");

        SAMLDocumentHolder docHolder = new SAMLDocumentHolder(saml2Object, null);
        IssuerInfoHolder issuerInfo = new IssuerInfoHolder("http://localhost:8080/idp/");
        SAML2HandlerRequest request = new DefaultSAML2HandlerRequest(httpContext, issuerInfo.getIssuer(), docHolder,
                SAML2Handler.HANDLER_TYPE.IDP);
        SAML2HandlerResponse response = new DefaultSAML2HandlerResponse();

        AssertionType assertion = new AssertionType(IDGenerator.create("ID_"), XMLTimeUtil.getIssueInstant());

        Map<String, Object> myattr = new HashMap<String, Object>();
        myattr.put("testKey", "hello");
        AttributeStatementType attState = StatementUtil.createAttributeStatement(myattr);
        assertion.addStatement(attState);

        request.addOption(GeneralConstants.ASSERTION, assertion);
        handler.handleStatusResponseType(request, response);

        Map<String, List<Object>> sessionMap = (Map<String, List<Object>>) session
                .getAttribute(GeneralConstants.SESSION_ATTRIBUTE_MAP);
        assertNotNull(sessionMap);
        List<Object> values = sessionMap.get("testKey");
        assertEquals("hello", values.get(0));
    }
View Full Code Here

     * @throws Exception
     */
    @Test
    public void testSAML2LogOutFromIDPServlet() throws Exception {
        tcl = Thread.currentThread().getContextClassLoader();
        MockHttpSession session = new MockHttpSession();

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

        URL url = Thread.currentThread().getContextClassLoader().getResource("roles.properties");
        assertNotNull("roles.properties visible?", url);

        ServletContext servletContext = new MockServletContext();
        session.setServletContext(servletContext);

        IdentityServer server = this.getIdentityServer(session);
        servletContext.setAttribute("IDENTITY_SERVER", server);
        MockServletConfig servletConfig = new MockServletConfig(servletContext);

        IDPServlet idp = new IDPServlet();
        // No signing outgoing messages
        servletConfig.addInitParameter(GeneralConstants.SIGN_OUTGOING_MESSAGES, "false");

        // Initialize the servlet
        idp.init(servletConfig);

        // Assume that we already have the principal and roles set in the session
        session.setAttribute(GeneralConstants.PRINCIPAL_ID, new Principal() {
            public String getName() {
                return "anil";
            }
        });
        List<String> rolesList = new ArrayList<String>();
        rolesList.add("manager");
        session.setAttribute(GeneralConstants.ROLES_ID, rolesList);

        MockHttpServletRequest request = new MockHttpServletRequest(session, "POST");
        request.addHeader("Referer", sales);

        String samlMessage = Base64.encodeBytes(createLogOutRequest(sales).getBytes());
        session.setAttribute("SAMLRequest", samlMessage);

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

        // The IDP is preloaded with 2 participants : "http://localhost:8080/sales/"
        // and "http://localhost:8080/employee"

        // Lets start the workflow with post
        idp.testPost(request, response);

        String idpResponse = new String(baos.toByteArray());
        assertNotNull(idpResponse);

        Document htmlResponse = DocumentUtil.getDocument(idpResponse);
        assertNotNull(htmlResponse);
        NodeList nodes = htmlResponse.getElementsByTagName("INPUT");
        Element inputElement = (Element) nodes.item(0);
        String logoutOrigResponse = inputElement.getAttributeNode("VALUE").getValue();

        String relayState = null;
        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);
        SPFilter spEmpl = new SPFilter();
        MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
        filterConfig.addInitParameter(GeneralConstants.IGNORE_SIGNATURES, "true");

        spEmpl.init(filterConfig);

        MockHttpSession filterSession = new MockHttpSession();
        MockHttpServletRequest filterRequest = new MockHttpServletRequest(filterSession, "POST");
        filterRequest.addParameter("SAMLResponse", logoutOrigResponse);
        filterRequest.addParameter("RelayState", relayState);

        MockHttpServletResponse filterResponse = new MockHttpServletResponse();
        ByteArrayOutputStream filterbaos = new ByteArrayOutputStream();
        filterResponse.setOutputStream(filterbaos);

        spEmpl.doFilter(filterRequest, filterResponse, new MockFilterChain());
        String spResponse = new String(filterbaos.toByteArray());
        Document spHTMLResponse = DocumentUtil.getDocument(spResponse);
        nodes = spHTMLResponse.getElementsByTagName("INPUT");
        inputElement = (Element) nodes.item(0);
        logoutOrigResponse = inputElement.getAttributeNode("VALUE").getValue();
        relayState = null;
        if (nodes.getLength() > 1)
            relayState = ((Element) nodes.item(1)).getAttributeNode("VALUE").getValue();

        // Now the SP (employee app) has logged out and sending a status response to IDP
        Thread.currentThread().setContextClassLoader(mclIDP);
        session.setAttribute("SAMLResponse", logoutOrigResponse);
        session.setAttribute("RelayState", relayState);

        idp.testPost(request, response);

        idpResponse = new String(filterbaos.toByteArray());
        assertNotNull(idpResponse);

        htmlResponse = DocumentUtil.getDocument(idpResponse);
        assertNotNull(htmlResponse);
        nodes = htmlResponse.getElementsByTagName("INPUT");
        inputElement = (Element) nodes.item(0);
        logoutOrigResponse = inputElement.getAttributeNode("VALUE").getValue();

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

        // Now we should have got a full success report from IDP
        MockContextClassLoader mclSPSales = setupTCL(profile + "/sp/employee");
        Thread.currentThread().setContextClassLoader(mclSPSales);
        SPFilter spSales = new SPFilter();

        spSales.init(filterConfig);

        filterRequest.addParameter("SAMLResponse", logoutOrigResponse);
        filterRequest.addParameter("RelayState", relayState);

        spSales.doFilter(filterRequest, filterResponse, new MockFilterChain());

        spResponse = new String(filterbaos.toByteArray());

        assertEquals(0, server.stack().getParticipants(session.getId()));
        assertEquals(0, server.stack().getNumOfParticipantsInTransit(session.getId()));

        spHTMLResponse = DocumentUtil.getDocument(spResponse);
        nodes = spHTMLResponse.getElementsByTagName("INPUT");
        inputElement = (Element) nodes.item(0);
        logoutOrigResponse = inputElement.getAttributeNode("VALUE").getValue();
        relayState = null;
        if (nodes.getLength() > 1)
            relayState = ((Element) nodes.item(1)).getAttributeNode("VALUE").getValue();

        // Finally the session should be invalidated
        assertTrue(filterSession.isInvalidated());
    }
View Full Code Here

    public void testIssuer() throws Exception {
        SAML2IssuerTrustHandler issuerTrustHandler = new SAML2IssuerTrustHandler();

        // Create a Protocol Context
        MockHttpSession session = new MockHttpSession();
        MockServletContext servletContext = new MockServletContext();
        MockHttpServletRequest servletRequest = new MockHttpServletRequest(session, "POST");
        MockHttpServletResponse servletResponse = new MockHttpServletResponse();
        HTTPContext httpContext = new HTTPContext(servletRequest, servletResponse, servletContext);
View Full Code Here

        MockFilterConfig filterConfig = new MockFilterConfig(servletContext);
        filterConfig.addInitParameter(GeneralConstants.IGNORE_SIGNATURES, "true");

        spEmpl.init(filterConfig);

        MockHttpSession filterSession = new MockHttpSession();
        MockHttpServletRequest filterRequest = new MockHttpServletRequest(filterSession, "POST");

        MockHttpServletResponse filterResponse = new MockHttpServletResponse();
        ByteArrayOutputStream filterbaos = new ByteArrayOutputStream();
        filterResponse.setOutputStream(filterbaos);

        spEmpl.doFilter(filterRequest, filterResponse, new MockFilterChain());
        String spResponse = new String(filterbaos.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

        MockHttpSession session = new MockHttpSession();
        servletContext = new MockServletContext();
        session.setServletContext(servletContext);
        IdentityServer server = this.getIdentityServer(session);
        servletContext.setAttribute("IDENTITY_SERVER", server);
        MockServletConfig servletConfig = new MockServletConfig(servletContext);

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

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

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

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

        IDPLoginServlet login = new IDPLoginServlet();
        login.init(servletConfig);

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

        String samlMessage = Base64.encodeBytes(samlAuth.getBytes());
        session.setAttribute("SAMLRequest", samlMessage);

        login.testPost(request, response);

        IDPServlet idp = new IDPServlet();
        // No signing outgoing messages
View Full Code Here

        // Initialize the handler
        handler.initChainConfig(chainConfig);
        handler.initHandlerConfig(handlerConfig);

        // Create a Protocol Context
        MockHttpSession session = new MockHttpSession();
        MockServletContext servletContext = new MockServletContext();
        String httpMethod = isPostBinding ? "POST" : "GET";
        MockHttpServletRequest servletRequest = new MockHttpServletRequest(session, httpMethod);
        MockHttpServletResponse servletResponse = new MockHttpServletResponse();
        HTTPContext httpContext = new HTTPContext(servletRequest, servletResponse, servletContext);
View Full Code Here

        // Initialize the handler
        handler.initChainConfig(chainConfig);
        handler.initHandlerConfig(handlerConfig);

        // Create a Protocol Context
        MockHttpSession session = new MockHttpSession();
        MockServletContext servletContext = new MockServletContext();
        MockHttpServletRequest servletRequest = new MockHttpServletRequest(session, "POST");
        MockHttpServletResponse servletResponse = new MockHttpServletResponse();
        HTTPContext httpContext = new HTTPContext(servletRequest, servletResponse, servletContext);
View Full Code Here

        // Initialize the handler
        handler.initChainConfig(chainConfig);
        handler.initHandlerConfig(handlerConfig);

        // Create a Protocol Context
        MockHttpSession session = new MockHttpSession();
        MockServletContext servletContext = new MockServletContext();
        MockHttpServletRequest servletRequest = new MockHttpServletRequest(session, "POST");
        MockHttpServletResponse servletResponse = new MockHttpServletResponse();
        HTTPContext httpContext = new HTTPContext(servletRequest, servletResponse, servletContext);

        SAML2Object saml2Object = new SAML2Object() {
        };

        KeyPair keypair = KeyStoreUtil.generateKeyPair("RSA");

        SAML2Response saml2Response = new SAML2Response();
        IssuerInfoHolder issuerInfoholder = new IssuerInfoHolder("testIssuer");

        AssertionType assertion = AssertionUtil.createAssertion(IDGenerator.create("ID_"), new NameIDType());
        SubjectType assertionSubject = new SubjectType();
        STSubType subType = new STSubType();
        NameIDType anil = new NameIDType();
        anil.setValue("anil");
        subType.addBaseID(anil);
        assertionSubject.setSubType(subType);
        assertion.setSubject(assertionSubject);

        ResponseType responseType = saml2Response.createResponseType(IDGenerator.create("ID_"), issuerInfoholder, assertion);

        String assertionNS = JBossSAMLURIConstants.ASSERTION_NSURI.get();

        QName assertionQName = new QName(assertionNS, "EncryptedAssertion", "saml");
        Document responseDoc = saml2Response.convert(responseType);

        byte[] secret = WSTrustUtil.createRandomSecret(128 / 8);
        SecretKey secretKey = new SecretKeySpec(secret, "AES");

        PublicKey publicKey = keypair.getPublic();
        XMLEncryptionUtil.encryptElement(new QName(assertionNS, "Assertion", "saml"), responseDoc, publicKey, secretKey, 128,
                assertionQName, true);

        SAMLParser parser = new SAMLParser();
        saml2Object = (SAML2Object) parser.parse(DocumentUtil.getNodeAsStream(responseDoc));

        SAMLDocumentHolder docHolder = new SAMLDocumentHolder(saml2Object, null);
        IssuerInfoHolder issuerInfo = new IssuerInfoHolder("http://localhost:8080/idp/");
        SAML2HandlerRequest request = new DefaultSAML2HandlerRequest(httpContext, issuerInfo.getIssuer(), docHolder,
                SAML2Handler.HANDLER_TYPE.SP);
        request.addOption(GeneralConstants.DECRYPTING_KEY, keypair.getPrivate());

        SAML2HandlerResponse response = new DefaultSAML2HandlerResponse();

        session.setAttribute(GeneralConstants.PRINCIPAL_ID, new Principal() {
            public String getName() {
                return "Hi";
            }
        });
View Full Code Here

TOP

Related Classes of org.picketlink.test.identity.federation.web.mock.MockHttpSession

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.