use of org.apache.cxf.sts.token.provider.SCTProvider in project cxf by apache.
the class IssueSCTUnitTest method testIssueSCTNoReferences.
/**
* Test to successfully issue a SecurityContextToken with no references
*/
@org.junit.Test
public void testIssueSCTNoReferences() throws Exception {
TokenIssueOperation issueOperation = new TokenIssueOperation();
issueOperation.setTokenStore(tokenStore);
issueOperation.setReturnReferences(false);
// Add Token Provider
issueOperation.setTokenProviders(Collections.singletonList(new SCTProvider()));
// 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, STSUtils.TOKEN_TYPE_SCT_05_12);
request.getAny().add(tokenType);
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 that no references were returned
boolean foundReference = false;
for (Object tokenObject : securityTokenResponse.get(0).getAny()) {
if (tokenObject instanceof JAXBElement<?> && (ATTACHED_REFERENCE.equals(((JAXBElement<?>) tokenObject).getName()) || UNATTACHED_REFERENCE.equals(((JAXBElement<?>) tokenObject).getName()))) {
foundReference = true;
break;
}
}
assertFalse(foundReference);
}
use of org.apache.cxf.sts.token.provider.SCTProvider in project cxf by apache.
the class IssueSCTUnitTest method testIssueEncryptedSCT.
/**
* Test to successfully issue an encrypted SecurityContextToken
*/
@org.junit.Test
public void testIssueEncryptedSCT() throws Exception {
TokenIssueOperation issueOperation = new TokenIssueOperation();
issueOperation.setTokenStore(tokenStore);
issueOperation.setEncryptIssuedToken(true);
// Add Token Provider
issueOperation.setTokenProviders(Collections.singletonList(new SCTProvider()));
// Add Service
ServiceMBean service = new StaticService();
service.setEndpoints(Collections.singletonList("http://dummy-service.com/dummy"));
EncryptionProperties encryptionProperties = new EncryptionProperties();
if (!unrestrictedPoliciesInstalled) {
encryptionProperties.setEncryptionAlgorithm(WSS4JConstants.AES_128);
}
service.setEncryptionProperties(encryptionProperties);
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, STSUtils.TOKEN_TYPE_SCT_05_12);
request.getAny().add(tokenType);
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 securityContextToken = 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();
securityContextToken = (Element) rstType.getAny();
break;
}
}
assertNotNull(securityContextToken);
String tokenString = DOM2Writer.nodeToString(securityContextToken);
assertFalse(tokenString.contains("SecurityContextToken"));
assertFalse(tokenString.contains("Identifier"));
assertTrue(tokenString, tokenString.contains("EncryptedData"));
}
use of org.apache.cxf.sts.token.provider.SCTProvider in project cxf by apache.
the class CancelSCTUnitTest method createSCT.
private TokenProviderResponse createSCT() throws WSSecurityException {
TokenProvider sctTokenProvider = new SCTProvider();
TokenProviderParameters providerParameters = createProviderParameters(STSUtils.TOKEN_TYPE_SCT_05_12);
assertTrue(sctTokenProvider.canHandleToken(STSUtils.TOKEN_TYPE_SCT_05_12));
TokenProviderResponse providerResponse = sctTokenProvider.createToken(providerParameters);
assertNotNull(providerResponse);
assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
return providerResponse;
}
use of org.apache.cxf.sts.token.provider.SCTProvider in project cxf by apache.
the class IssueSCTUnitTest method testIssueSCT.
/**
* Test to successfully issue a SecurityContextToken
*/
@org.junit.Test
public void testIssueSCT() throws Exception {
TokenIssueOperation issueOperation = new TokenIssueOperation();
issueOperation.setTokenStore(tokenStore);
// Add Token Provider
issueOperation.setTokenProviders(Collections.singletonList(new SCTProvider()));
// 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, STSUtils.TOKEN_TYPE_SCT_05_12);
request.getAny().add(tokenType);
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 securityContextToken = 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();
securityContextToken = (Element) rstType.getAny();
break;
}
}
assertNotNull(securityContextToken);
String tokenString = DOM2Writer.nodeToString(securityContextToken);
assertTrue(tokenString.contains("SecurityContextToken"));
assertTrue(tokenString.contains("Identifier"));
}
use of org.apache.cxf.sts.token.provider.SCTProvider in project cxf by apache.
the class ValidateSCTUnitTest method createSCT.
private TokenProviderResponse createSCT() throws WSSecurityException {
TokenProvider sctTokenProvider = new SCTProvider();
TokenProviderParameters providerParameters = createProviderParameters(STSUtils.TOKEN_TYPE_SCT_05_12);
assertTrue(sctTokenProvider.canHandleToken(STSUtils.TOKEN_TYPE_SCT_05_12));
TokenProviderResponse providerResponse = sctTokenProvider.createToken(providerParameters);
assertNotNull(providerResponse);
assertTrue(providerResponse.getToken() != null && providerResponse.getTokenId() != null);
return providerResponse;
}
Aggregations