Search in sources :

Example 26 with TokenStore

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();
}
Also used : SecurityToken(org.apache.cxf.ws.security.tokenstore.SecurityToken) MemoryTokenStore(org.apache.cxf.ws.security.tokenstore.MemoryTokenStore) QName(javax.xml.namespace.QName) Service(javax.xml.ws.Service) DoubleItPortType(org.example.contract.doubleit.DoubleItPortType) BindingProvider(javax.xml.ws.BindingProvider) TokenStore(org.apache.cxf.ws.security.tokenstore.TokenStore) MemoryTokenStore(org.apache.cxf.ws.security.tokenstore.MemoryTokenStore) URL(java.net.URL) BusException(org.apache.cxf.BusException) EndpointException(org.apache.cxf.endpoint.EndpointException)

Aggregations

TokenStore (org.apache.cxf.ws.security.tokenstore.TokenStore)26 SecurityToken (org.apache.cxf.ws.security.tokenstore.SecurityToken)17 URL (java.net.URL)9 QName (javax.xml.namespace.QName)8 Service (javax.xml.ws.Service)8 DoubleItPortType (org.example.contract.doubleit.DoubleItPortType)8 Client (org.apache.cxf.endpoint.Client)7 Element (org.w3c.dom.Element)6 Bus (org.apache.cxf.Bus)5 Endpoint (org.apache.cxf.endpoint.Endpoint)5 ReceivedToken (org.apache.cxf.sts.request.ReceivedToken)5 Collection (java.util.Collection)4 SpringBusFactory (org.apache.cxf.bus.spring.SpringBusFactory)4 TokenValidatorParameters (org.apache.cxf.sts.token.validator.TokenValidatorParameters)4 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)4 SamlAssertionWrapper (org.apache.wss4j.common.saml.SamlAssertionWrapper)4 Test (org.junit.Test)4 Matchers.anyString (org.mockito.Matchers.anyString)4 SoapMessage (org.apache.cxf.binding.soap.SoapMessage)3 MemoryTokenStore (org.apache.cxf.ws.security.tokenstore.MemoryTokenStore)3