Search in sources :

Example 56 with AuthorizationPolicy

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

the class AuthPolicyValidatingInterceptorTest method testNoUsername.

@Test
public void testNoUsername() throws Exception {
    AuthPolicyValidatingInterceptor in = new AuthPolicyValidatingInterceptor();
    TestSTSTokenValidator validator = new TestSTSTokenValidator();
    in.setValidator(validator);
    AuthorizationPolicy policy = new AuthorizationPolicy();
    policy.setPassword("pswd");
    Message message = new MessageImpl();
    message.put(AuthorizationPolicy.class, policy);
    try {
        in.handleMessage(message);
        fail("Failure expected with no username");
    } catch (SecurityException ex) {
    // expected
    }
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) Message(org.apache.cxf.message.Message) WSSecurityException(org.apache.wss4j.common.ext.WSSecurityException) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 57 with AuthorizationPolicy

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

the class AuthPolicyValidatingInterceptorTest method testInvalidUsernamePassword.

@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());
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) Message(org.apache.cxf.message.Message) MessageImpl(org.apache.cxf.message.MessageImpl) Test(org.junit.Test)

Example 58 with AuthorizationPolicy

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

the class JAXRSJaasSecurityTest method testJaasFilterAuthenticationFailure.

@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());
}
Also used : Response(javax.ws.rs.core.Response) AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 59 with AuthorizationPolicy

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

the class JAXRSJaasSecurityTest method testJaasFilterWebClientAuthorizationPolicy.

@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());
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) Book(org.apache.cxf.systest.jaxrs.Book) WebClient(org.apache.cxf.jaxrs.client.WebClient) Test(org.junit.Test)

Example 60 with AuthorizationPolicy

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

the class ClientServerTest method testBasicAuth.

@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();
    }
}
Also used : HTTPConduit(org.apache.cxf.transport.http.HTTPConduit) AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) Greeter(org.apache.hello_world_soap_http.Greeter) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) SOAPService(org.apache.hello_world_soap_http.SOAPService) Service(javax.xml.ws.Service) ExecutorService(java.util.concurrent.ExecutorService) BindingProvider(javax.xml.ws.BindingProvider) Client(org.apache.cxf.endpoint.Client) SOAPServiceMultiPortTypeTest(org.apache.hello_world_soap_http.SOAPServiceMultiPortTypeTest) SOAPServiceBogusAddressTest(org.apache.hello_world_soap_http.SOAPServiceBogusAddressTest) Test(org.junit.Test)

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