Search in sources :

Example 1 with ReferenceType

use of org.apache.cxf.ws.security.sts.provider.model.secext.ReferenceType in project cxf by apache.

the class AbstractOperation method createRequestedReference.

/**
 * Create a RequestedReferenceType object using a TokenReference object
 */
protected static RequestedReferenceType createRequestedReference(TokenReference tokenReference, boolean attached) {
    RequestedReferenceType requestedReferenceType = QNameConstants.WS_TRUST_FACTORY.createRequestedReferenceType();
    SecurityTokenReferenceType securityTokenReferenceType = QNameConstants.WSSE_FACTORY.createSecurityTokenReferenceType();
    // TokenType
    String tokenType = tokenReference.getWsse11TokenType();
    if (tokenType != null) {
        securityTokenReferenceType.getOtherAttributes().put(TOKEN_TYPE, tokenType);
    }
    if (tokenReference.isUseKeyIdentifier()) {
        String identifier = XMLUtils.getIDFromReference(tokenReference.getIdentifier());
        KeyIdentifierType keyIdentifierType = QNameConstants.WSSE_FACTORY.createKeyIdentifierType();
        keyIdentifierType.setValue(identifier);
        String valueType = tokenReference.getWsseValueType();
        if (valueType != null) {
            keyIdentifierType.setValueType(valueType);
        }
        JAXBElement<KeyIdentifierType> keyIdentifier = QNameConstants.WSSE_FACTORY.createKeyIdentifier(keyIdentifierType);
        securityTokenReferenceType.getAny().add(keyIdentifier);
    } else if (tokenReference.isUseDirectReference()) {
        String identifier = tokenReference.getIdentifier();
        if (attached && identifier.charAt(0) != '#') {
            identifier = "#" + identifier;
        } else if (!attached && identifier.charAt(0) == '#') {
            identifier = identifier.substring(1);
        }
        ReferenceType referenceType = QNameConstants.WSSE_FACTORY.createReferenceType();
        referenceType.setURI(identifier);
        String valueType = tokenReference.getWsseValueType();
        if (valueType != null) {
            referenceType.setValueType(valueType);
        }
        JAXBElement<ReferenceType> reference = QNameConstants.WSSE_FACTORY.createReference(referenceType);
        securityTokenReferenceType.getAny().add(reference);
    }
    requestedReferenceType.setSecurityTokenReference(securityTokenReferenceType);
    return requestedReferenceType;
}
Also used : RequestedReferenceType(org.apache.cxf.ws.security.sts.provider.model.RequestedReferenceType) KeyIdentifierType(org.apache.cxf.ws.security.sts.provider.model.secext.KeyIdentifierType) SecurityTokenReferenceType(org.apache.cxf.ws.security.sts.provider.model.secext.SecurityTokenReferenceType) JAXBElement(javax.xml.bind.JAXBElement) SecurityTokenReferenceType(org.apache.cxf.ws.security.sts.provider.model.secext.SecurityTokenReferenceType) RequestedReferenceType(org.apache.cxf.ws.security.sts.provider.model.RequestedReferenceType) ReferenceType(org.apache.cxf.ws.security.sts.provider.model.secext.ReferenceType)

Example 2 with ReferenceType

use of org.apache.cxf.ws.security.sts.provider.model.secext.ReferenceType in project cxf by apache.

the class RequestParser method fetchTokenElementFromReference.

/**
 * Method to fetch token from the SecurityTokenReference
 */
private static Element fetchTokenElementFromReference(Object targetToken, Map<String, Object> messageContext) {
    // Get the reference URI
    String referenceURI = null;
    if (targetToken instanceof Element) {
        Element tokenElement = (Element) targetToken;
        NodeList refList = tokenElement.getElementsByTagNameNS(STSConstants.WSSE_EXT_04_01, "Reference");
        if (refList.getLength() == 0) {
            throw new STSException("Cannot find Reference element in the SecurityTokenReference.", STSException.REQUEST_FAILED);
        }
        referenceURI = refList.item(0).getNodeValue();
    } else if (targetToken instanceof SecurityTokenReferenceType) {
        Iterator<?> iterator = ((SecurityTokenReferenceType) targetToken).getAny().iterator();
        while (iterator.hasNext()) {
            JAXBElement<?> jaxbElement = (JAXBElement<?>) iterator.next();
            if (jaxbElement.getValue() instanceof ReferenceType) {
                referenceURI = ((ReferenceType) jaxbElement.getValue()).getURI();
            }
        }
    }
    if (LOG.isLoggable(Level.FINE)) {
        LOG.fine("Reference URI found " + referenceURI);
    }
    if (referenceURI == null) {
        LOG.log(Level.WARNING, "No Reference URI was received");
        throw new STSException("An unknown element was received", STSException.BAD_REQUEST);
    }
    // Find processed token corresponding to the URI
    referenceURI = XMLUtils.getIDFromReference(referenceURI);
    final List<WSHandlerResult> handlerResults = CastUtils.cast((List<?>) messageContext.get(WSHandlerConstants.RECV_RESULTS));
    if (handlerResults != null && !handlerResults.isEmpty()) {
        WSHandlerResult handlerResult = handlerResults.get(0);
        List<WSSecurityEngineResult> engineResults = handlerResult.getResults();
        for (WSSecurityEngineResult engineResult : engineResults) {
            Integer actInt = (Integer) engineResult.get(WSSecurityEngineResult.TAG_ACTION);
            String id = (String) engineResult.get(WSSecurityEngineResult.TAG_ID);
            if (referenceURI.equals(id)) {
                Element tokenElement = (Element) engineResult.get(WSSecurityEngineResult.TAG_TOKEN_ELEMENT);
                if (tokenElement == null) {
                    throw new STSException("Cannot retrieve token from reference", STSException.INVALID_REQUEST);
                }
                return tokenElement;
            } else if (actInt == WSConstants.SCT) {
                // Need to check special case of SecurityContextToken Identifier separately
                SecurityContextToken sct = (SecurityContextToken) engineResult.get(WSSecurityEngineResult.TAG_SECURITY_CONTEXT_TOKEN);
                if (referenceURI.equals(sct.getIdentifier())) {
                    return sct.getElement();
                }
            }
        }
    }
    throw new STSException("Cannot retreive token from reference", STSException.REQUEST_FAILED);
}
Also used : JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) NodeList(org.w3c.dom.NodeList) STSException(org.apache.cxf.ws.security.sts.provider.STSException) JAXBElement(javax.xml.bind.JAXBElement) WSHandlerResult(org.apache.wss4j.dom.handler.WSHandlerResult) WSSecurityEngineResult(org.apache.wss4j.dom.engine.WSSecurityEngineResult) SecurityTokenReferenceType(org.apache.cxf.ws.security.sts.provider.model.secext.SecurityTokenReferenceType) ReferenceType(org.apache.cxf.ws.security.sts.provider.model.secext.ReferenceType) SecurityContextToken(org.apache.wss4j.dom.message.token.SecurityContextToken) Iterator(java.util.Iterator) SecurityTokenReferenceType(org.apache.cxf.ws.security.sts.provider.model.secext.SecurityTokenReferenceType)

Aggregations

JAXBElement (javax.xml.bind.JAXBElement)2 ReferenceType (org.apache.cxf.ws.security.sts.provider.model.secext.ReferenceType)2 SecurityTokenReferenceType (org.apache.cxf.ws.security.sts.provider.model.secext.SecurityTokenReferenceType)2 Iterator (java.util.Iterator)1 STSException (org.apache.cxf.ws.security.sts.provider.STSException)1 RequestedReferenceType (org.apache.cxf.ws.security.sts.provider.model.RequestedReferenceType)1 KeyIdentifierType (org.apache.cxf.ws.security.sts.provider.model.secext.KeyIdentifierType)1 WSSecurityEngineResult (org.apache.wss4j.dom.engine.WSSecurityEngineResult)1 WSHandlerResult (org.apache.wss4j.dom.handler.WSHandlerResult)1 SecurityContextToken (org.apache.wss4j.dom.message.token.SecurityContextToken)1 Element (org.w3c.dom.Element)1 NodeList (org.w3c.dom.NodeList)1