Search in sources :

Example 1 with MessageEncodingException

use of org.opensaml.ws.message.encoder.MessageEncodingException in project MaxKey by dromara.

the class AssertionEndpoint method assertion.

@RequestMapping(value = "/authz/saml20/assertion")
public ModelAndView assertion(HttpServletRequest request, HttpServletResponse response) throws Exception {
    logger.debug("saml20 assertion start.");
    bindingAdapter = (BindingAdapter) request.getSession().getAttribute(WebConstants.AUTHORIZE_SIGN_ON_APP_SAMLV20_ADAPTER);
    logger.debug("saml20 assertion get session samlv20Adapter " + bindingAdapter);
    AppsSAML20Details saml20Details = bindingAdapter.getSaml20Details();
    logger.debug("saml20Details " + saml20Details.getExtendAttr());
    AuthnRequestInfo authnRequestInfo = bindingAdapter.getAuthnRequestInfo();
    if (authnRequestInfo == null) {
        logger.warn("Could not find AuthnRequest on the request.  Responding with SC_FORBIDDEN.");
        throw new Exception();
    }
    logger.debug("AuthnRequestInfo: {}", authnRequestInfo);
    HashMap<String, String> attributeMap = new HashMap<String, String>();
    attributeMap.put(WebConstants.ONLINE_TICKET_NAME, ((SigninPrincipal) WebContext.getAuthentication().getPrincipal()).getOnlineTicket().getTicketId());
    // saml20Details
    Response authResponse = authnResponseGenerator.generateAuthnResponse(saml20Details, authnRequestInfo, attributeMap, bindingAdapter);
    Endpoint endpoint = endpointGenerator.generateEndpoint(saml20Details.getSpAcsUrl());
    request.getSession().removeAttribute(AuthnRequestInfo.class.getName());
    // request issuer...
    try {
        bindingAdapter.sendSAMLMessage(authResponse, endpoint, request, response);
    } catch (MessageEncodingException mee) {
        logger.error("Exception encoding SAML message", mee);
        throw new Exception(mee);
    }
    return null;
}
Also used : HttpServletResponse(javax.servlet.http.HttpServletResponse) Response(org.opensaml.saml2.core.Response) AppsSAML20Details(org.maxkey.entity.apps.AppsSAML20Details) Endpoint(org.opensaml.saml2.metadata.Endpoint) HashMap(java.util.HashMap) AuthnRequestInfo(org.maxkey.authz.saml.common.AuthnRequestInfo) SigninPrincipal(org.maxkey.authn.SigninPrincipal) MessageEncodingException(org.opensaml.ws.message.encoder.MessageEncodingException) MessageEncodingException(org.opensaml.ws.message.encoder.MessageEncodingException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 2 with MessageEncodingException

use of org.opensaml.ws.message.encoder.MessageEncodingException in project MaxKey by dromara.

the class WebServicePostEncoder method encodeMsgContext.

@SuppressWarnings("rawtypes")
public VelocityContext encodeMsgContext(MessageContext messageContext) throws MessageEncodingException {
    SAMLMessageContext samlMsgCtx = (SAMLMessageContext) messageContext;
    SAMLObject outboundMessage = samlMsgCtx.getOutboundSAMLMessage();
    if (outboundMessage == null) {
        throw new MessageEncodingException("No outbound SAML message contained in message context");
    }
    signMessage(samlMsgCtx);
    samlMsgCtx.setOutboundMessage(outboundMessage);
    return encodeMsgContext(samlMsgCtx);
}
Also used : SAMLMessageContext(org.opensaml.common.binding.SAMLMessageContext) SAMLObject(org.opensaml.common.SAMLObject) MessageEncodingException(org.opensaml.ws.message.encoder.MessageEncodingException)

Example 3 with MessageEncodingException

use of org.opensaml.ws.message.encoder.MessageEncodingException in project MaxKey by dromara.

the class WebServicePostEncoder method generateSignature.

/**
 * Generates the signature over the string of concatenated form control data
 * as indicated by the SimpleSign spec.
 *
 * @param signingCredential
 *            credential that will be used to sign
 * @param algorithmURI
 *            algorithm URI of the signing credential
 * @param formData
 *            form control data to be signed
 *
 * @return base64 encoded signature of form control data
 *
 * @throws MessageEncodingException
 *             there is an error computing the signature
 */
protected String generateSignature(Credential signingCredential, String algorithmURI, String formData) throws MessageEncodingException {
    log.debug(String.format("Generating signature with key type '%s', algorithm URI '%s' over form control string '%s'", SecurityHelper.extractSigningKey(signingCredential).getAlgorithm(), algorithmURI, formData));
    String b64Signature = null;
    try {
        byte[] rawSignature = SigningUtil.signWithURI(signingCredential, algorithmURI, formData.getBytes("UTF-8"));
        b64Signature = Base64.encodeBytes(rawSignature, Base64.DONT_BREAK_LINES);
        log.debug("Generated digital signature value (base64-encoded) {}", b64Signature);
    } catch (SecurityException e) {
        log.error("Error during URL signing process", e);
        throw new MessageEncodingException("Unable to sign form control string", e);
    } catch (UnsupportedEncodingException e) {
    // UTF-8 encoding is required to be supported by all JVMs
    }
    return b64Signature;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) SecurityException(org.opensaml.xml.security.SecurityException) MessageEncodingException(org.opensaml.ws.message.encoder.MessageEncodingException)

Example 4 with MessageEncodingException

use of org.opensaml.ws.message.encoder.MessageEncodingException in project MaxKey by dromara.

the class WebServicePostEncoder method encodeMsgContext.

/**
 * Base64 and POST encodes the outbound message and writes it to the
 * outbound transport.
 *
 * @param messageContext
 *            current message context
 * @param endpointURL
 *            endpoint URL to encode message to
 *
 * @throws MessageEncodingException
 *             thrown if there is a problem encoding the message
 */
@SuppressWarnings("rawtypes")
protected VelocityContext encodeMsgContext(SAMLMessageContext messageContext) throws MessageEncodingException {
    try {
        VelocityContext context = new VelocityContext();
        populateVelocityContext(context, messageContext);
        return context;
    } catch (Exception e) {
        log.error("Error invoking velocity template", e);
        throw new MessageEncodingException("Error creating output document", e);
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) MessageEncodingException(org.opensaml.ws.message.encoder.MessageEncodingException) MarshallingException(org.opensaml.xml.io.MarshallingException) MessageEncodingException(org.opensaml.ws.message.encoder.MessageEncodingException) SecurityException(org.opensaml.xml.security.SecurityException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with MessageEncodingException

use of org.opensaml.ws.message.encoder.MessageEncodingException in project uaa by cloudfoundry.

the class IdpSamlAuthenticationSuccessHandler method onAuthenticationSuccess.

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws ServletException {
    SAMLMessageContext context = ((UaaAuthentication) authentication).getSamlMessageContext();
    IdpExtendedMetadata extendedMetadata = null;
    try {
        extendedMetadata = (IdpExtendedMetadata) metadataManager.getExtendedMetadata(context.getLocalEntityId());
    } catch (MetadataProviderException e) {
        throw new ServletException("Failed to obtain local SAML IdP extended metadata.", e);
    }
    try {
        populatePeerContext(context);
    } catch (MetadataProviderException e) {
        throw new ServletException("Failed to populate peer SAML SP context.", e);
    }
    try {
        IdpWebSSOProfileOptions options = new IdpWebSSOProfileOptions();
        options.setAssertionsSigned(extendedMetadata.isAssertionsSigned());
        options.setAssertionTimeToLiveSeconds(extendedMetadata.getAssertionTimeToLiveSeconds());
        idpWebSsoProfile.sendResponse(authentication, context, options);
    } catch (SAMLException e) {
        LOGGER.debug("Incoming SAML message is invalid.", e);
        throw new AuthenticationServiceException("Incoming SAML message is invalid.", e);
    } catch (MetadataProviderException e) {
        LOGGER.debug("Error determining metadata contracts.", e);
        throw new AuthenticationServiceException("Error determining metadata contracts.", e);
    } catch (MessageEncodingException e) {
        LOGGER.debug("Error decoding incoming SAML message.", e);
        throw new AuthenticationServiceException("Error encoding outgoing SAML message.", e);
    } catch (MarshallingException | SecurityException | SignatureException e) {
        LOGGER.debug("Error signing SAML assertion.", e);
        throw new AuthenticationServiceException("Error signing SAML assertion.", e);
    }
}
Also used : SecurityException(org.opensaml.xml.security.SecurityException) SignatureException(org.opensaml.xml.signature.SignatureException) MessageEncodingException(org.opensaml.ws.message.encoder.MessageEncodingException) SAMLException(org.opensaml.common.SAMLException) AuthenticationServiceException(org.springframework.security.authentication.AuthenticationServiceException) UaaAuthentication(org.cloudfoundry.identity.uaa.authentication.UaaAuthentication) ServletException(javax.servlet.ServletException) SAMLMessageContext(org.springframework.security.saml.context.SAMLMessageContext) MarshallingException(org.opensaml.xml.io.MarshallingException) MetadataProviderException(org.opensaml.saml2.metadata.provider.MetadataProviderException)

Aggregations

MessageEncodingException (org.opensaml.ws.message.encoder.MessageEncodingException)10 SecurityException (org.opensaml.xml.security.SecurityException)5 MarshallingException (org.opensaml.xml.io.MarshallingException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)3 SAMLException (org.opensaml.common.SAMLException)3 MetadataProviderException (org.opensaml.saml2.metadata.provider.MetadataProviderException)3 SAMLMessageContext (org.springframework.security.saml.context.SAMLMessageContext)3 ServletException (javax.servlet.ServletException)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2 SignatureException (org.opensaml.xml.signature.SignatureException)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Optional (java.util.Optional)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 QName (javax.xml.namespace.QName)1 VelocityContext (org.apache.velocity.VelocityContext)1 UaaAuthentication (org.cloudfoundry.identity.uaa.authentication.UaaAuthentication)1 SigninPrincipal (org.maxkey.authn.SigninPrincipal)1 AuthnRequestInfo (org.maxkey.authz.saml.common.AuthnRequestInfo)1