Search in sources :

Example 1 with BinarySecretType

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

the class TokenIssueOperation method constructSecretToken.

/**
 * Construct a token containing the secret to return to the client. The secret is returned in a
 * BinarySecretType JAXBElement.
 */
private Object constructSecretToken(byte[] secret, EncryptionProperties encryptionProperties, KeyRequirements keyRequirements) throws WSSecurityException {
    /*if (encryptIssuedToken) {
            return encryptSecret(secret, encryptionProperties, keyRequirements);
        } else {
        */
    BinarySecretType binarySecretType = QNameConstants.WS_TRUST_FACTORY.createBinarySecretType();
    String nonce = "http://docs.oasis-open.org/ws-sx/ws-trust/200512/Nonce";
    binarySecretType.setType(nonce);
    binarySecretType.setValue(secret);
    return QNameConstants.WS_TRUST_FACTORY.createBinarySecret(binarySecretType);
}
Also used : BinarySecretType(org.apache.cxf.ws.security.sts.provider.model.BinarySecretType)

Example 2 with BinarySecretType

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

the class RequestParser method parseEntropy.

/**
 * Parse an Entropy object
 * @param entropy an Entropy object
 * @param stsProperties A STSPropertiesMBean object used to decrypt an EncryptedKey
 */
private static Entropy parseEntropy(EntropyType entropyType, STSPropertiesMBean stsProperties) throws STSException {
    for (Object entropyObject : entropyType.getAny()) {
        if (entropyObject instanceof JAXBElement<?>) {
            JAXBElement<?> entropyObjectJaxb = (JAXBElement<?>) entropyObject;
            if (QNameConstants.BINARY_SECRET.equals(entropyObjectJaxb.getName())) {
                BinarySecretType binarySecretType = (BinarySecretType) entropyObjectJaxb.getValue();
                LOG.fine("Found BinarySecret Entropy type");
                Entropy entropy = new Entropy();
                BinarySecret binarySecret = new BinarySecret();
                binarySecret.setBinarySecretType(binarySecretType.getType());
                binarySecret.setBinarySecretValue(binarySecretType.getValue());
                entropy.setBinarySecret(binarySecret);
                return entropy;
            } else if (LOG.isLoggable(Level.FINE)) {
                LOG.fine("Unsupported Entropy type: " + entropyObjectJaxb.getName());
            }
        } else if (entropyObject instanceof Element && "EncryptedKey".equals(((Element) entropyObject).getLocalName())) {
            EncryptedKeyProcessor processor = new EncryptedKeyProcessor();
            Element entropyElement = (Element) entropyObject;
            RequestData requestData = new RequestData();
            requestData.setDecCrypto(stsProperties.getSignatureCrypto());
            requestData.setCallbackHandler(stsProperties.getCallbackHandler());
            requestData.setWssConfig(WSSConfig.getNewInstance());
            requestData.setWsDocInfo(new WSDocInfo(entropyElement.getOwnerDocument()));
            try {
                List<WSSecurityEngineResult> results = processor.handleToken(entropyElement, requestData);
                Entropy entropy = new Entropy();
                entropy.setDecryptedKey((byte[]) results.get(0).get(WSSecurityEngineResult.TAG_SECRET));
                return entropy;
            } catch (WSSecurityException e) {
                LOG.log(Level.WARNING, "", e);
                throw new STSException(e.getMessage(), e, STSException.INVALID_REQUEST);
            }
        } else {
            LOG.log(Level.WARNING, "An unknown element was received");
            throw new STSException("An unknown element was received", STSException.BAD_REQUEST);
        }
    }
    return null;
}
Also used : WSDocInfo(org.apache.wss4j.dom.WSDocInfo) BinarySecretType(org.apache.cxf.ws.security.sts.provider.model.BinarySecretType) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) STSException(org.apache.cxf.ws.security.sts.provider.STSException) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) JAXBElement(javax.xml.bind.JAXBElement) EncryptedKeyProcessor(org.apache.wss4j.dom.processor.EncryptedKeyProcessor) RequestData(org.apache.wss4j.dom.handler.RequestData) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList)

Example 3 with BinarySecretType

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

the class IssueSamlUnitTest method testIssueSaml2SymmetricKeyTokenSecretKey.

/**
 * Test to successfully issue a Saml2 SymmetricKey token. Rather than using a Nonce as the
 * Entropy, a secret key is supplied by the client instead.
 */
@org.junit.Test
public void testIssueSaml2SymmetricKeyTokenSecretKey() throws Exception {
    TokenIssueOperation issueOperation = new TokenIssueOperation();
    // Add Token Provider
    List<TokenProvider> providerList = new ArrayList<>();
    providerList.add(new SAMLTokenProvider());
    issueOperation.setTokenProviders(providerList);
    // Add Service
    ServiceMBean service = new StaticService();
    service.setEndpoints(Collections.singletonList("http://dummy-service.com/dummy"));
    issueOperation.setServices(Collections.singletonList(service));
    // Add STSProperties object
    STSPropertiesMBean stsProperties = new StaticSTSProperties();
    Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
    stsProperties.setEncryptionCrypto(crypto);
    stsProperties.setSignatureCrypto(crypto);
    stsProperties.setEncryptionUsername("myservicekey");
    stsProperties.setSignatureUsername("mystskey");
    stsProperties.setCallbackHandler(new PasswordCallbackHandler());
    stsProperties.setIssuer("STS");
    issueOperation.setStsProperties(stsProperties);
    // Mock up a request
    RequestSecurityTokenType request = new RequestSecurityTokenType();
    JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, WSS4JConstants.WSS_SAML2_TOKEN_TYPE);
    request.getAny().add(tokenType);
    JAXBElement<String> keyType = new JAXBElement<String>(QNameConstants.KEY_TYPE, String.class, STSConstants.SYMMETRIC_KEY_KEYTYPE);
    request.getAny().add(keyType);
    request.getAny().add(createAppliesToElement("http://dummy-service.com/dummy"));
    // Mock up message context
    MessageImpl msg = new MessageImpl();
    WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
    Principal principal = new CustomTokenPrincipal("alice");
    msgCtx.put(SecurityContext.class.getName(), createSecurityContext(principal));
    // Now add Entropy
    BinarySecretType binarySecretType = new BinarySecretType();
    binarySecretType.setType(STSConstants.SYMMETRIC_KEY_TYPE);
    binarySecretType.setValue(WSSecurityUtil.generateNonce(256 / 8));
    JAXBElement<BinarySecretType> binarySecretTypeJaxb = new JAXBElement<BinarySecretType>(QNameConstants.BINARY_SECRET, BinarySecretType.class, binarySecretType);
    EntropyType entropyType = new EntropyType();
    entropyType.getAny().add(binarySecretTypeJaxb);
    JAXBElement<EntropyType> entropyJaxbType = new JAXBElement<EntropyType>(QNameConstants.ENTROPY, EntropyType.class, entropyType);
    request.getAny().add(entropyJaxbType);
    RequestSecurityTokenResponseCollectionType response = issueOperation.issue(request, principal, msgCtx);
    List<RequestSecurityTokenResponseType> securityTokenResponse = response.getRequestSecurityTokenResponse();
    assertTrue(!securityTokenResponse.isEmpty());
    // Test the generated token.
    Element assertion = null;
    for (Object tokenObject : securityTokenResponse.get(0).getAny()) {
        if (tokenObject instanceof JAXBElement<?> && REQUESTED_SECURITY_TOKEN.equals(((JAXBElement<?>) tokenObject).getName())) {
            RequestedSecurityTokenType rstType = (RequestedSecurityTokenType) ((JAXBElement<?>) tokenObject).getValue();
            assertion = (Element) rstType.getAny();
        }
    }
    assertNotNull(assertion);
    String tokenString = DOM2Writer.nodeToString(assertion);
    assertTrue(tokenString.contains("AttributeStatement"));
    assertTrue(tokenString.contains("alice"));
    assertFalse(tokenString.contains(SAML2Constants.CONF_BEARER));
    assertTrue(tokenString.contains(SAML2Constants.CONF_HOLDER_KEY));
}
Also used : ServiceMBean(org.apache.cxf.sts.service.ServiceMBean) BinarySecretType(org.apache.cxf.ws.security.sts.provider.model.BinarySecretType) RequestSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) StaticSTSProperties(org.apache.cxf.sts.StaticSTSProperties) RequestedSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType) StaticService(org.apache.cxf.sts.service.StaticService) RequestSecurityTokenResponseCollectionType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseCollectionType) CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) TokenProvider(org.apache.cxf.sts.token.provider.TokenProvider) SAMLTokenProvider(org.apache.cxf.sts.token.provider.SAMLTokenProvider) EntropyType(org.apache.cxf.ws.security.sts.provider.model.EntropyType) PasswordCallbackHandler(org.apache.cxf.sts.common.PasswordCallbackHandler) JAXBElement(javax.xml.bind.JAXBElement) Crypto(org.apache.wss4j.common.crypto.Crypto) SAMLTokenProvider(org.apache.cxf.sts.token.provider.SAMLTokenProvider) STSPropertiesMBean(org.apache.cxf.sts.STSPropertiesMBean) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) SecurityContext(org.apache.cxf.security.SecurityContext) MessageImpl(org.apache.cxf.message.MessageImpl) CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) Principal(java.security.Principal)

Example 4 with BinarySecretType

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

the class IssueSamlUnitTest method testIssueSaml2SymmetricKeyToken.

/**
 * Test to successfully issue a Saml2 SymmetricKey token.
 */
@org.junit.Test
public void testIssueSaml2SymmetricKeyToken() throws Exception {
    TokenIssueOperation issueOperation = new TokenIssueOperation();
    // Add Token Provider
    List<TokenProvider> providerList = new ArrayList<>();
    providerList.add(new SAMLTokenProvider());
    issueOperation.setTokenProviders(providerList);
    // Add Service
    ServiceMBean service = new StaticService();
    service.setEndpoints(Collections.singletonList("http://dummy-service.com/dummy"));
    issueOperation.setServices(Collections.singletonList(service));
    // Add STSProperties object
    STSPropertiesMBean stsProperties = new StaticSTSProperties();
    Crypto crypto = CryptoFactory.getInstance(getEncryptionProperties());
    stsProperties.setEncryptionCrypto(crypto);
    stsProperties.setSignatureCrypto(crypto);
    stsProperties.setEncryptionUsername("myservicekey");
    stsProperties.setSignatureUsername("mystskey");
    stsProperties.setCallbackHandler(new PasswordCallbackHandler());
    stsProperties.setIssuer("STS");
    issueOperation.setStsProperties(stsProperties);
    // Mock up a request
    RequestSecurityTokenType request = new RequestSecurityTokenType();
    JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, WSS4JConstants.WSS_SAML2_TOKEN_TYPE);
    request.getAny().add(tokenType);
    JAXBElement<String> keyType = new JAXBElement<String>(QNameConstants.KEY_TYPE, String.class, STSConstants.SYMMETRIC_KEY_KEYTYPE);
    request.getAny().add(keyType);
    JAXBElement<String> computedKey = new JAXBElement<String>(QNameConstants.COMPUTED_KEY_ALGORITHM, String.class, STSConstants.COMPUTED_KEY_PSHA1);
    request.getAny().add(computedKey);
    request.getAny().add(createAppliesToElement("http://dummy-service.com/dummy"));
    // Mock up message context
    MessageImpl msg = new MessageImpl();
    WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
    Principal principal = new CustomTokenPrincipal("alice");
    msgCtx.put(SecurityContext.class.getName(), createSecurityContext(principal));
    // Now add Entropy
    BinarySecretType binarySecretType = new BinarySecretType();
    binarySecretType.setType(STSConstants.NONCE_TYPE);
    binarySecretType.setValue(WSSecurityUtil.generateNonce(256 / 8));
    JAXBElement<BinarySecretType> binarySecretTypeJaxb = new JAXBElement<BinarySecretType>(QNameConstants.BINARY_SECRET, BinarySecretType.class, binarySecretType);
    EntropyType entropyType = new EntropyType();
    entropyType.getAny().add(binarySecretTypeJaxb);
    JAXBElement<EntropyType> entropyJaxbType = new JAXBElement<EntropyType>(QNameConstants.ENTROPY, EntropyType.class, entropyType);
    request.getAny().add(entropyJaxbType);
    RequestSecurityTokenResponseCollectionType response = issueOperation.issue(request, principal, msgCtx);
    List<RequestSecurityTokenResponseType> securityTokenResponse = response.getRequestSecurityTokenResponse();
    assertTrue(!securityTokenResponse.isEmpty());
    // Test the generated token.
    Element assertion = null;
    for (Object tokenObject : securityTokenResponse.get(0).getAny()) {
        if (tokenObject instanceof JAXBElement<?> && REQUESTED_SECURITY_TOKEN.equals(((JAXBElement<?>) tokenObject).getName())) {
            RequestedSecurityTokenType rstType = (RequestedSecurityTokenType) ((JAXBElement<?>) tokenObject).getValue();
            assertion = (Element) rstType.getAny();
            break;
        }
    }
    assertNotNull(assertion);
    String tokenString = DOM2Writer.nodeToString(assertion);
    assertTrue(tokenString.contains("AttributeStatement"));
    assertTrue(tokenString.contains("alice"));
    assertFalse(tokenString.contains(SAML2Constants.CONF_BEARER));
    assertTrue(tokenString.contains(SAML2Constants.CONF_HOLDER_KEY));
}
Also used : ServiceMBean(org.apache.cxf.sts.service.ServiceMBean) BinarySecretType(org.apache.cxf.ws.security.sts.provider.model.BinarySecretType) RequestSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType) JAXBElement(javax.xml.bind.JAXBElement) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) RequestSecurityTokenResponseType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType) StaticSTSProperties(org.apache.cxf.sts.StaticSTSProperties) RequestedSecurityTokenType(org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType) StaticService(org.apache.cxf.sts.service.StaticService) RequestSecurityTokenResponseCollectionType(org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseCollectionType) CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) TokenProvider(org.apache.cxf.sts.token.provider.TokenProvider) SAMLTokenProvider(org.apache.cxf.sts.token.provider.SAMLTokenProvider) EntropyType(org.apache.cxf.ws.security.sts.provider.model.EntropyType) PasswordCallbackHandler(org.apache.cxf.sts.common.PasswordCallbackHandler) JAXBElement(javax.xml.bind.JAXBElement) Crypto(org.apache.wss4j.common.crypto.Crypto) SAMLTokenProvider(org.apache.cxf.sts.token.provider.SAMLTokenProvider) STSPropertiesMBean(org.apache.cxf.sts.STSPropertiesMBean) WrappedMessageContext(org.apache.cxf.jaxws.context.WrappedMessageContext) SecurityContext(org.apache.cxf.security.SecurityContext) MessageImpl(org.apache.cxf.message.MessageImpl) CustomTokenPrincipal(org.apache.wss4j.common.principal.CustomTokenPrincipal) Principal(java.security.Principal)

Aggregations

BinarySecretType (org.apache.cxf.ws.security.sts.provider.model.BinarySecretType)4 ArrayList (java.util.ArrayList)3 JAXBElement (javax.xml.bind.JAXBElement)3 Element (org.w3c.dom.Element)3 Principal (java.security.Principal)2 WrappedMessageContext (org.apache.cxf.jaxws.context.WrappedMessageContext)2 MessageImpl (org.apache.cxf.message.MessageImpl)2 SecurityContext (org.apache.cxf.security.SecurityContext)2 STSPropertiesMBean (org.apache.cxf.sts.STSPropertiesMBean)2 StaticSTSProperties (org.apache.cxf.sts.StaticSTSProperties)2 PasswordCallbackHandler (org.apache.cxf.sts.common.PasswordCallbackHandler)2 ServiceMBean (org.apache.cxf.sts.service.ServiceMBean)2 StaticService (org.apache.cxf.sts.service.StaticService)2 SAMLTokenProvider (org.apache.cxf.sts.token.provider.SAMLTokenProvider)2 TokenProvider (org.apache.cxf.sts.token.provider.TokenProvider)2 EntropyType (org.apache.cxf.ws.security.sts.provider.model.EntropyType)2 RequestSecurityTokenResponseCollectionType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseCollectionType)2 RequestSecurityTokenResponseType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenResponseType)2 RequestSecurityTokenType (org.apache.cxf.ws.security.sts.provider.model.RequestSecurityTokenType)2 RequestedSecurityTokenType (org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType)2