Skip to content

Commit

Permalink
Inline all methods in Ldap utility
Browse files Browse the repository at this point in the history
- Lets me delete this class entirely. Utility need isn't as widespread
  as I thought it would be.
  • Loading branch information
argherna committed Aug 7, 2018
1 parent c9deaa5 commit 913b28e
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 114 deletions.
33 changes: 30 additions & 3 deletions src/main/java/com/github/argherna/pike/BaseLdapHandler.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package com.github.argherna.pike;

import java.io.IOException;
import java.util.Hashtable;
import java.util.List;
import java.util.logging.Logger;

import javax.naming.ldap.LdapContext;
import javax.naming.Context;
import javax.naming.directory.DirContext;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.StartTlsRequest;
import javax.naming.ldap.StartTlsResponse;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;

abstract class BaseLdapHandler implements HttpHandler {

private static final Logger LOGGER = Logger.getLogger(BaseLdapHandler.class.getName());

@Override
public void handle(HttpExchange exchange) throws IOException {
var headers = exchange.getRequestHeaders();
Expand Down Expand Up @@ -46,9 +54,28 @@ private void internalDoHtml(HttpExchange exchange) throws IOException {

abstract void doJson(HttpExchange exchange) throws IOException;

LdapContext getLdapContext() throws IOException {
DirContext getLdapContext() throws IOException {
try {
return Ldap.createLdapContext(Settings.getActiveConnectionName());
var connection = Settings.getConnectionSettings(Settings.getActiveConnectionName());
var env = new Hashtable<String, Object>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, connection.getLdapUrl());
var ldapContext = new InitialLdapContext(env, null);
if (connection.getUseStartTls()) {
LOGGER.fine("Starting TLS session...");
var tls = (StartTlsResponse) ldapContext.extendedOperation(new StartTlsRequest());
tls.negotiate();
}
ldapContext.addToEnvironment(Context.SECURITY_AUTHENTICATION, connection.getAuthType().toLowerCase());
if (!connection.getAuthType().toLowerCase().equals("none")) {
LOGGER.fine("Authenticating...");
ldapContext.addToEnvironment(Context.SECURITY_PRINCIPAL, connection.getBindDn());
ldapContext.addToEnvironment(Context.SECURITY_CREDENTIALS,
new String(Settings.byteArrayToSecretText(connection.getBindDn(), connection.getPassword())));
}
ldapContext.addToEnvironment(Context.REFERRAL, connection.getReferralPolicy().toLowerCase());
LOGGER.fine("Ldap context successfully created!");
return ldapContext;
} catch (Exception e) {
if (e instanceof IOException) {
throw (IOException) e;
Expand Down
108 changes: 0 additions & 108 deletions src/main/java/com/github/argherna/pike/Ldap.java

This file was deleted.

10 changes: 9 additions & 1 deletion src/main/java/com/github/argherna/pike/Maps.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.argherna.pike;

import java.net.URI;
import java.util.ArrayList;
import java.util.Map;
import java.util.stream.Collectors;
Expand All @@ -18,13 +19,20 @@ static Map<String, Object> toMap(Settings.ConnectionSettings connSettings) {
return Map
.of("name", Strings.nullToEmpty(connSettings.getName()), "ldapUrl",
Strings.nullToEmpty(connSettings.getLdapUrl()), "host",
Ldap.getLdapHost(Strings.nullToEmpty(connSettings.getLdapUrl())), "baseDn",
getLdapHost(Strings.nullToEmpty(connSettings.getLdapUrl())), "baseDn",
Strings.nullToEmpty(connSettings.getBaseDn()), "authType", Strings.nullToEmpty(connSettings.getAuthType()),
"bindDn", Strings.nullToEmpty(connSettings.getBindDn()), "useStartTls", connSettings.getUseStartTls())
.entrySet().stream().filter(e -> e.getValue().toString().length() > 0)
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
}

private static String getLdapHost(String ldapUrl) {
if (Strings.isNullOrEmpty(ldapUrl)) {
return "unknown";
}
return URI.create(ldapUrl).getHost();
}

static Map<String, Object> toMap(Settings.SearchSettings searchSettings) {
return Map
.of("name", Strings.nullToEmpty(searchSettings.getName()), "rdn", Strings.nullToEmpty(searchSettings.getRdn()),
Expand Down
32 changes: 30 additions & 2 deletions src/main/java/com/github/argherna/pike/SearchHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.function.Function;

import javax.naming.NamingException;
import javax.naming.directory.SearchControls;

import com.sun.net.httpserver.HttpExchange;

Expand Down Expand Up @@ -54,12 +55,12 @@ void doJson(HttpExchange exchange) throws IOException {
if (rawQuery != null && !rawQuery.isEmpty()) {
parameters = Http.queryToMap(rawQuery, PARAM_PROCS);
rdn = parameters.containsKey("rdn") ? parameters.get("rdn").get(0) : null;
filter = Ldap.getFilter(parameters);
filter = parameters.containsKey("filter") ? parameters.get("filter").get(0) : "(objectClass=*)";
attrs = parameters.get("attr");
scope = parameters.containsKey("scope") ? parameters.get("scope").get(0) : "subtree";

var searchBase = getSearchBase(rdn);
var searchControls = Ldap.getSearchControls(parameters);
var searchControls = getSearchControls(parameters);
var results = ldapContext.search(searchBase, filter, searchControls);
if (results.hasMoreElements()) {
records = new ArrayList<>();
Expand Down Expand Up @@ -111,4 +112,31 @@ private String getSearchBase(String rdn) {
return sj.toString();
}
}

private SearchControls getSearchControls(Map<String, List<String>> parameters) {
// Do a subtree search by default. If another (valid) scope is specified
// then search with that.
var scope = SearchControls.SUBTREE_SCOPE;
if (parameters.containsKey("scope")) {
var value = parameters.get("scope").get(0);
if (value.equalsIgnoreCase("object")) {
scope = SearchControls.OBJECT_SCOPE;
} else if (value.equalsIgnoreCase("onelevel")) {
scope = SearchControls.ONELEVEL_SCOPE;
}
}

String[] returningAttributes = null;
if (parameters.containsKey("attr")) {
var value = parameters.get("attr");
if (value != null && !value.isEmpty()) {
returningAttributes = value.toArray(new String[value.size()]);
}
}

var searchControls = new SearchControls();
searchControls.setSearchScope(scope);
searchControls.setReturningAttributes(returningAttributes);
return searchControls;
}
}

0 comments on commit 913b28e

Please sign in to comment.