Search in sources :

Example 1 with LdapJsonEntry

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

the class OpenUnisonRestful method search.

@Override
public void search(SearchInterceptorChain chain, DistinguishedName base, Int scope, Filter filter, ArrayList<Attribute> attributes, Bool typesOnly, Results results, LDAPSearchConstraints constraints) throws LDAPException {
    String localBindDN = this.getRemoteMappedDN(base.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 {
        String searchScope;
        switch(scope.getValue()) {
            case 0:
                searchScope = "base";
                break;
            case 1:
                searchScope = "one";
                break;
            case 2:
                searchScope = "sub";
                break;
            default:
                throw new LDAPException(LDAPException.resultCodeToString(LDAPException.OPERATIONS_ERROR), LDAPException.OPERATIONS_ERROR, "Unknown search scope : " + scope.getValue());
        }
        StringBuffer b = new StringBuffer();
        b.append(this.uriPath).append('/').append(URLEncoder.encode(localBindDN, "UTF-8")).append('/').append(URLEncoder.encode(searchScope, "UTF-8"));
        StringBuffer urlBuffer = new StringBuffer();
        urlBuffer.append(this.urlBase).append(b);
        urlBuffer.append("?filter=").append(URLEncoder.encode(filter.getRoot().toString(), "UTF-8"));
        for (Attribute attribute : attributes) {
            urlBuffer.append("&attributes=").append(URLEncoder.encode(attribute.getAttribute().getName(), "UTF-8"));
        }
        HttpGet get = new HttpGet(urlBuffer.toString());
        this.addAuthorizationHeader(b.toString(), get);
        HttpResponse resp = con.getHttp().execute(get);
        String json = EntityUtils.toString(resp.getEntity());
        if (resp.getStatusLine().getStatusCode() == 200) {
            ArrayList<Entry> toReturn = new ArrayList<Entry>();
            Type listType = new TypeToken<List<LdapJsonEntry>>() {
            }.getType();
            List<LdapJsonEntry> returned = gson.fromJson(json, listType);
            for (LdapJsonEntry fromServer : returned) {
                LDAPAttributeSet attrs = new LDAPAttributeSet();
                for (String attrName : fromServer.getAttrs().keySet()) {
                    LDAPAttribute attr = new LDAPAttribute(attrName);
                    for (String value : fromServer.getAttrs().get(attrName)) {
                        attr.addValue(value);
                    }
                    attrs.add(attr);
                }
                LDAPEntry ldapEntry = new LDAPEntry(this.getLocalMappedDN(new DN(fromServer.getDn())).toString(), attrs);
                toReturn.add(new Entry(ldapEntry));
            }
            chain.addResult(results, new IteratorEntrySet(toReturn.iterator()), base, scope, filter, attributes, typesOnly, constraints);
        } else {
            LdapJsonError ldapResponse = gson.fromJson(json, LdapJsonError.class);
            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 : HttpGet(org.apache.http.client.methods.HttpGet) ArrayList(java.util.ArrayList) LdapJsonEntry(com.tremolosecurity.ldapJson.LdapJsonEntry) HttpResponse(org.apache.http.HttpResponse) DN(com.novell.ldap.util.DN) IOException(java.io.IOException) IOException(java.io.IOException) IteratorEntrySet(net.sourceforge.myvd.util.IteratorEntrySet) LdapJsonEntry(com.tremolosecurity.ldapJson.LdapJsonEntry) ContentType(org.apache.http.entity.ContentType) Type(java.lang.reflect.Type) HttpCon(com.tremolosecurity.provisioning.util.HttpCon) LdapJsonError(com.tremolosecurity.ldapJson.LdapJsonError) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with LdapJsonEntry

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

the class LdapOnJson method ldapSearh.

private void ldapSearh(HttpFilterRequest request, HttpFilterResponse response, String[] parts) throws Exception {
    Attribute attributes = request.getParameter("attributes");
    String filter = request.getParameter("filter").getValues().get(0);
    String scope = URLDecoder.decode(parts[parts.length - 1], "UTF-8");
    ;
    String dn = URLDecoder.decode(parts[parts.length - 2], "UTF-8");
    ;
    int searchScope = 0;
    switch(scope) {
        case "sub":
            searchScope = 2;
            break;
        case "one":
            searchScope = 1;
            break;
        case "base":
            searchScope = 0;
            break;
        default:
            throw new Exception("Invalid search scope : '" + scope + "'");
    }
    ArrayList<String> attrsForSearch = new ArrayList<String>();
    if (attributes != null) {
        attrsForSearch.addAll(attributes.getValues());
    }
    LDAPSearchResults res = GlobalEntries.getGlobalEntries().getConfigManager().getMyVD().search(dn, searchScope, filter, attrsForSearch);
    ArrayList<LdapJsonEntry> entries = new ArrayList<LdapJsonEntry>();
    while (res.hasMore()) {
        LDAPEntry entry = res.next();
        LdapJsonEntry jsonEntry = new LdapJsonEntry();
        jsonEntry.setDn(entry.getDN());
        for (Object o : entry.getAttributeSet()) {
            LDAPAttribute attr = (LDAPAttribute) o;
            jsonEntry.getAttrs().put(attr.getName(), Arrays.asList(attr.getStringValueArray()));
        }
        entries.add(jsonEntry);
    }
    response.getWriter().println(gson.toJson(entries));
}
Also used : LDAPAttribute(com.novell.ldap.LDAPAttribute) LDAPEntry(com.novell.ldap.LDAPEntry) LDAPSearchResults(com.novell.ldap.LDAPSearchResults) Attribute(com.tremolosecurity.saml.Attribute) LDAPAttribute(com.novell.ldap.LDAPAttribute) ArrayList(java.util.ArrayList) LdapJsonEntry(com.tremolosecurity.ldapJson.LdapJsonEntry) LDAPException(com.novell.ldap.LDAPException)

Aggregations

LdapJsonEntry (com.tremolosecurity.ldapJson.LdapJsonEntry)2 ArrayList (java.util.ArrayList)2 LDAPAttribute (com.novell.ldap.LDAPAttribute)1 LDAPEntry (com.novell.ldap.LDAPEntry)1 LDAPException (com.novell.ldap.LDAPException)1 LDAPSearchResults (com.novell.ldap.LDAPSearchResults)1 DN (com.novell.ldap.util.DN)1 LdapJsonError (com.tremolosecurity.ldapJson.LdapJsonError)1 HttpCon (com.tremolosecurity.provisioning.util.HttpCon)1 Attribute (com.tremolosecurity.saml.Attribute)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1 List (java.util.List)1 IteratorEntrySet (net.sourceforge.myvd.util.IteratorEntrySet)1 HttpResponse (org.apache.http.HttpResponse)1 HttpGet (org.apache.http.client.methods.HttpGet)1 ContentType (org.apache.http.entity.ContentType)1