Search in sources :

Example 1 with AssertionIDRequestMapper

use of com.sun.identity.saml2.plugins.AssertionIDRequestMapper in project OpenAM by OpenRock.

the class AssertionIDRequestUtil method getAssertionIDRequestMapper.

private static AssertionIDRequestMapper getAssertionIDRequestMapper(String realm, String samlAuthorityEntityID, String role) throws SAML2Exception {
    String aidReqMapperName = null;
    AssertionIDRequestMapper aidReqMapper = null;
    try {
        aidReqMapperName = SAML2Utils.getAttributeValueFromSSOConfig(realm, samlAuthorityEntityID, role, SAML2Constants.ASSERTION_ID_REQUEST_MAPPER);
        if (aidReqMapperName == null) {
            aidReqMapperName = SAML2Constants.DEFAULT_ASSERTION_ID_REQUEST_MAPPER_CLASS;
            if (SAML2Utils.debug.messageEnabled()) {
                SAML2Utils.debug.message("AssertionIDRequestUtil.getAssertionIDRequestMapper:" + " use " + aidReqMapperName);
            }
        }
        aidReqMapper = (AssertionIDRequestMapper) assertionIDRequestMapperCache.get(aidReqMapperName);
        if (aidReqMapper == null) {
            aidReqMapper = (AssertionIDRequestMapper) Class.forName(aidReqMapperName).newInstance();
            assertionIDRequestMapperCache.put(aidReqMapperName, aidReqMapper);
        } else {
            if (SAML2Utils.debug.messageEnabled()) {
                SAML2Utils.debug.message("AssertionIDRequestUtil.getAssertionIDRequestMapper:" + " got the AssertionIDRequestMapper from cache");
            }
        }
    } catch (Exception ex) {
        SAML2Utils.debug.error("AssertionIDRequestUtil.getAssertionIDRequestMapper:", ex);
        throw new SAML2Exception(ex);
    }
    return aidReqMapper;
}
Also used : SAML2Exception(com.sun.identity.saml2.common.SAML2Exception) AssertionIDRequestMapper(com.sun.identity.saml2.plugins.AssertionIDRequestMapper) SOAPException(javax.xml.soap.SOAPException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) SAML2MetaException(com.sun.identity.saml2.meta.SAML2MetaException) MalformedURLException(java.net.MalformedURLException) SAML2TokenRepositoryException(org.forgerock.openam.federation.saml2.SAML2TokenRepositoryException) IOException(java.io.IOException) SAML2Exception(com.sun.identity.saml2.common.SAML2Exception)

Example 2 with AssertionIDRequestMapper

use of com.sun.identity.saml2.plugins.AssertionIDRequestMapper in project OpenAM by OpenRock.

the class AssertionIDRequestUtil method processAssertionIDRequestURI.

/**
     * Gets assertion ID from URI and returns assertion if found.
     *
     * @param request the <code>HttpServletRequest</code> object
     * @param response the <code>HttpServletResponse</code> object
     * @param samlAuthorityEntityID entity ID of SAML authority
     * @param role SAML authority role
     * @param realm the realm of hosted entity
     *
     * @exception IOException if response can't be sent
     */
public static void processAssertionIDRequestURI(HttpServletRequest request, HttpServletResponse response, String samlAuthorityEntityID, String role, String realm) throws IOException {
    String assertionID = request.getParameter("ID");
    if (assertionID == null) {
        SAMLUtils.sendError(request, response, HttpServletResponse.SC_BAD_REQUEST, "nullAssertionID", SAML2Utils.bundle.getString("nullAssertionID"));
        return;
    }
    AssertionIDRequestMapper aidReqMapper = null;
    try {
        aidReqMapper = getAssertionIDRequestMapper(realm, samlAuthorityEntityID, role);
    } catch (SAML2Exception ex) {
        SAMLUtils.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "failedToGetAssertionIDRequestMapper", ex.getMessage());
        return;
    }
    try {
        aidReqMapper.authenticateRequesterURI(request, response, samlAuthorityEntityID, role, realm);
    } catch (SAML2Exception ex) {
        SAMLUtils.sendError(request, response, HttpServletResponse.SC_FORBIDDEN, "failedToAuthenticateRequesterURI", ex.getMessage());
        return;
    }
    Assertion assertion = (Assertion) IDPCache.assertionByIDCache.get(assertionID);
    if ((assertion == null) || (!assertion.isTimeValid())) {
        SAMLUtils.sendError(request, response, HttpServletResponse.SC_NOT_FOUND, "invalidAssertionID", SAML2Utils.bundle.getString("invalidAssertionID"));
        return;
    }
    response.setContentType(MIME_TYPE_ASSERTION);
    response.addHeader("Cache-Control", "no-cache, no-store");
    response.addHeader("Pragma", "no-cache");
    String content = null;
    try {
        content = assertion.toXMLString(true, true);
    } catch (SAML2Exception ex) {
        if (SAML2Utils.debug.messageEnabled()) {
            SAML2Utils.debug.message("AssertionIDRequestUtil." + "processAssertionIDRequestURI:", ex);
        }
        SAMLUtils.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "invalidAssertion", ex.getMessage());
        return;
    }
    byte[] bytes = null;
    try {
        bytes = content.getBytes("UTF-8");
    } catch (UnsupportedEncodingException ueex) {
        if (SAML2Utils.debug.messageEnabled()) {
            SAML2Utils.debug.message("AssertionIDRequestUtil." + "processAssertionIDRequestURI:", ueex);
        }
        SAMLUtils.sendError(request, response, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "unsupportedEncoding", ueex.getMessage());
        return;
    }
    response.setContentLength(bytes.length);
    BufferedOutputStream bos = null;
    try {
        bos = new BufferedOutputStream(response.getOutputStream());
        bos.write(bytes, 0, bytes.length);
    } catch (IOException ioex) {
        SAML2Utils.debug.error("AssertionIDRequestUtil." + "processAssertionIDRequestURI:", ioex);
    } finally {
        if (bos != null) {
            try {
                bos.close();
            } catch (IOException ioex) {
                SAML2Utils.debug.error("AssertionIDRequestUtil." + "processAssertionIDRequestURI:", ioex);
            }
        }
    }
}
Also used : SAML2Exception(com.sun.identity.saml2.common.SAML2Exception) AssertionIDRequestMapper(com.sun.identity.saml2.plugins.AssertionIDRequestMapper) Assertion(com.sun.identity.saml2.assertion.Assertion) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Aggregations

SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)2 AssertionIDRequestMapper (com.sun.identity.saml2.plugins.AssertionIDRequestMapper)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 Assertion (com.sun.identity.saml2.assertion.Assertion)1 SAML2MetaException (com.sun.identity.saml2.meta.SAML2MetaException)1 BufferedOutputStream (java.io.BufferedOutputStream)1 MalformedURLException (java.net.MalformedURLException)1 SOAPException (javax.xml.soap.SOAPException)1 SAML2TokenRepositoryException (org.forgerock.openam.federation.saml2.SAML2TokenRepositoryException)1