Search in sources :

Example 41 with AuthorizationPolicy

use of org.apache.cxf.configuration.security.AuthorizationPolicy in project cxf by apache.

the class SpringBeansTest method testClients.

@Test
public void testClients() throws Exception {
    AbstractFactoryBeanDefinitionParser.setFactoriesAreAbstract(false);
    ctx = new ClassPathXmlApplicationContext(new String[] { "/org/apache/cxf/frontend/spring/clients.xml" });
    Object bean = ctx.getBean("client1.proxyFactory");
    assertNotNull(bean);
    ClientProxyFactoryBean cpfbean = (ClientProxyFactoryBean) bean;
    BindingConfiguration bc = cpfbean.getBindingConfig();
    assertTrue(bc instanceof SoapBindingConfiguration);
    SoapBindingConfiguration sbc = (SoapBindingConfiguration) bc;
    assertTrue(sbc.getVersion() instanceof Soap12);
    HelloService greeter = (HelloService) ctx.getBean("client1");
    assertNotNull(greeter);
    Client client = ClientProxy.getClient(greeter);
    assertNotNull("expected ConduitSelector", client.getConduitSelector());
    assertTrue("unexpected ConduitSelector", client.getConduitSelector() instanceof NullConduitSelector);
    List<Interceptor<? extends Message>> inInterceptors = client.getInInterceptors();
    boolean saaj = false;
    boolean logging = false;
    for (Interceptor<? extends Message> i : inInterceptors) {
        if (i instanceof SAAJInInterceptor) {
            saaj = true;
        } else if (i instanceof LoggingInInterceptor) {
            logging = true;
        }
    }
    assertTrue(saaj);
    assertTrue(logging);
    saaj = false;
    logging = false;
    for (Interceptor<?> i : client.getOutInterceptors()) {
        if (i instanceof SAAJOutInterceptor) {
            saaj = true;
        } else if (i instanceof LoggingOutInterceptor) {
            logging = true;
        }
    }
    assertTrue(saaj);
    assertTrue(logging);
    ClientProxyFactoryBean clientProxyFactoryBean = (ClientProxyFactoryBean) ctx.getBean("client2.proxyFactory");
    assertNotNull(clientProxyFactoryBean);
    assertEquals("get the wrong transportId", clientProxyFactoryBean.getTransportId(), "http://cxf.apache.org/transports/local");
    assertEquals("get the wrong bindingId", clientProxyFactoryBean.getBindingId(), "http://cxf.apache.org/bindings/xformat");
    greeter = (HelloService) ctx.getBean("client2");
    assertNotNull(greeter);
    greeter = (HelloService) ctx.getBean("client3");
    assertNotNull(greeter);
    client = ClientProxy.getClient(greeter);
    EndpointInfo epi = client.getEndpoint().getEndpointInfo();
    AuthorizationPolicy ap = epi.getExtensor(AuthorizationPolicy.class);
    assertNotNull("The AuthorizationPolicy instance should not be null", ap);
    assertEquals("Get the wrong username", ap.getUserName(), "testUser");
    assertEquals("Get the wrong password", ap.getPassword(), "password");
}
Also used : Soap12(org.apache.cxf.binding.soap.Soap12) SAAJOutInterceptor(org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor) Message(org.apache.cxf.message.Message) HelloService(org.apache.cxf.service.factory.HelloService) NullConduitSelector(org.apache.cxf.endpoint.NullConduitSelector) SAAJInInterceptor(org.apache.cxf.binding.soap.saaj.SAAJInInterceptor) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) SoapBindingConfiguration(org.apache.cxf.binding.soap.SoapBindingConfiguration) LoggingOutInterceptor(org.apache.cxf.ext.logging.LoggingOutInterceptor) ClientProxyFactoryBean(org.apache.cxf.frontend.ClientProxyFactoryBean) LoggingInInterceptor(org.apache.cxf.ext.logging.LoggingInInterceptor) Client(org.apache.cxf.endpoint.Client) SAAJOutInterceptor(org.apache.cxf.binding.soap.saaj.SAAJOutInterceptor) SAAJInInterceptor(org.apache.cxf.binding.soap.saaj.SAAJInInterceptor) Interceptor(org.apache.cxf.interceptor.Interceptor) LoggingInInterceptor(org.apache.cxf.ext.logging.LoggingInInterceptor) LoggingOutInterceptor(org.apache.cxf.ext.logging.LoggingOutInterceptor) BindingConfiguration(org.apache.cxf.binding.BindingConfiguration) SoapBindingConfiguration(org.apache.cxf.binding.soap.SoapBindingConfiguration) Test(org.junit.Test)

Example 42 with AuthorizationPolicy

use of org.apache.cxf.configuration.security.AuthorizationPolicy in project cxf by apache.

the class UndertowHTTPDestinationTest method verifyRequestHeaders.

private void verifyRequestHeaders() throws Exception {
    Map<String, List<String>> requestHeaders = CastUtils.cast((Map<?, ?>) inMessage.get(Message.PROTOCOL_HEADERS));
    assertNotNull("expected request headers", requestHeaders);
    List<String> values = requestHeaders.get("content-type");
    assertNotNull("expected field", values);
    assertEquals("unexpected values", 2, values.size());
    assertTrue("expected value", values.contains("text/xml"));
    assertTrue("expected value", values.contains("charset=utf8"));
    values = requestHeaders.get(AUTH_HEADER);
    assertNotNull("expected field", values);
    assertEquals("unexpected values", 1, values.size());
    assertTrue("expected value", values.contains(BASIC_AUTH));
    AuthorizationPolicy authpolicy = inMessage.get(AuthorizationPolicy.class);
    assertNotNull("Expected some auth tokens", policy);
    assertEquals("expected user", USER, authpolicy.getUserName());
    assertEquals("expected passwd", PASSWD, authpolicy.getPassword());
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) List(java.util.List) ArrayList(java.util.ArrayList) HttpString(io.undertow.util.HttpString)

Example 43 with AuthorizationPolicy

use of org.apache.cxf.configuration.security.AuthorizationPolicy in project cxf by apache.

the class JAXRSClientFactoryBean method initClient.

protected void initClient(AbstractClient client, Endpoint ep, boolean addHeaders) {
    if (username != null) {
        AuthorizationPolicy authPolicy = new AuthorizationPolicy();
        authPolicy.setUserName(username);
        authPolicy.setPassword(password);
        ep.getEndpointInfo().addExtensor(authPolicy);
    }
    client.getConfiguration().setConduitSelector(getConduitSelector(ep));
    client.getConfiguration().setBus(getBus());
    client.getConfiguration().getOutInterceptors().addAll(getOutInterceptors());
    client.getConfiguration().getOutInterceptors().addAll(ep.getOutInterceptors());
    client.getConfiguration().getInInterceptors().addAll(getInInterceptors());
    client.getConfiguration().getInInterceptors().addAll(ep.getInInterceptors());
    client.getConfiguration().getInFaultInterceptors().addAll(getInFaultInterceptors());
    applyFeatures(client);
    if (headers != null && addHeaders) {
        client.headers(headers);
    }
    ClientProviderFactory factory = ClientProviderFactory.createInstance(getBus());
    setupFactory(factory, ep);
    final Map<String, Object> theProperties = super.getProperties();
    final boolean encodeClientParameters = PropertyUtils.isTrue(theProperties, "url.encode.client.parameters");
    if (encodeClientParameters) {
        final String encodeClientParametersList = (String) getProperties().get("url.encode.client.parameters.list");
        factory.registerUserProvider(new ParamConverterProvider() {

            @SuppressWarnings("unchecked")
            @Override
            public <T> ParamConverter<T> getConverter(Class<T> cls, Type t, Annotation[] anns) {
                if (cls == String.class && AnnotationUtils.getAnnotation(anns, HeaderParam.class) == null && AnnotationUtils.getAnnotation(anns, CookieParam.class) == null) {
                    return (ParamConverter<T>) new UrlEncodingParamConverter(encodeClientParametersList);
                }
                return null;
            }
        });
    }
}
Also used : ParamConverter(javax.ws.rs.ext.ParamConverter) Annotation(java.lang.annotation.Annotation) CookieParam(javax.ws.rs.CookieParam) AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) RuntimeType(javax.ws.rs.RuntimeType) Type(java.lang.reflect.Type) ParamConverterProvider(javax.ws.rs.ext.ParamConverterProvider)

Example 44 with AuthorizationPolicy

use of org.apache.cxf.configuration.security.AuthorizationPolicy in project cxf by apache.

the class WrappedMessageContext method get.

public Object get(Object key) {
    String mappedkey = mapKey((String) key);
    Object ret = message.get(mappedkey);
    if (MessageContext.HTTP_REQUEST_METHOD.equals(key) && isRequestor()) {
        return null;
    }
    if (ret == null) {
        if (Message.class.getName().equals(mappedkey)) {
            return message;
        }
        if (exchange != null) {
            ret = exchange.get(mappedkey);
            if (ret != null) {
                return ret;
            }
        }
        if (MessageContext.INBOUND_MESSAGE_ATTACHMENTS.equals(key)) {
            if (isRequestor() && isOutbound()) {
                ret = null;
            } else if (isOutbound()) {
                ret = createAttachments(reqMessage, MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
            } else {
                ret = createAttachments(message, MessageContext.INBOUND_MESSAGE_ATTACHMENTS);
            }
        } else if (MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS.equals(key)) {
            if (isRequestor() && !isOutbound()) {
                ret = createAttachments(reqMessage, MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS);
            } else {
                ret = createAttachments(isRequestor() ? getWrappedMessage() : createResponseMessage(), MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS);
            }
        } else if (MessageContext.MESSAGE_OUTBOUND_PROPERTY.equals(key)) {
            ret = isOutbound();
        } else if (MessageContext.HTTP_REQUEST_HEADERS.equals(key)) {
            if (!isResponse()) {
                ret = message.get(Message.PROTOCOL_HEADERS);
            } else if (reqMessage != null && !isRequestor()) {
                ret = reqMessage.get(Message.PROTOCOL_HEADERS);
            }
        } else if (MessageContext.HTTP_RESPONSE_HEADERS.equals(key)) {
            Map<?, ?> mp = null;
            if (isResponse()) {
                mp = (Map<?, ?>) message.get(Message.PROTOCOL_HEADERS);
            } else if (exchange != null) {
                // may have to create the out message and add the headers
                Message tmp = createResponseMessage();
                if (tmp != null) {
                    mp = (Map<?, ?>) tmp.get(Message.PROTOCOL_HEADERS);
                }
            }
            ret = mp;
        } else if (BindingProvider.USERNAME_PROPERTY.equals(key)) {
            AuthorizationPolicy authPolicy = (AuthorizationPolicy) message.get(AuthorizationPolicy.class.getName());
            if (authPolicy != null) {
                ret = authPolicy.getUserName();
            }
        } else if (BindingProvider.PASSWORD_PROPERTY.equals(key)) {
            AuthorizationPolicy authPolicy = (AuthorizationPolicy) message.get(AuthorizationPolicy.class.getName());
            if (authPolicy != null) {
                ret = authPolicy.getPassword();
            }
        } else if (Message.WSDL_OPERATION.equals(key)) {
            BindingOperationInfo boi = getBindingOperationInfo(exchange);
            if (boi != null && !Boolean.TRUE.equals(boi.getProperty("operation.is.synthetic"))) {
                ret = boi.getName();
            }
        } else if (Message.WSDL_SERVICE.equals(key)) {
            BindingOperationInfo boi = getBindingOperationInfo(exchange);
            if (boi != null) {
                ret = boi.getBinding().getService().getName();
            }
        } else if (Message.WSDL_INTERFACE.equals(key)) {
            BindingOperationInfo boi = getBindingOperationInfo(exchange);
            if (boi != null) {
                ret = boi.getBinding().getService().getInterface().getName();
            }
        } else if (Message.WSDL_PORT.equals(key)) {
            EndpointInfo endpointInfo = getEndpointInfo(exchange);
            if (endpointInfo != null) {
                ret = endpointInfo.getName();
            }
        } else if (Message.WSDL_DESCRIPTION.equals(key)) {
            EndpointInfo endpointInfo = getEndpointInfo(exchange);
            if (endpointInfo != null) {
                URI wsdlDescription = endpointInfo.getProperty("URI", URI.class);
                if (wsdlDescription == null) {
                    String address = endpointInfo.getAddress();
                    try {
                        wsdlDescription = new URI(address + "?wsdl");
                    } catch (URISyntaxException e) {
                    // do nothing
                    }
                    endpointInfo.setProperty("URI", wsdlDescription);
                }
                ret = wsdlDescription;
            }
        }
        if (ret == null && reqMessage != null) {
            ret = reqMessage.get(mappedkey);
        }
    }
    return ret;
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) EndpointInfo(org.apache.cxf.service.model.EndpointInfo) BindingOperationInfo(org.apache.cxf.service.model.BindingOperationInfo) Message(org.apache.cxf.message.Message) URISyntaxException(java.net.URISyntaxException) HashMap(java.util.HashMap) Map(java.util.Map) URI(java.net.URI)

Example 45 with AuthorizationPolicy

use of org.apache.cxf.configuration.security.AuthorizationPolicy in project cxf by apache.

the class WSS4JBasicAuthValidator method validate.

protected void validate(Message message) throws WSSecurityException {
    AuthorizationPolicy policy = message.get(AuthorizationPolicy.class);
    if (policy == null || policy.getUserName() == null || policy.getPassword() == null) {
        String name = null;
        if (policy != null) {
            name = policy.getUserName();
        }
        String errorMsg = "No user name and/or password is available, name: " + name;
        LOG.warning(errorMsg);
        throw new SecurityException(errorMsg);
    }
    UsernameToken token = convertPolicyToToken(policy);
    Credential credential = new Credential();
    credential.setUsernametoken(token);
    RequestData data = new RequestData();
    data.setMsgContext(message);
    data.setCallbackHandler(callbackHandler);
    credential = getValidator().validate(credential, data);
    // Create a Principal/SecurityContext
    final SecurityContext sc;
    if (credential != null && credential.getPrincipal() != null) {
        sc = createSecurityContext(message, credential);
    } else {
        Principal p = new WSUsernameTokenPrincipalImpl(policy.getUserName(), false);
        ((WSUsernameTokenPrincipalImpl) p).setPassword(policy.getPassword());
        sc = createSecurityContext(p);
    }
    message.put(SecurityContext.class, sc);
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) Credential(org.apache.wss4j.dom.validate.Credential) RequestData(org.apache.wss4j.dom.handler.RequestData) UsernameToken(org.apache.wss4j.dom.message.token.UsernameToken) SAMLSecurityContext(org.apache.cxf.rt.security.saml.claims.SAMLSecurityContext) SecurityContext(org.apache.cxf.security.SecurityContext) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) Principal(java.security.Principal) WSUsernameTokenPrincipalImpl(org.apache.wss4j.common.principal.WSUsernameTokenPrincipalImpl)

Aggregations

AuthorizationPolicy (org.apache.cxf.configuration.security.AuthorizationPolicy)86 Message (org.apache.cxf.message.Message)25 Test (org.junit.Test)22 HTTPConduit (org.apache.cxf.transport.http.HTTPConduit)16 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)11 Client (org.apache.cxf.endpoint.Client)11 List (java.util.List)9 HTTPClientPolicy (org.apache.cxf.transports.http.configuration.HTTPClientPolicy)9 URL (java.net.URL)7 HashMap (java.util.HashMap)7 ProxyAuthorizationPolicy (org.apache.cxf.configuration.security.ProxyAuthorizationPolicy)7 Map (java.util.Map)6 SecurityContext (org.apache.cxf.security.SecurityContext)6 Bus (org.apache.cxf.Bus)5 TLSClientParameters (org.apache.cxf.configuration.jsse.TLSClientParameters)5 WebClient (org.apache.cxf.jaxrs.client.WebClient)5 MessageImpl (org.apache.cxf.message.MessageImpl)5 EndpointInfo (org.apache.cxf.service.model.EndpointInfo)5 Principal (java.security.Principal)4