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);
}
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;
}
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));
}
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));
}
Aggregations