use of org.apache.cxf.ws.security.sts.provider.STSException in project ddf by codice.
the class StaticStsProperties method configureProperties.
/**
* Load the CallbackHandler, Crypto objects, if necessary.
*/
public void configureProperties() throws STSException {
if (signatureCrypto == null && signatureCryptoProperties != null) {
Properties sigProperties = null;
if (signatureCryptoProperties instanceof Properties) {
sigProperties = (Properties) signatureCryptoProperties;
} else {
ResourceManager resourceManager = getResourceManager();
URL url = SecurityUtils.loadResource(resourceManager, signatureCryptoProperties);
sigProperties = SecurityUtils.loadProperties(url);
}
if (sigProperties == null) {
LOG.fine("Cannot load signature properties using: " + signatureCryptoProperties);
throw new STSException("Configuration error: cannot load signature properties");
}
try {
signatureCrypto = CryptoFactory.getInstance(sigProperties);
} catch (WSSecurityException ex) {
LOG.fine("Error in loading the signature Crypto object: " + ex.getMessage());
throw new STSException(ex.getMessage());
}
}
if (encryptionCrypto == null && encryptionCryptoProperties != null) {
Properties encrProperties = null;
if (encryptionCryptoProperties instanceof Properties) {
encrProperties = (Properties) encryptionCryptoProperties;
} else {
ResourceManager resourceManager = getResourceManager();
URL url = SecurityUtils.loadResource(resourceManager, encryptionCryptoProperties);
encrProperties = SecurityUtils.loadProperties(url);
}
if (encrProperties == null) {
LOG.fine("Cannot load encryption properties using: " + encryptionCryptoProperties);
throw new STSException("Configuration error: cannot load encryption properties");
}
try {
encryptionCrypto = CryptoFactory.getInstance(encrProperties);
} catch (WSSecurityException ex) {
LOG.fine("Error in loading the encryption Crypto object: " + ex.getMessage());
throw new STSException(ex.getMessage());
}
}
if (callbackHandler == null && callbackHandlerClass != null) {
try {
callbackHandler = SecurityUtils.getCallbackHandler(callbackHandlerClass);
if (callbackHandler == null) {
LOG.fine("Cannot load CallbackHandler using: " + callbackHandlerClass);
throw new STSException("Configuration error: cannot load callback handler");
}
} catch (Exception ex) {
LOG.fine("Error in loading the callback handler: " + ex.getMessage());
throw new STSException(ex.getMessage());
}
}
WSSConfig.init();
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class IssueOnbehalfofUnitTest method testIssueSaml2TokenOnBehalfOfSaml2SymmetricHOK.
/**
* Test to successfully issue a SAML 2 token on-behalf-of a SAML 2 Symmetric HOK token
*/
@org.junit.Test
public void testIssueSaml2TokenOnBehalfOfSaml2SymmetricHOK() throws Exception {
TokenIssueOperation issueOperation = new TokenIssueOperation();
// Add Token Provider
issueOperation.setTokenProviders(Collections.singletonList(new SAMLTokenProvider()));
// Add Token Validator
issueOperation.setTokenValidators(Collections.singletonList(new SAMLTokenValidator()));
// 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);
// Get a SAML Token via the SAMLTokenProvider
CallbackHandler callbackHandler = new PasswordCallbackHandler();
Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, crypto, "mystskey", callbackHandler, null, STSConstants.SYMMETRIC_KEY_TYPE);
Document doc = samlToken.getOwnerDocument();
samlToken = (Element) doc.appendChild(samlToken);
OnBehalfOfType onbehalfof = new OnBehalfOfType();
onbehalfof.setAny(samlToken);
JAXBElement<OnBehalfOfType> onbehalfofType = new JAXBElement<OnBehalfOfType>(QNameConstants.ON_BEHALF_OF, OnBehalfOfType.class, onbehalfof);
request.getAny().add(onbehalfofType);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
// This should fail as the default DelegationHandler does not allow HolderOfKey
try {
issueOperation.issue(request, null, msgCtx);
fail("Failure expected as HolderOfKey is not allowed by default");
} catch (STSException ex) {
// expected
}
TokenDelegationHandler delegationHandler = new HOKDelegationHandler();
issueOperation.setDelegationHandlers(Collections.singletonList(delegationHandler));
RequestSecurityTokenResponseCollectionType response = issueOperation.issue(request, null, msgCtx);
List<RequestSecurityTokenResponseType> securityTokenResponse = response.getRequestSecurityTokenResponse();
assertFalse(securityTokenResponse.isEmpty());
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class IssueOnbehalfofUnitTest method testIssueSaml2TokenOnBehalfOfSaml1PublicKeyHOK.
/**
* Test to successfully issue a SAML 2 token on-behalf-of a SAML 1 PublicKey HOK token
*/
@org.junit.Test
public void testIssueSaml2TokenOnBehalfOfSaml1PublicKeyHOK() throws Exception {
TokenIssueOperation issueOperation = new TokenIssueOperation();
// Add Token Provider
issueOperation.setTokenProviders(Collections.singletonList(new SAMLTokenProvider()));
// Add Token Validator
issueOperation.setTokenValidators(Collections.singletonList(new SAMLTokenValidator()));
// 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);
// Get a SAML Token via the SAMLTokenProvider
CallbackHandler callbackHandler = new PasswordCallbackHandler();
Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML_TOKEN_TYPE, crypto, "mystskey", callbackHandler, null, STSConstants.PUBLIC_KEY_KEYTYPE);
Document doc = samlToken.getOwnerDocument();
samlToken = (Element) doc.appendChild(samlToken);
OnBehalfOfType onbehalfof = new OnBehalfOfType();
onbehalfof.setAny(samlToken);
JAXBElement<OnBehalfOfType> onbehalfofType = new JAXBElement<OnBehalfOfType>(QNameConstants.ON_BEHALF_OF, OnBehalfOfType.class, onbehalfof);
request.getAny().add(onbehalfofType);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
// This should fail as the default DelegationHandler does not allow HolderOfKey
try {
issueOperation.issue(request, null, msgCtx);
fail("Failure expected as HolderOfKey is not allowed by default");
} catch (STSException ex) {
// expected
}
TokenDelegationHandler delegationHandler = new HOKDelegationHandler();
issueOperation.setDelegationHandlers(Collections.singletonList(delegationHandler));
RequestSecurityTokenResponseCollectionType response = issueOperation.issue(request, null, msgCtx);
List<RequestSecurityTokenResponseType> securityTokenResponse = response.getRequestSecurityTokenResponse();
assertFalse(securityTokenResponse.isEmpty());
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class IssueOnbehalfofUnitTest method testSaml1AudienceRestriction.
/**
* Test to unsuccessfully issue a SAML 2 token on-behalf-of a SAML 1 token. The
* problem is that the Audience Restriction URLs in the original token do not
* match the AppliesTo address.
*/
@org.junit.Test
public void testSaml1AudienceRestriction() throws Exception {
TokenIssueOperation issueOperation = new TokenIssueOperation();
// Add Token Provider
issueOperation.setTokenProviders(Collections.singletonList(new SAMLTokenProvider()));
// Add Token Validator
issueOperation.setTokenValidators(Collections.singletonList(new SAMLTokenValidator()));
TokenDelegationHandler delegationHandler = new SAMLDelegationHandler();
issueOperation.setDelegationHandlers(Collections.singletonList(delegationHandler));
// 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);
// Get a SAML Token via the SAMLTokenProvider
CallbackHandler callbackHandler = new PasswordCallbackHandler();
Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML_TOKEN_TYPE, crypto, "mystskey", callbackHandler);
Document doc = samlToken.getOwnerDocument();
samlToken = (Element) doc.appendChild(samlToken);
OnBehalfOfType onbehalfof = new OnBehalfOfType();
onbehalfof.setAny(samlToken);
JAXBElement<OnBehalfOfType> onbehalfofType = new JAXBElement<OnBehalfOfType>(QNameConstants.ON_BEHALF_OF, OnBehalfOfType.class, onbehalfof);
request.getAny().add(onbehalfofType);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
// Issue a token - this should work
issueOperation.issue(request, null, msgCtx);
request.getAny().add(createAppliesToElement("http://dummy-service.com/dummy2"));
// This should fail
try {
issueOperation.issue(request, null, msgCtx);
fail("Failure expected due to AudienceRestriction");
} catch (STSException ex) {
// expected
}
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class IssueOnbehalfofUnitTest method testIssueSaml2TokenOnBehalfOfInvalidUsernameToken.
/**
* Test to successfully issue a SAML 2 token on-behalf-of a UsernameToken
*/
@org.junit.Test
public void testIssueSaml2TokenOnBehalfOfInvalidUsernameToken() throws Exception {
TokenIssueOperation issueOperation = new TokenIssueOperation();
// Add Token Provider
issueOperation.setTokenProviders(Collections.singletonList(new SAMLTokenProvider()));
// Add Token Validator
issueOperation.setTokenValidators(Collections.singletonList(new UsernameTokenValidator()));
// 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);
// Create a UsernameToken without password
JAXBElement<UsernameTokenType> usernameTokenType = createUsernameToken("alice", null);
OnBehalfOfType onbehalfof = new OnBehalfOfType();
onbehalfof.setAny(usernameTokenType);
JAXBElement<OnBehalfOfType> onbehalfofType = new JAXBElement<OnBehalfOfType>(QNameConstants.ON_BEHALF_OF, OnBehalfOfType.class, onbehalfof);
request.getAny().add(onbehalfofType);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
TokenDelegationHandler delegationHandler = new UsernameTokenDelegationHandler();
issueOperation.setDelegationHandlers(Collections.singletonList(delegationHandler));
// Issue a token - this will fail as the UsernameToken validation fails
try {
issueOperation.issue(request, null, msgCtx);
fail("Failure expected as no principal is available to create SAML assertion");
} catch (STSException ex) {
// expected
}
}
Aggregations