Java Code Examples for org.apache.cxf.configuration.security.AuthorizationPolicy#setUserName()

The following examples show how to use org.apache.cxf.configuration.security.AuthorizationPolicy#setUserName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AuthPolicyValidatingInterceptorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidateAuthorizationPolicy() throws Exception {
    AuthPolicyValidatingInterceptor in = new AuthPolicyValidatingInterceptor();
    TestSTSTokenValidator validator = new TestSTSTokenValidator();
    in.setValidator(validator);

    AuthorizationPolicy policy = new AuthorizationPolicy();
    policy.setUserName("bob");
    policy.setPassword("pswd");
    Message message = new MessageImpl();
    message.put(AuthorizationPolicy.class, policy);

    in.handleMessage(message);

    assertTrue(validator.isValidated());
}
 
Example 2
Source File: HTTPConduitURLEasyMockTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void setUpHeaders(Message message) {
    Map<String, List<String>> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    List<String> contentTypes = new ArrayList<>();
    contentTypes.add("text/xml;charset=utf8");
    headers.put("content-type", contentTypes);

    List<String> acceptTypes = new ArrayList<>();
    acceptTypes.add("text/xml;charset=utf8");
    acceptTypes.add("text/plain");
    headers.put("Accept", acceptTypes);

    message.put(Message.PROTOCOL_HEADERS, headers);

    AuthorizationPolicy authPolicy = new AuthorizationPolicy();
    authPolicy.setUserName("BJ");
    authPolicy.setPassword("value");
    message.put(AuthorizationPolicy.class, authPolicy);
}
 
Example 3
Source File: AuthPolicyValidatingInterceptorTest.java    From steady with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidateAuthorizationPolicy() throws Exception {
    AuthPolicyValidatingInterceptor in = new AuthPolicyValidatingInterceptor();
    TestSTSTokenValidator validator = new TestSTSTokenValidator();
    in.setValidator(validator);
    
    AuthorizationPolicy policy = new AuthorizationPolicy();
    policy.setUserName("bob");
    policy.setPassword("pswd");
    Message message = new MessageImpl();
    message.put(AuthorizationPolicy.class, policy);
    
    in.handleMessage(message);
    
    assertTrue(validator.isValidated());
}
 
Example 4
Source File: JAXRSJaasSecurityTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testJaasFilterAuthenticationFailure() throws Exception {
    String endpointAddress =
        "http://localhost:" + PORT + "/service/jaas2/bookstorestorage/thosebooks/123";
    WebClient wc = WebClient.create(endpointAddress);
    AuthorizationPolicy pol = new AuthorizationPolicy();
    pol.setUserName("foo");
    pol.setPassword("bar1");
    WebClient.getConfig(wc).getHttpConduit().setAuthorization(pol);

    wc.accept("application/xml");

    //wc.header(HttpHeaders.AUTHORIZATION,
    //          "Basic " + base64Encode("foo" + ":" + "bar1"));
    Response r = wc.get();
    assertEquals(401, r.getStatus());
    Object wwwAuthHeader = r.getMetadata().getFirst(HttpHeaders.WWW_AUTHENTICATE);
    assertNotNull(wwwAuthHeader);
    assertEquals("Basic", wwwAuthHeader.toString());
}
 
Example 5
Source File: AuthPolicyValidatingInterceptorTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidUsernamePassword() throws Exception {
    AuthPolicyValidatingInterceptor in = new AuthPolicyValidatingInterceptor();
    TestSTSTokenValidator validator = new TestSTSTokenValidator();
    in.setValidator(validator);

    AuthorizationPolicy policy = new AuthorizationPolicy();
    policy.setUserName("bob");
    policy.setPassword("pswd2");
    Message message = new MessageImpl();
    message.put(AuthorizationPolicy.class, policy);

    in.handleMessage(message);

    assertFalse(validator.isValidated());
}
 
Example 6
Source File: AuthPolicyValidatingInterceptorTest.java    From steady with Apache License 2.0 6 votes vote down vote up
@Test
public void testValidateAuthorizationPolicy() throws Exception {
    AuthPolicyValidatingInterceptor in = new AuthPolicyValidatingInterceptor();
    TestSTSTokenValidator validator = new TestSTSTokenValidator();
    in.setValidator(validator);
    
    AuthorizationPolicy policy = new AuthorizationPolicy();
    policy.setUserName("bob");
    policy.setPassword("pswd");
    Message message = new MessageImpl();
    message.put(AuthorizationPolicy.class, policy);
    
    in.handleMessage(message);
    
    assertTrue(validator.isValidated());
}
 
Example 7
Source File: JAXRSKerberosBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetBookWithInterceptor() throws Exception {
    if (!runTests) {
        return;
    }

    WebClient wc = WebClient.create("http://localhost:" + PORT + "/bookstore/books/123");

    KerberosAuthOutInterceptor kbInterceptor = new KerberosAuthOutInterceptor();

    AuthorizationPolicy policy = new AuthorizationPolicy();
    policy.setAuthorizationType(HttpAuthHeader.AUTH_TYPE_NEGOTIATE);
    policy.setAuthorization("alice");
    policy.setUserName("alice");
    policy.setPassword("alice");

    kbInterceptor.setPolicy(policy);
    kbInterceptor.setCredDelegation(true);

    WebClient.getConfig(wc).getOutInterceptors().add(new LoggingOutInterceptor());
    WebClient.getConfig(wc).getOutInterceptors().add(kbInterceptor);

    // Required so as to get it working with our KDC
    kbInterceptor.setServicePrincipalName("[email protected]");
    kbInterceptor.setServiceNameType(GSSName.NT_HOSTBASED_SERVICE);

    Book b = wc.get(Book.class);
    Assert.assertEquals(b.getId(), 123);
}
 
Example 8
Source File: JAXRSJaasSecurityTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testJaasFilterWebClientAuthorizationPolicy() throws Exception {
    String endpointAddress =
        "http://localhost:" + PORT + "/service/jaas2/bookstorestorage/thosebooks/123";
    WebClient wc = WebClient.create(endpointAddress);
    AuthorizationPolicy pol = new AuthorizationPolicy();
    pol.setUserName("bob");
    pol.setPassword("bobspassword");
    WebClient.getConfig(wc).getHttpConduit().setAuthorization(pol);
    wc.accept("application/xml");
    Book book = wc.get(Book.class);
    assertEquals(123L, book.getId());
}
 
Example 9
Source File: DigestAuthTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testDigestAuth() throws Exception {
    URL wsdl = getClass().getResource("../greeting.wsdl");
    assertNotNull("WSDL is null", wsdl);

    SOAPService service = new SOAPService(wsdl, serviceName);
    assertNotNull("Service is null", service);

    Greeter mortimer = service.getPort(mortimerQ, Greeter.class);
    assertNotNull("Port is null", mortimer);

    TestUtil.setAddress(mortimer, "http://localhost:" + PORT + "/digestauth/greeter");

    Client client = ClientProxy.getClient(mortimer);

    HTTPConduit http =
        (HTTPConduit) client.getConduit();
    AuthorizationPolicy authPolicy = new AuthorizationPolicy();
    authPolicy.setAuthorizationType("Digest");
    authPolicy.setUserName("foo");
    authPolicy.setPassword("bar");
    http.setAuthorization(authPolicy);

    String answer = mortimer.sayHi();
    assertEquals("Unexpected answer: " + answer,
            "Hi", answer);

}
 
Example 10
Source File: BasicAuthenticationInterceptorTest.java    From dropwizard-jaxws with Apache License 2.0 5 votes vote down vote up
private Message createMessageWithUsernameAndPassword(String username, String password) {
    Message message = createEmptyMessage();

    AuthorizationPolicy policy = new AuthorizationPolicy();
    policy.setUserName(username);
    policy.setPassword(password);
    message.put(AuthorizationPolicy.class, policy);
    return message;
}
 
Example 11
Source File: ClientServerTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicAuth() throws Exception {
    Service service = Service.create(serviceName);
    service.addPort(fakePortName, "http://schemas.xmlsoap.org/soap/",
                    "http://localhost:" + PORT + "/SoapContext/SoapPort");
    Greeter greeter = service.getPort(fakePortName, Greeter.class);

    try {
        //try the jaxws way
        BindingProvider bp = (BindingProvider)greeter;
        bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "BJ");
        bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pswd");
        String s = greeter.greetMe("secure");
        assertEquals("Hello BJ", s);
        bp.getRequestContext().remove(BindingProvider.USERNAME_PROPERTY);
        bp.getRequestContext().remove(BindingProvider.PASSWORD_PROPERTY);

        //try setting on the conduit directly
        Client client = ClientProxy.getClient(greeter);
        HTTPConduit httpConduit = (HTTPConduit)client.getConduit();
        AuthorizationPolicy policy = new AuthorizationPolicy();
        policy.setUserName("BJ2");
        policy.setPassword("pswd");
        httpConduit.setAuthorization(policy);

        s = greeter.greetMe("secure");
        assertEquals("Hello BJ2", s);
    } catch (UndeclaredThrowableException ex) {
        throw (Exception)ex.getCause();
    }
}
 
Example 12
Source File: ClientServerWebSocketTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testBasicAuth() throws Exception {
    URL wsdl = getClass().getResource("/wsdl/hello_world.wsdl");
    assertNotNull(wsdl);

    SOAPService service = new SOAPService(wsdl, serviceName);
    Greeter greeter = service.getPort(portName, Greeter.class);
    updateGreeterAddress(greeter, PORT);

    try {
        //try the jaxws way
        BindingProvider bp = (BindingProvider)greeter;
        bp.getRequestContext().put(BindingProvider.USERNAME_PROPERTY, "BJ");
        bp.getRequestContext().put(BindingProvider.PASSWORD_PROPERTY, "pswd");
        String s = greeter.greetMe("secure");
        assertEquals("Hello BJ", s);
        bp.getRequestContext().remove(BindingProvider.USERNAME_PROPERTY);
        bp.getRequestContext().remove(BindingProvider.PASSWORD_PROPERTY);
        ((Closeable)greeter).close();

        greeter = service.getPort(portName, Greeter.class);
        updateGreeterAddress(greeter, PORT);
        //try setting on the conduit directly
        Client client = ClientProxy.getClient(greeter);
        HTTPConduit httpConduit = (HTTPConduit)client.getConduit();
        AuthorizationPolicy policy = new AuthorizationPolicy();
        policy.setUserName("BJ2");
        policy.setPassword("pswd");
        httpConduit.setAuthorization(policy);

        s = greeter.greetMe("secure");
        ((Closeable)greeter).close();
        assertEquals("Hello BJ2", s);
    } catch (UndeclaredThrowableException ex) {
        throw (Exception)ex.getCause();
    }
}
 
Example 13
Source File: UndertowBasicAuthTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void configure(String name, String address, HTTPConduit c) {

            AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();

            authorizationPolicy.setUserName("ffang");
            authorizationPolicy.setPassword("pswd");
            authorizationPolicy.setAuthorizationType("Basic");
            c.setAuthorization(authorizationPolicy);
        }
 
Example 14
Source File: UndertowDigestAuthTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
public void configure(String name, String address, HTTPConduit c) {

            AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();

            authorizationPolicy.setUserName("ffang");
            authorizationPolicy.setPassword("pswd");
            authorizationPolicy.setAuthorizationType("Digest");
            c.setAuthorization(authorizationPolicy);
        }
 
Example 15
Source File: JAASResourceOwnerLoginHandler.java    From cxf with Apache License 2.0 5 votes vote down vote up
private Message setupMessage(String name, String password) {
    AuthorizationPolicy policy = new AuthorizationPolicy();
    policy.setUserName(name);
    policy.setPassword(password);
    Message message = new MessageImpl();
    message.put(AuthorizationPolicy.class, policy);
    return message;
}
 
Example 16
Source File: ClientConfig.java    From cxf-jaxws with MIT License 5 votes vote down vote up
@Bean
public AuthorizationPolicy basicAuthorization() {
  AuthorizationPolicy authorizationPolicy =
      new AuthorizationPolicy();
  authorizationPolicy.setUserName(userName);
  authorizationPolicy.setPassword(password);
  authorizationPolicy.setAuthorizationType("Basic");

  return authorizationPolicy;
}
 
Example 17
Source File: JAASLoginInterceptorTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
private void addAuthPolicy(Message message, String username, String password) {
    AuthorizationPolicy authPol = new AuthorizationPolicy();
    authPol.setUserName(username);
    authPol.setPassword(password);
    message.put(AuthorizationPolicy.class, authPol);
}
 
Example 18
Source File: HTTPSConduitTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * This methods tests a basic https connection to Bethal.
 * It supplies an authorization policy with premetive user/pass
 * to avoid the 401.
 */
@Test
public void testHttpsBasicConnection() throws Exception {
    startServer("Bethal");

    URL wsdl = getClass().getResource("greeting.wsdl");
    assertNotNull("WSDL is null", wsdl);

    SOAPService service = new SOAPService(wsdl, serviceName);
    assertNotNull("Service is null", service);

    Greeter bethal = service.getPort(bethalQ, Greeter.class);
    assertNotNull("Port is null", bethal);
    updateAddressPort(bethal, getPort("PORT4"));

    // Okay, I'm sick of configuration files.
    // This also tests dynamic configuration of the conduit.
    Client client = ClientProxy.getClient(bethal);
    HTTPConduit http =
        (HTTPConduit) client.getConduit();

    HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();

    httpClientPolicy.setAutoRedirect(false);
    // If we set any name, but Edward, Mary, or George,
    // and a password of "password" we will get through
    // Bethal.
    AuthorizationPolicy authPolicy = new AuthorizationPolicy();
    authPolicy.setUserName("Betty");
    authPolicy.setPassword("password");

    http.setClient(httpClientPolicy);
    http.setTlsClientParameters(tlsClientParameters);
    http.setAuthorization(authPolicy);

    configureProxy(client);
    String answer = bethal.sayHi();
    assertTrue("Unexpected answer: " + answer,
            "Bonjour from Bethal".equals(answer));
    assertProxyRequestCount(0);
}
 
Example 19
Source File: JAXRSClientFactoryBean.java    From cxf with Apache License 2.0 4 votes vote down vote up
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;
                }

            });
        }
    }
 
Example 20
Source File: HTTPConduitTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
/**
 * This test verifies the precedence of Authorization Information.
 * Setting authorization information on the Message takes precedence
 * over a Basic Auth Supplier with preemptive UserPass, and that
 * followed by setting it directly on the Conduit.
 */
@Test
public void testAuthPolicyPrecedence() throws Exception {
    Bus bus = new ExtensionManagerBus();
    EndpointInfo ei = new EndpointInfo();
    ei.setAddress("http://nowhere.com/bar/foo");
    HTTPConduit conduit = new URLConnectionHTTPConduit(bus, ei, null);
    conduit.finalizeConfig();

    conduit.getAuthorization().setUserName("Satan");
    conduit.getAuthorization().setPassword("hell");
    Message message = getNewMessage();

    // Test call
    conduit.prepare(message);

    Map<String, List<String>> headers =
        CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));

    assertNotNull("Authorization Header should exist",
            headers.get("Authorization"));

    assertEquals("Unexpected Authorization Token",
        DefaultBasicAuthSupplier.getBasicAuthHeader("Satan", "hell"),
            headers.get("Authorization").get(0));

    // Setting a Basic Auth User Pass should override
    conduit.setAuthSupplier(new TestAuthSupplier());
    message = getNewMessage();

    // Test Call
    conduit.prepare(message);

    headers =
        CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));
    List<String> authorization = headers.get("Authorization");
    assertNotNull("Authorization Token must be set", authorization);
    assertEquals("Wrong Authorization Token", "myauth", authorization.get(0));

    conduit.setAuthSupplier(null);
    // Setting authorization policy on the message should override
    // conduit setting
    AuthorizationPolicy authPolicy = new AuthorizationPolicy();
    authPolicy.setUserName("Hello");
    authPolicy.setPassword("world");
    authPolicy.setAuthorizationType("Basic");
    message = getNewMessage();
    message.put(AuthorizationPolicy.class, authPolicy);

    conduit.prepare(message);

    headers =
        CastUtils.cast((Map<?, ?>)message.get(Message.PROTOCOL_HEADERS));

    assertEquals("Unexpected Authorization Token",
        DefaultBasicAuthSupplier.getBasicAuthHeader("Hello", "world"),
            headers.get("Authorization").get(0));
}