Search in sources :

Example 6 with AuthorizationPolicy

use of org.apache.cxf.configuration.security.AuthorizationPolicy in project carbon-apimgt by wso2.

the class XACMLAuthenticationInterceptor method handleRequest.

/**
 * isUserPermitted requests received at the ml endpoint, using HTTP basic-auth headers as the authentication
 * mechanism. This method returns a null value which indicates that the request to be processed.
 */
public boolean handleRequest(Message message, ClassResourceInfo resourceInfo) {
    if (logger.isDebugEnabled()) {
        logger.debug(String.format("Authenticating request: " + message.getId()));
    }
    AuthorizationPolicy policy = message.get(AuthorizationPolicy.class);
    if (policy == null) {
        logger.error("Authentication failed: Basic authentication header is missing");
        return false;
    }
    Object certObject = null;
    String username = StringUtils.trim(policy.getUserName());
    if (StringUtils.isEmpty(username)) {
        logger.error("Username cannot be null/empty.");
        return false;
    }
    return isUserPermitted(username, (String) message.get(Message.REQUEST_URI), (String) message.get(Message.HTTP_REQUEST_METHOD), null);
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy)

Example 7 with AuthorizationPolicy

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

the class WrappedMessageContext method put.

public final Object put(String key, Object value, Scope scope) {
    String mappedKey = mapKey(key);
    if (!MessageContext.MESSAGE_OUTBOUND_PROPERTY.equals(mappedKey)) {
        scopes.put(mappedKey, scope);
    }
    Object ret = null;
    if ((MessageContext.HTTP_RESPONSE_HEADERS.equals(key) || MessageContext.HTTP_RESPONSE_CODE.equals(key)) && !isResponse() && !isRequestor()) {
        Message tmp = createResponseMessage();
        if (tmp != null) {
            if (MessageContext.HTTP_RESPONSE_HEADERS.equals(key)) {
                return tmp.put(Message.PROTOCOL_HEADERS, value);
            }
            return tmp.put(mappedKey, value);
        }
    } else if (BindingProvider.USERNAME_PROPERTY.equals(key)) {
        AuthorizationPolicy authPolicy = (AuthorizationPolicy) message.get(AuthorizationPolicy.class.getName());
        if (authPolicy == null) {
            authPolicy = new AuthorizationPolicy();
            message.put(AuthorizationPolicy.class.getName(), authPolicy);
        }
        ret = authPolicy.getUserName();
        authPolicy.setUserName((String) value);
    } else if (BindingProvider.PASSWORD_PROPERTY.equals(key)) {
        AuthorizationPolicy authPolicy = (AuthorizationPolicy) message.get(AuthorizationPolicy.class.getName());
        if (authPolicy == null) {
            authPolicy = new AuthorizationPolicy();
            message.put(AuthorizationPolicy.class.getName(), authPolicy);
        }
        ret = authPolicy.getPassword();
        authPolicy.setPassword((String) value);
    } else if (MessageContext.HTTP_REQUEST_HEADERS.equals(key)) {
        ret = message.put(Message.PROTOCOL_HEADERS, value);
    } else if (MessageContext.OUTBOUND_MESSAGE_ATTACHMENTS.equals(key)) {
        Map<String, DataHandler> attachments = CastUtils.cast((Map<?, ?>) value);
        ret = message.put(Message.ATTACHMENTS, new WrappedAttachments(attachments));
    } else if (SoapBindingConstants.SOAP_ACTION.equals(mappedKey) && !isRequestor() && exchange != null) {
        Message tmp = createResponseMessage();
        if (tmp != null) {
            tmp.put(mappedKey, value);
        }
    } else {
        ret = message.put(mappedKey, value);
    }
    return ret;
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) Message(org.apache.cxf.message.Message) DataHandler(javax.activation.DataHandler)

Example 8 with AuthorizationPolicy

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

the class JAASLoginInterceptorTest method addAuthPolicy.

private void addAuthPolicy(Message message, String username, String password) {
    AuthorizationPolicy authPol = new AuthorizationPolicy();
    authPol.setUserName(username);
    authPol.setPassword(password);
    message.put(AuthorizationPolicy.class, authPol);
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy)

Example 9 with AuthorizationPolicy

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

the class JAXRSSamlTest method testSAMLTokenHeaderUsingAuthorizationPolicy.

@Test
public void testSAMLTokenHeaderUsingAuthorizationPolicy() throws Exception {
    String address = "https://localhost:" + PORT + "/samlheader/bookstore/books/123";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    SpringBusFactory bf = new SpringBusFactory();
    URL busFile = JAXRSSamlTest.class.getResource("client.xml");
    Bus springBus = bf.createBus(busFile.toString());
    bean.setBus(springBus);
    // Create SAML Token
    SAMLCallback samlCallback = new SAMLCallback();
    SAMLUtil.doSAMLCallback(new SamlCallbackHandler(), samlCallback);
    SamlAssertionWrapper assertion = new SamlAssertionWrapper(samlCallback);
    Document doc = DOMUtils.createDocument();
    Element token = assertion.toDOM(doc);
    WebClient wc = bean.createWebClient();
    HTTPConduit http = (HTTPConduit) WebClient.getConfig(wc).getConduit();
    AuthorizationPolicy authorizationPolicy = new AuthorizationPolicy();
    String encodedToken = encodeToken(DOM2Writer.nodeToString(token));
    authorizationPolicy.setAuthorization(encodedToken);
    authorizationPolicy.setAuthorizationType("SAML");
    http.setAuthorization(authorizationPolicy);
    try {
        Book book = wc.get(Book.class);
        assertEquals(123L, book.getId());
    } catch (WebApplicationException ex) {
        fail(ex.getMessage());
    } catch (ProcessingException ex) {
        if (ex.getCause() != null && ex.getCause().getMessage() != null) {
            fail(ex.getCause().getMessage());
        } else {
            fail(ex.getMessage());
        }
    }
}
Also used : Bus(org.apache.cxf.Bus) JAXRSClientFactoryBean(org.apache.cxf.jaxrs.client.JAXRSClientFactoryBean) WebApplicationException(javax.ws.rs.WebApplicationException) Element(org.w3c.dom.Element) SamlAssertionWrapper(org.apache.wss4j.common.saml.SamlAssertionWrapper) Document(org.w3c.dom.Document) WebClient(org.apache.cxf.jaxrs.client.WebClient) URL(java.net.URL) HTTPConduit(org.apache.cxf.transport.http.HTTPConduit) AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) SpringBusFactory(org.apache.cxf.bus.spring.SpringBusFactory) Book(org.apache.cxf.systest.jaxrs.security.Book) SAMLCallback(org.apache.wss4j.common.saml.SAMLCallback) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 10 with AuthorizationPolicy

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

the class AuthPolicyValidatingInterceptor method handleMessage.

public void handleMessage(Message message) throws Fault {
    AuthorizationPolicy policy = message.get(AuthorizationPolicy.class);
    if (policy == null || policy.getUserName() == null || policy.getPassword() == null) {
        String name = null;
        if (policy != null) {
            name = policy.getUserName();
        }
        org.apache.cxf.common.i18n.Message errorMsg = new org.apache.cxf.common.i18n.Message("NO_USER_PASSWORD", BUNDLE, name);
        LOG.warning(errorMsg.toString());
        throw new SecurityException(errorMsg.toString());
    }
    try {
        super.validate(message);
    } catch (Exception ex) {
        throw new Fault(ex);
    }
}
Also used : AuthorizationPolicy(org.apache.cxf.configuration.security.AuthorizationPolicy) Message(org.apache.cxf.message.Message) Fault(org.apache.cxf.interceptor.Fault)

Aggregations

AuthorizationPolicy (org.apache.cxf.configuration.security.AuthorizationPolicy)85 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 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 TLSClientParameters (org.apache.cxf.configuration.jsse.TLSClientParameters)4