use of org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType in project cxf by apache.
the class ValidateJWTTransformationTest method testSAMLToJWTTransformation.
@org.junit.Test
public void testSAMLToJWTTransformation() throws Exception {
TokenValidateOperation validateOperation = new TokenValidateOperation();
// Add Token Validator
List<TokenValidator> validatorList = new ArrayList<>();
validatorList.add(new SAMLTokenValidator());
validateOperation.setTokenValidators(validatorList);
// Add Token Provider
List<TokenProvider> providerList = new ArrayList<>();
providerList.add(new JWTTokenProvider());
validateOperation.setTokenProviders(providerList);
// 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");
validateOperation.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 SAML Token
Element samlToken = createSAMLAssertion(WSS4JConstants.WSS_SAML2_TOKEN_TYPE, crypto, "mystskey", new PasswordCallbackHandler());
Document doc = samlToken.getOwnerDocument();
samlToken = (Element) doc.appendChild(samlToken);
ValidateTargetType validateTarget = new ValidateTargetType();
validateTarget.setAny(samlToken);
JAXBElement<ValidateTargetType> validateTargetType = new JAXBElement<ValidateTargetType>(QNameConstants.VALIDATE_TARGET, ValidateTargetType.class, validateTarget);
request.getAny().add(validateTargetType);
// 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));
// Validate a token
RequestSecurityTokenResponseType response = validateOperation.validate(request, principal, msgCtx);
assertTrue(validateResponse(response));
// Test the generated token.
Element token = null;
for (Object tokenObject : response.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);
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.model.RequestedSecurityTokenType in project cxf by apache.
the class CustomParameterTest method validateSAMLSecurityTokenResponse.
private Element validateSAMLSecurityTokenResponse(RequestSecurityTokenResponseType securityResponse, boolean saml2) throws Exception {
RequestedSecurityTokenType requestedSecurityToken = getRequestedSecurityToken(securityResponse);
assertNotNull(requestedSecurityToken);
// Process the token
List<WSSecurityEngineResult> results = processToken((Element) requestedSecurityToken.getAny());
assertTrue(results != null && results.size() == 1);
SamlAssertionWrapper assertion = (SamlAssertionWrapper) results.get(0).get(WSSecurityEngineResult.TAG_SAML_ASSERTION);
assertTrue(assertion != null);
if (saml2) {
assertTrue(assertion.getSaml2() != null && assertion.getSaml1() == null);
} else {
assertTrue(assertion.getSaml2() == null && assertion.getSaml1() != null);
}
assertTrue(assertion.isSigned());
return (Element) results.get(0).get(WSSecurityEngineResult.TAG_TOKEN_ELEMENT);
}
use of org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType in project cxf by apache.
the class STSRESTTest method validateSAMLSecurityTokenResponse.
private Element validateSAMLSecurityTokenResponse(RequestSecurityTokenResponseType securityResponse, boolean saml2) throws Exception {
RequestedSecurityTokenType requestedSecurityToken = getRequestedSecurityToken(securityResponse);
assertNotNull(requestedSecurityToken);
// Process the token
List<WSSecurityEngineResult> results = processToken((Element) requestedSecurityToken.getAny());
assertTrue(results != null && results.size() == 1);
SamlAssertionWrapper assertion = (SamlAssertionWrapper) results.get(0).get(WSSecurityEngineResult.TAG_SAML_ASSERTION);
assertTrue(assertion != null);
if (saml2) {
assertTrue(assertion.getSaml2() != null && assertion.getSaml1() == null);
} else {
assertTrue(assertion.getSaml2() == null && assertion.getSaml1() != null);
}
assertTrue(assertion.isSigned());
return (Element) results.get(0).get(WSSecurityEngineResult.TAG_TOKEN_ELEMENT);
}
use of org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType in project cxf by apache.
the class STSRESTTest method testIssueJWTTokenViaPOST.
@org.junit.Test
public void testIssueJWTTokenViaPOST() throws Exception {
SpringBusFactory bf = new SpringBusFactory();
URL busFile = STSRESTTest.class.getResource("cxf-client.xml");
Bus bus = bf.createBus(busFile.toString());
BusFactory.setDefaultBus(bus);
BusFactory.setThreadDefaultBus(bus);
String address = "https://localhost:" + STSPORT + "/SecurityTokenService/token";
WebClient client = WebClient.create(address, busFile.toString());
client.type("application/xml").accept("application/xml");
// Create RequestSecurityToken
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
String namespace = STSUtils.WST_NS_05_12;
writer.writeStartElement("wst", "RequestSecurityToken", namespace);
writer.writeNamespace("wst", namespace);
writer.writeStartElement("wst", "RequestType", namespace);
writer.writeCharacters(namespace + "/Issue");
writer.writeEndElement();
writer.writeStartElement("wst", "TokenType", namespace);
writer.writeCharacters(JWT_TOKEN_TYPE);
writer.writeEndElement();
writer.writeEndElement();
Response response = client.post(new DOMSource(writer.getDocument().getDocumentElement()));
RequestSecurityTokenResponseType securityResponse = response.readEntity(RequestSecurityTokenResponseType.class);
RequestedSecurityTokenType requestedSecurityToken = getRequestedSecurityToken(securityResponse);
assertNotNull(requestedSecurityToken);
String token = ((Element) requestedSecurityToken.getAny()).getTextContent();
assertNotNull(token);
validateJWTToken(token, null);
bus.shutdown(true);
}
use of org.apache.cxf.ws.security.sts.provider.model.RequestedSecurityTokenType in project cxf by apache.
the class STSRESTTest method testValidateSAMLAndIssueJWT.
@org.junit.Test
public void testValidateSAMLAndIssueJWT() throws Exception {
SpringBusFactory bf = new SpringBusFactory();
URL busFile = STSRESTTest.class.getResource("cxf-client.xml");
Bus bus = bf.createBus(busFile.toString());
BusFactory.setDefaultBus(bus);
BusFactory.setThreadDefaultBus(bus);
String address = "https://localhost:" + STSPORT + "/SecurityTokenService/token";
WebClient client = WebClient.create(address, busFile.toString());
client.accept("application/xml");
client.path("saml2.0");
// 1. Get a token via GET
Response response = client.get();
Document assertionDoc = response.readEntity(Document.class);
assertNotNull(assertionDoc);
// 2. Now validate it in the STS using POST
client = WebClient.create(address, busFile.toString());
client.type("application/xml").accept("application/xml");
client.query("action", "validate");
// Create RequestSecurityToken
W3CDOMStreamWriter writer = new W3CDOMStreamWriter();
String namespace = STSUtils.WST_NS_05_12;
writer.writeStartElement("wst", "RequestSecurityToken", namespace);
writer.writeNamespace("wst", namespace);
writer.writeStartElement("wst", "RequestType", namespace);
writer.writeCharacters(namespace + "/Validate");
writer.writeEndElement();
writer.writeStartElement("wst", "TokenType", namespace);
writer.writeCharacters(JWT_TOKEN_TYPE);
writer.writeEndElement();
writer.writeStartElement("wst", "ValidateTarget", namespace);
StaxUtils.copy(assertionDoc.getDocumentElement(), writer);
writer.writeEndElement();
writer.writeEndElement();
response = client.post(new DOMSource(writer.getDocument().getDocumentElement()));
RequestSecurityTokenResponseType securityResponse = response.readEntity(RequestSecurityTokenResponseType.class);
StatusType status = null;
for (Object obj : securityResponse.getAny()) {
if (obj instanceof JAXBElement<?>) {
JAXBElement<?> jaxbElement = (JAXBElement<?>) obj;
if ("Status".equals(jaxbElement.getName().getLocalPart())) {
status = (StatusType) jaxbElement.getValue();
break;
}
}
}
assertNotNull(status);
// Check the token was valid
String validCode = "http://docs.oasis-open.org/ws-sx/ws-trust/200512/status/valid";
assertEquals(validCode, status.getCode());
// Check the token
RequestedSecurityTokenType requestedSecurityToken = getRequestedSecurityToken(securityResponse);
assertNotNull(requestedSecurityToken);
String token = ((Element) requestedSecurityToken.getAny()).getTextContent();
assertNotNull(token);
validateJWTToken(token, null);
bus.shutdown(true);
}
Aggregations