use of org.apache.cxf.ws.security.tokenstore.MemoryTokenStore in project cxf by apache.
the class UsernameActAsCachingTest method testDifferentUsersCaching.
/**
* Test caching the issued token when the STSClient is deployed in an intermediary
*/
@org.junit.Test
public void testDifferentUsersCaching() throws Exception {
SpringBusFactory bf = new SpringBusFactory();
URL busFile = UsernameActAsCachingTest.class.getResource("cxf-client.xml");
Bus bus = bf.createBus(busFile.toString());
BusFactory.setDefaultBus(bus);
BusFactory.setThreadDefaultBus(bus);
URL wsdl = UsernameActAsCachingTest.class.getResource("DoubleIt.wsdl");
Service service = Service.create(wsdl, SERVICE_QNAME);
QName portQName = new QName(NAMESPACE, "DoubleItAsymmetricSAML2BearerPort3");
DoubleItPortType port = service.getPort(portQName, DoubleItPortType.class);
updateAddressPort(port, PORT);
TokenTestUtils.updateSTSPort((BindingProvider) port, STSPORT2);
// Disable storing tokens per-proxy
((BindingProvider) port).getRequestContext().put(SecurityConstants.CACHE_ISSUED_TOKEN_IN_ENDPOINT, "false");
// Make a successful invocation
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "alice");
doubleIt(port, 25);
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "bob");
doubleIt(port, 30);
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "eve");
try {
doubleIt(port, 30);
fail("Failure expected on a bad user");
} catch (Exception ex) {
//
}
// Change the STSClient so that it can no longer find the STS
BindingProvider p = (BindingProvider) port;
clearSTSClient(p);
// Make a successful invocation
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "alice");
doubleIt(port, 25);
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "bob");
doubleIt(port, 30);
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "eve2");
try {
doubleIt(port, 30);
fail("Failure expected on a bad user");
} catch (Exception ex) {
//
}
// Reset the cache - this invocation should fail
p.getRequestContext().put(TokenStore.class.getName(), new MemoryTokenStore());
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "alice");
try {
doubleIt(port, 30);
fail("Failure expected");
} catch (Exception ex) {
//
}
((java.io.Closeable) port).close();
bus.shutdown(true);
}
use of org.apache.cxf.ws.security.tokenstore.MemoryTokenStore in project cxf by apache.
the class AbstractPolicySecurityTest method runOutInterceptorAndValidateSamlTokenAttached.
protected void runOutInterceptorAndValidateSamlTokenAttached(String policyDoc) throws Exception {
// create the request message
final Document document = this.readDocument("wsse-request-clean.xml");
final Element outPolicyElement = this.readDocument(policyDoc).getDocumentElement();
final Policy policy = this.policyBuilder.getPolicy(outPolicyElement);
AssertionInfoMap aim = new AssertionInfoMap(policy);
SoapMessage msg = this.getOutSoapMessageForDom(document, aim);
// add an "issued" assertion into the message exchange
Element issuedAssertion = this.readDocument("example-sts-issued-saml-assertion.xml").getDocumentElement();
Properties cryptoProps = new Properties();
URL url = ClassLoader.getSystemResource("outsecurity.properties");
cryptoProps.load(url.openStream());
Crypto crypto = CryptoFactory.getInstance(cryptoProps);
// Sign the "issued" assertion
SamlAssertionWrapper assertionWrapper = new SamlAssertionWrapper(issuedAssertion);
assertionWrapper.signAssertion("myalias", "myAliasPassword", crypto, false);
Document doc = DOMUtils.newDocument();
issuedAssertion = OpenSAMLUtil.toDom(assertionWrapper.getSaml1(), doc);
String assertionId = issuedAssertion.getAttributeNodeNS(null, "AssertionID").getNodeValue();
SecurityToken issuedToken = new SecurityToken(assertionId, issuedAssertion, null);
String alias = cryptoProps.getProperty("org.apache.ws.security.crypto.merlin.keystore.alias");
CryptoType cryptoType = new CryptoType(CryptoType.TYPE.ALIAS);
cryptoType.setAlias(alias);
issuedToken.setX509Certificate(crypto.getX509Certificates(cryptoType)[0], crypto);
msg.getExchange().getEndpoint().put(SecurityConstants.TOKEN_ID, issuedToken.getId());
msg.getExchange().put(SecurityConstants.TOKEN_ID, issuedToken.getId());
TokenStore tokenStore = new MemoryTokenStore();
msg.getExchange().getEndpoint().getEndpointInfo().setProperty(TokenStore.class.getName(), tokenStore);
tokenStore.add(issuedToken);
// fire the interceptor and verify results
final Document signedDoc = this.runOutInterceptorAndValidate(msg, policy, aim, null, null);
this.runInInterceptorAndValidate(signedDoc, policy, Collections.singletonList(SP12Constants.ISSUED_TOKEN), null, Collections.singletonList(CoverageType.SIGNED));
}
use of org.apache.cxf.ws.security.tokenstore.MemoryTokenStore in project cxf by apache.
the class UsernameActAsCachingTest method testUsernameActAsCaching.
/**
* Test caching the issued token
*/
@org.junit.Test
public void testUsernameActAsCaching() throws Exception {
SpringBusFactory bf = new SpringBusFactory();
URL busFile = UsernameActAsCachingTest.class.getResource("cxf-client.xml");
Bus bus = bf.createBus(busFile.toString());
BusFactory.setDefaultBus(bus);
BusFactory.setThreadDefaultBus(bus);
URL wsdl = UsernameActAsCachingTest.class.getResource("DoubleIt.wsdl");
Service service = Service.create(wsdl, SERVICE_QNAME);
QName portQName = new QName(NAMESPACE, "DoubleItAsymmetricSAML2BearerPort2");
//
// Proxy no. 1
//
DoubleItPortType port = service.getPort(portQName, DoubleItPortType.class);
updateAddressPort(port, PORT);
TokenTestUtils.updateSTSPort((BindingProvider) port, STSPORT2);
TokenStore tokenStore = new MemoryTokenStore();
((BindingProvider) port).getRequestContext().put(TokenStore.class.getName(), tokenStore);
// Make a successful invocation
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "alice");
doubleIt(port, 25);
// Change the STSClient so that it can no longer find the STS
BindingProvider p = (BindingProvider) port;
clearSTSClient(p);
// This invocation should be successful as the token is cached
doubleIt(port, 25);
//
// Proxy no. 2
//
DoubleItPortType port2 = service.getPort(portQName, DoubleItPortType.class);
updateAddressPort(port2, PORT);
TokenTestUtils.updateSTSPort((BindingProvider) port2, STSPORT2);
// Change the STSClient so that it can no longer find the STS
p = (BindingProvider) port2;
clearSTSClient(p);
// This should fail as the cache is not being used
try {
doubleIt(port2, 40);
fail("Failure expected as the token is not stored in the cache");
} catch (Exception ex) {
// expected
}
// Set the cache correctly
p.getRequestContext().put(TokenStore.class.getName(), tokenStore);
// Make another invocation - this should succeed as the token is cached
p.getRequestContext().put(SecurityConstants.USERNAME, "alice");
doubleIt(port2, 40);
// Reset the cache - this invocation should fail
p.getRequestContext().put(TokenStore.class.getName(), new MemoryTokenStore());
p.getRequestContext().put(SecurityConstants.TOKEN, new SecurityToken());
try {
doubleIt(port2, 40);
fail("Failure expected as the cache is reset");
} catch (Exception ex) {
// expected
}
((java.io.Closeable) port).close();
bus.shutdown(true);
}
use of org.apache.cxf.ws.security.tokenstore.MemoryTokenStore in project cxf by apache.
the class UsernameOnBehalfOfCachingTest method testUsernameOnBehalfOfCaching.
/**
* Test caching the issued token
*/
@org.junit.Test
public void testUsernameOnBehalfOfCaching() throws Exception {
SpringBusFactory bf = new SpringBusFactory();
URL busFile = UsernameOnBehalfOfCachingTest.class.getResource("cxf-client.xml");
Bus bus = bf.createBus(busFile.toString());
BusFactory.setDefaultBus(bus);
BusFactory.setThreadDefaultBus(bus);
URL wsdl = UsernameOnBehalfOfCachingTest.class.getResource("DoubleIt.wsdl");
Service service = Service.create(wsdl, SERVICE_QNAME);
QName portQName = new QName(NAMESPACE, "DoubleItOBOAsymmetricSAML2BearerPort2");
//
// Proxy no. 1
//
DoubleItPortType port = service.getPort(portQName, DoubleItPortType.class);
updateAddressPort(port, PORT);
TokenTestUtils.updateSTSPort((BindingProvider) port, STSPORT2);
TokenStore tokenStore = new MemoryTokenStore();
((BindingProvider) port).getRequestContext().put(TokenStore.class.getName(), tokenStore);
// Make a successful invocation
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "alice");
doubleIt(port, 25);
// Change the STSClient so that it can no longer find the STS
BindingProvider p = (BindingProvider) port;
clearSTSClient(p);
// This invocation should be successful as the token is cached
doubleIt(port, 25);
((java.io.Closeable) port).close();
//
// Proxy no. 2
//
DoubleItPortType port2 = service.getPort(portQName, DoubleItPortType.class);
updateAddressPort(port2, PORT);
TokenTestUtils.updateSTSPort((BindingProvider) port2, STSPORT2);
// Change the STSClient so that it can no longer find the STS
p = (BindingProvider) port2;
clearSTSClient(p);
// This should fail as the cache is not being used
try {
doubleIt(port2, 40);
fail("Failure expected as the token is not stored in the cache");
} catch (Exception ex) {
// expected
}
// Set the cache correctly
p.getRequestContext().put(TokenStore.class.getName(), tokenStore);
// Make another invocation - this should succeed as the token is cached
p.getRequestContext().put(SecurityConstants.USERNAME, "alice");
doubleIt(port2, 40);
// Reset the cache - this invocation should fail
p.getRequestContext().put(TokenStore.class.getName(), new MemoryTokenStore());
p.getRequestContext().put(SecurityConstants.TOKEN, new SecurityToken());
try {
doubleIt(port2, 40);
fail("Failure expected as the cache is reset");
} catch (Exception ex) {
// expected
}
((java.io.Closeable) port2).close();
bus.shutdown(true);
}
use of org.apache.cxf.ws.security.tokenstore.MemoryTokenStore in project cxf by apache.
the class UsernameOnBehalfOfCachingTest method testDifferentUsersCaching.
/**
* Test caching the issued token when the STSClient is deployed in an intermediary
*/
@org.junit.Test
public void testDifferentUsersCaching() throws Exception {
SpringBusFactory bf = new SpringBusFactory();
URL busFile = UsernameOnBehalfOfCachingTest.class.getResource("cxf-client.xml");
Bus bus = bf.createBus(busFile.toString());
BusFactory.setDefaultBus(bus);
BusFactory.setThreadDefaultBus(bus);
URL wsdl = UsernameOnBehalfOfCachingTest.class.getResource("DoubleIt.wsdl");
Service service = Service.create(wsdl, SERVICE_QNAME);
QName portQName = new QName(NAMESPACE, "DoubleItOBOAsymmetricSAML2BearerPort3");
DoubleItPortType port = service.getPort(portQName, DoubleItPortType.class);
updateAddressPort(port, PORT);
TokenTestUtils.updateSTSPort((BindingProvider) port, STSPORT2);
// Disable storing tokens per-proxy
((BindingProvider) port).getRequestContext().put(SecurityConstants.CACHE_ISSUED_TOKEN_IN_ENDPOINT, "false");
// Make a successful invocation
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "alice");
doubleIt(port, 25);
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "bob");
doubleIt(port, 30);
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "eve");
try {
doubleIt(port, 30);
fail("Failure expected on a bad user");
} catch (Exception ex) {
//
}
// Change the STSClient so that it can no longer find the STS
BindingProvider p = (BindingProvider) port;
clearSTSClient(p);
// Make a successful invocation
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "alice");
doubleIt(port, 25);
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "bob");
doubleIt(port, 30);
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "eve2");
try {
doubleIt(port, 30);
fail("Failure expected on a bad user");
} catch (Exception ex) {
//
}
// Reset the cache - this invocation should fail
p.getRequestContext().put(TokenStore.class.getName(), new MemoryTokenStore());
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "alice");
try {
doubleIt(port, 30);
fail("Failure expected");
} catch (Exception ex) {
//
}
((java.io.Closeable) port).close();
bus.shutdown(true);
}
Aggregations