Search in sources :

Example 21 with Base64Exception

use of org.apache.cxf.common.util.Base64Exception in project tesb-rt-se by Talend.

the class SecurityContextFilter method filter.

@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
    Message message = JAXRSUtils.getCurrentMessage();
    SecurityContext sc = message.get(SecurityContext.class);
    if (sc != null) {
        Principal principal = sc.getUserPrincipal();
        if (principal != null && users.containsKey(principal.getName())) {
            return;
        }
    }
    List<String> authValues = headers.getRequestHeader("Authorization");
    if (authValues == null || authValues.size() != 1) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String[] values = authValues.get(0).split(" ");
    if (values.length != 2 || !"Basic".equals(values[0])) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String decodedValue = null;
    try {
        decodedValue = new String(Base64Utility.decode(values[1]));
    } catch (Base64Exception ex) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    final String[] namePassword = decodedValue.split(":");
    if (namePassword.length != 2) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    String password = users.get(namePassword[0]);
    if (password == null || !password.equals(namePassword[1])) {
        requestContext.abortWith(createFaultResponse());
        return;
    }
    final SecurityContext newSc = new SecurityContext() {

        public Principal getUserPrincipal() {
            return new SimplePrincipal(namePassword[0]);
        }

        public boolean isUserInRole(String arg0) {
            return false;
        }
    };
    message.put(SecurityContext.class, newSc);
}
Also used : Message(org.apache.cxf.message.Message) Base64Exception(org.apache.cxf.common.util.Base64Exception) SecurityContext(org.apache.cxf.security.SecurityContext) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal) Principal(java.security.Principal) SimplePrincipal(org.apache.cxf.common.security.SimplePrincipal)

Aggregations

Base64Exception (org.apache.cxf.common.util.Base64Exception)21 EncryptionException (com.evolveum.midpoint.prism.crypto.EncryptionException)4 ByteArrayInputStream (java.io.ByteArrayInputStream)4 Certificate (java.security.cert.Certificate)4 SimplePrincipal (org.apache.cxf.common.security.SimplePrincipal)4 Message (org.apache.cxf.message.Message)4 SecurityContext (org.apache.cxf.security.SecurityContext)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 CertificateException (java.security.cert.CertificateException)3 X509Certificate (java.security.cert.X509Certificate)3 WSSecurityException (org.apache.wss4j.common.ext.WSSecurityException)3 InputStreamReader (java.io.InputStreamReader)2 Principal (java.security.Principal)2 PrivateKey (java.security.PrivateKey)2 RSAPublicKey (java.security.interfaces.RSAPublicKey)2 DataFormatException (java.util.zip.DataFormatException)2 Cipher (javax.crypto.Cipher)2 Response (javax.ws.rs.core.Response)2 AuthorizationPolicy (org.apache.cxf.configuration.security.AuthorizationPolicy)2