Search in sources :

Example 1 with LdapJsonBindRequest

use of com.tremolosecurity.ldapJson.LdapJsonBindRequest in project OpenUnison by TremoloSecurity.

the class LdapOnJson method doFilter.

@Override
public void doFilter(HttpFilterRequest request, HttpFilterResponse response, HttpFilterChain chain) throws Exception {
    // every request is distinct, logout immediately
    chain.setLogout(true);
    request.setAttribute("com.tremolosecurity.unison.proxy.noRedirectOnError", "com.tremolosecurity.unison.proxy.noRedirectOnError");
    response.setContentType("application/json");
    try {
        URL reqURL;
        reqURL = new URL(request.getRequestURL().toString());
        String[] parts = reqURL.getPath().split("[/]");
        if (request.getServletRequest().getMethod().equalsIgnoreCase("get")) {
            ldapSearh(request, response, parts);
        } else if (request.getServletRequest().getMethod().equalsIgnoreCase("post")) {
            String dn = URLDecoder.decode(parts[parts.length - 1], "UTF-8");
            LdapJsonBindRequest bindReq = gson.fromJson(new String((byte[]) request.getAttribute(ProxySys.MSG_BODY)), LdapJsonBindRequest.class);
            GlobalEntries.getGlobalEntries().getConfigManager().getMyVD().bind(dn, bindReq.getPassword());
            // no errors so we're good
            response.setContentType("application/json");
            response.getWriter().println(gson.toJson(new LdapJsonError()));
        } else {
            throw new LDAPException(LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), LDAPException.OPERATIONS_ERROR, "Invalid operation : '" + request.getMethod() + "'");
        }
    } catch (LDAPException e) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(baos));
        LdapJsonError err = new LdapJsonError();
        err.setResponseCode(e.getResultCode());
        err.setErrorMessage(new String(baos.toByteArray()));
        response.setStatus(500);
        response.getWriter().println(gson.toJson(err));
    } catch (Exception e) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        e.printStackTrace(new PrintStream(baos));
        LdapJsonError err = new LdapJsonError();
        err.setResponseCode(LDAPException.OPERATIONS_ERROR);
        err.setErrorMessage(new String(baos.toByteArray()));
        response.setStatus(500);
        response.getWriter().println(gson.toJson(err));
    }
}
Also used : PrintStream(java.io.PrintStream) LdapJsonError(com.tremolosecurity.ldapJson.LdapJsonError) LDAPException(com.novell.ldap.LDAPException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) LDAPException(com.novell.ldap.LDAPException) LdapJsonBindRequest(com.tremolosecurity.ldapJson.LdapJsonBindRequest)

Example 2 with LdapJsonBindRequest

use of com.tremolosecurity.ldapJson.LdapJsonBindRequest in project OpenUnison by TremoloSecurity.

the class OpenUnisonRestful method bind.

@Override
public void bind(BindInterceptorChain chain, DistinguishedName dn, Password pwd, LDAPConstraints constraints) throws LDAPException {
    String localBindDN = this.getRemoteMappedDN(dn.getDN()).toString();
    HttpCon con;
    try {
        con = this.createClient();
    } catch (Exception e) {
        throw new LDAPException(LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), LDAPException.OPERATIONS_ERROR, "Could not create connection", e);
    }
    try {
        LdapJsonBindRequest bindRequest = new LdapJsonBindRequest();
        bindRequest.setPassword(new String(pwd.getValue()));
        StringBuffer b = new StringBuffer();
        b.append(this.uriPath).append('/').append(URLEncoder.encode(localBindDN, "UTF-8"));
        StringBuffer urlBuffer = new StringBuffer();
        urlBuffer.append(this.urlBase);
        urlBuffer.append(b);
        HttpPost post = new HttpPost(urlBuffer.toString());
        this.addAuthorizationHeader(b.toString(), post);
        StringEntity str = new StringEntity(gson.toJson(bindRequest), ContentType.APPLICATION_JSON);
        post.setEntity(str);
        HttpResponse resp = con.getHttp().execute(post);
        String json = EntityUtils.toString(resp.getEntity());
        LdapJsonError ldapResponse = gson.fromJson(json, LdapJsonError.class);
        if (ldapResponse.getResponseCode() != 0) {
            throw new LDAPException(LDAPException.resultCodeToString(ldapResponse.getResponseCode()), ldapResponse.getResponseCode(), ldapResponse.getErrorMessage());
        }
    } catch (LDAPException e) {
        throw e;
    } catch (Exception e) {
        throw new LDAPException(LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), LDAPException.OPERATIONS_ERROR, "Could not create connection", e);
    } finally {
        if (con != null) {
            try {
                con.getHttp().close();
            } catch (IOException e) {
            // no point
            }
            con.getBcm().close();
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) LdapJsonError(com.tremolosecurity.ldapJson.LdapJsonError) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) IOException(java.io.IOException) LdapJsonBindRequest(com.tremolosecurity.ldapJson.LdapJsonBindRequest)

Aggregations

LdapJsonBindRequest (com.tremolosecurity.ldapJson.LdapJsonBindRequest)2 LdapJsonError (com.tremolosecurity.ldapJson.LdapJsonError)2 LDAPException (com.novell.ldap.LDAPException)1 HttpCon (com.tremolosecurity.provisioning.util.HttpCon)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 PrintStream (java.io.PrintStream)1 URL (java.net.URL)1 HttpResponse (org.apache.http.HttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1