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");
}
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());
}
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;
}
});
}
}
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;
}
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);
}
Aggregations