use of org.apache.cxf.ws.security.tokenstore.TokenStore in project cxf by apache.
the class UsernameOnBehalfOfCachingTest method testUsernameOnBehalfOfCaching.
/**
* Test caching the issued token
*/
@org.junit.Test
public void testUsernameOnBehalfOfCaching() throws Exception {
createBus(getClass().getResource("cxf-client.xml").toString());
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();
}
Aggregations