Class LdapConnection

java.lang.Object
com.codename1.backend.security.ldap.LdapConnection
All Implemented Interfaces:
AutoCloseable

public final class LdapConnection extends Object implements AutoCloseable

One LDAPv3 session (RFC 4511) on one socket: simple bind, search, StartTLS and unbind -- what signing users in against a directory needs, and no more. Not shared between threads; open one per sign-in, as LdapContextSource does, and close it.

Only simple binds are made, so the password travels as the server reads it: over plain ldap:// without StartTLS, anyone on the network path reads every password. Use ldaps://, or StartTLS.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The scope of a search: the base entry alone.
    static final int
    The scope of a search: the base entry's children.
    static final int
    The scope of a search: the base entry and everything under it.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    bind(String dn, String password)
    A simple bind as dn.
    void
    Says goodbye and closes the socket.
    boolean
    Whether the session is encrypted.
    open(String url, boolean startTls, String caFile, int connectTimeoutMillis, int readTimeoutMillis)
     
    search(String base, int scope, String filter, int sizeLimit, int timeLimitSeconds, String... attributes)
    Searches base for filter and answers the entries found, with the attributes asked for (none: all user attributes).

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • SCOPE_BASE

      public static final int SCOPE_BASE
      The scope of a search: the base entry alone.
      See Also:
    • SCOPE_ONE_LEVEL

      public static final int SCOPE_ONE_LEVEL
      The scope of a search: the base entry's children.
      See Also:
    • SCOPE_SUBTREE

      public static final int SCOPE_SUBTREE
      The scope of a search: the base entry and everything under it.
      See Also:
  • Method Details

    • open

      public static LdapConnection open(String url, boolean startTls, String caFile, int connectTimeoutMillis, int readTimeoutMillis) throws IOException
      Parameters:
      readTimeoutMillis - how long an answer may take; zero waits forever
      Throws:
      IOException
    • isSecure

      public boolean isSecure()
      Whether the session is encrypted.
    • bind

      public void bind(String dn, String password) throws IOException

      A simple bind as dn.

      An empty password is refused here rather than sent: RFC 4513 section 5.1.2 makes a name with no password an unauthenticated bind, which many directories answer with success -- and a sign-in that checked only that would let anyone in as anyone.

      Throws:
      LdapException - with LdapException.INVALID_CREDENTIALS when the directory refuses the password
      IOException
    • search

      public List<LdapEntry> search(String base, int scope, String filter, int sizeLimit, int timeLimitSeconds, String... attributes) throws IOException
      Searches base for filter and answers the entries found, with the attributes asked for (none: all user attributes).
      Parameters:
      filter - an RFC 4515 filter; escape what a user typed with LdapEncoder.filterEncode(String)
      sizeLimit - the most entries wanted; zero for the server's limit. More than that is LdapException.SIZE_LIMIT_EXCEEDED.
      Throws:
      IOException
    • close

      public void close()
      Says goodbye and closes the socket. Never throws.
      Specified by:
      close in interface AutoCloseable