use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class IssueSamlUnitTest method testIssueSaml1PublicKeyToken.
/**
* Test to successfully issue a Saml 1.1 PublicKey token.
*/
@org.junit.Test
public void testIssueSaml1PublicKeyToken() throws Exception {
TokenIssueOperation issueOperation = new TokenIssueOperation();
// Add Token Provider
issueOperation.setTokenProviders(Collections.singletonList(new SAMLTokenProvider()));
// 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_SAML_TOKEN_TYPE);
request.getAny().add(tokenType);
JAXBElement<String> keyType = new JAXBElement<String>(QNameConstants.KEY_TYPE, String.class, STSConstants.PUBLIC_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));
// Issue a token
try {
issueOperation.issue(request, principal, msgCtx);
fail("Failure expected on no certificate");
} catch (STSException ex) {
// expected failure on no certificate
}
// Now add UseKey
UseKeyType useKey = createUseKey(crypto, "myclientkey");
JAXBElement<UseKeyType> useKeyType = new JAXBElement<>(QNameConstants.USE_KEY, UseKeyType.class, useKey);
request.getAny().add(useKeyType);
RequestSecurityTokenResponseCollectionType response = issueOperation.issue(request, principal, msgCtx);
List<RequestSecurityTokenResponseType> securityTokenResponse = response.getRequestSecurityTokenResponse();
assertFalse(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(SAML1Constants.CONF_BEARER));
assertTrue(tokenString.contains(SAML1Constants.CONF_HOLDER_KEY));
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class IssueSamlUnitTest method testUseKey.
/**
* Test to UseKey validation
*/
@org.junit.Test
public void testUseKey() throws Exception {
TokenIssueOperation issueOperation = new TokenIssueOperation();
// Add Token Provider
issueOperation.setTokenProviders(Collections.singletonList(new SAMLTokenProvider()));
// 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.PUBLIC_KEY_KEYTYPE);
request.getAny().add(keyType);
UseKeyType useKey = createUseKey(crypto, "myclientkey");
JAXBElement<UseKeyType> useKeyType = new JAXBElement<>(QNameConstants.USE_KEY, UseKeyType.class, useKey);
request.getAny().add(useKeyType);
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));
// Issue a token
RequestSecurityTokenResponseCollectionType response = issueOperation.issue(request, principal, msgCtx);
List<RequestSecurityTokenResponseType> securityTokenResponse = response.getRequestSecurityTokenResponse();
assertFalse(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();
}
}
String tokenString = DOM2Writer.nodeToString(assertion);
assertTrue(tokenString.contains("AttributeStatement"));
assertTrue(tokenString.contains("alice"));
assertTrue(tokenString.contains(SAML2Constants.CONF_HOLDER_KEY));
// Now remove the UseKey + send a non-trusted UseKey certificate
request.getAny().remove(useKeyType);
Properties properties = new Properties();
properties.put("org.apache.wss4j.crypto.provider", "org.apache.wss4j.common.crypto.Merlin");
properties.put("org.apache.wss4j.crypto.merlin.keystore.password", "evespass");
properties.put("org.apache.wss4j.crypto.merlin.keystore.file", "eve.jks");
useKey = createUseKey(CryptoFactory.getInstance(properties), "eve");
useKeyType = new JAXBElement<>(QNameConstants.USE_KEY, UseKeyType.class, useKey);
request.getAny().add(useKeyType);
// This should fail as the UseKey certificate is not trusted
try {
issueOperation.issue(request, principal, msgCtx);
fail("Failure expected as the UseKey certificate is not trusted");
} catch (STSException ex) {
// expected
}
// Now allow non-trusted UseKey certificates...
stsProperties.setValidateUseKey(false);
response = issueOperation.issue(request, principal, msgCtx);
securityTokenResponse = response.getRequestSecurityTokenResponse();
assertFalse(securityTokenResponse.isEmpty());
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class IssueJWTOnbehalfofUnitTest method testIssueJWTTokenOnBehalfOfUsernameToken.
/**
* Test to successfully issue a JWT token on-behalf-of a UsernameToken
*/
@org.junit.Test
public void testIssueJWTTokenOnBehalfOfUsernameToken() throws Exception {
TokenIssueOperation issueOperation = new TokenIssueOperation();
// Add Token Provider
issueOperation.setTokenProviders(Collections.singletonList(new JWTTokenProvider()));
// 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, JWTTokenProvider.JWT_TOKEN_TYPE);
request.getAny().add(tokenType);
// Create a UsernameToken
JAXBElement<UsernameTokenType> usernameTokenType = createUsernameToken("alice", "clarinet");
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);
// This should fail as the default DelegationHandler does not allow UsernameTokens
try {
issueOperation.issue(request, null, msgCtx);
fail("Failure expected as UsernameTokens are not accepted for OnBehalfOf by default");
} catch (STSException ex) {
// expected
}
TokenDelegationHandler delegationHandler = new UsernameTokenDelegationHandler();
issueOperation.setDelegationHandlers(Collections.singletonList(delegationHandler));
RequestSecurityTokenResponseCollectionType response = issueOperation.issue(request, null, msgCtx);
List<RequestSecurityTokenResponseType> securityTokenResponse = response.getRequestSecurityTokenResponse();
assertFalse(securityTokenResponse.isEmpty());
// Test the generated token.
Element token = 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();
token = (Element) rstType.getAny();
break;
}
}
assertNotNull(token);
// Validate the token
JwsJwtCompactConsumer jwtConsumer = new JwsJwtCompactConsumer(token.getTextContent());
JwtToken jwt = jwtConsumer.getJwtToken();
Assert.assertEquals("alice", jwt.getClaim(JwtConstants.CLAIM_SUBJECT));
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class ValidateUnitTest method testNoToken.
/**
* Test that calls Validate without a ValidateTarget
*/
@org.junit.Test
public void testNoToken() throws Exception {
TokenValidateOperation validateOperation = new TokenValidateOperation();
// Add Token Validator
validateOperation.setTokenValidators(Collections.singletonList(new DummyTokenValidator()));
// Add STSProperties object
STSPropertiesMBean stsProperties = new StaticSTSProperties();
validateOperation.setStsProperties(stsProperties);
// Mock up a request
RequestSecurityTokenType request = new RequestSecurityTokenType();
JAXBElement<String> tokenType = new JAXBElement<String>(QNameConstants.TOKEN_TYPE, String.class, STSConstants.STATUS);
request.getAny().add(tokenType);
// Mock up message context
MessageImpl msg = new MessageImpl();
WrappedMessageContext msgCtx = new WrappedMessageContext(msg);
// Validate a token
try {
validateOperation.validate(request, null, msgCtx);
fail("Failure expected when no element is presented for validation");
} catch (STSException ex) {
// expected
}
}
use of org.apache.cxf.ws.security.sts.provider.STSException in project cxf by apache.
the class DummyTokenProvider method createToken.
public TokenProviderResponse createToken(TokenProviderParameters tokenParameters) {
try {
Document doc = DOMUtils.getEmptyDocument();
// Mock up a dummy BinarySecurityToken
String id = "BST-1234";
BinarySecurity bst = new BinarySecurity(doc);
bst.addWSSENamespace();
bst.addWSUNamespace();
bst.setID(id);
bst.setValueType(TOKEN_TYPE);
bst.setEncodingType(BASE64_NS);
bst.setToken("12345678".getBytes());
TokenProviderResponse response = new TokenProviderResponse();
response.setToken(bst.getElement());
response.setTokenId(id);
if (tokenParameters.isEncryptToken()) {
Element el = TokenProviderUtils.encryptToken(bst.getElement(), response.getTokenId(), tokenParameters.getStsProperties(), tokenParameters.getEncryptionProperties(), tokenParameters.getKeyRequirements(), tokenParameters.getMessageContext());
response.setToken(el);
} else {
response.setToken(bst.getElement());
}
return response;
} catch (Exception e) {
throw new STSException("Can't serialize SAML assertion", e, STSException.REQUEST_FAILED);
}
}
Aggregations