mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Mark Craig
28.46.2011 7324c9d93c3eb95c53a095a2fa7e402d9ec345e7
Some stuff lying around
2 files added
1 files modified
273 ■■■■■ changed files
opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/simpleauth/Main.java 182 ●●●●● patch | view | raw | blame | history
opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/simpleauth/package-info.java 36 ●●●●● patch | view | raw | blame | history
opendj3/src/main/docbkx/dev-guide/chap-authenticating.xml 55 ●●●●● patch | view | raw | blame | history
opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/simpleauth/Main.java
New file
@@ -0,0 +1,182 @@
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opendj3/legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opendj3/legal-notices/CDDLv1_0.txt.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2011 ForgeRock AS
 */
package org.forgerock.opendj.examples.simpleauth;
import org.forgerock.opendj.ldap.*;
/**
 * An example client application which performs simple authentication to a
 * directory server. This example takes the following command line parameters:
 * <ul>
 * <li>host - host name of the directory server</li>
 * <li>port - port number of the directory server, e.g. 1389, 1636</li>
 * <li>bind-dn - DN of the user to authenticate</li>
 * <li>bind-password - Password of the user to authenticate</li>
 * <li>use-starttls - (Optional) connect with StartTLS</li>
 * <li>use-ssl - (Optional) connect over SSL</li>
 * </ul>
 * The host, port, bind-dn, and bind-password are required. The use-starttls
 * and use-ssl parameters are optional and mutually exclusive.
 */
public final class Main
{
  /**
   * Authenticate to the directory either over LDAP, over LDAPS, or using
   * StartTLS.
   *
   * @param args The command line arguments
   */
  public static void main(final String[] args)
  {
    parseArgs(args);
    // Connect and bind to the server, then close the connection.
    if (useStartTLS) connectStartTLS();
    else if (useSSL) connectSSL();
    else connect();
  }
  /**
   * Authenticate over LDAP.
   */
  private static void connect()
  {
    final LDAPConnectionFactory factory = new LDAPConnectionFactory(
      host, port);
    Connection connection = null;
    try
    {
      connection = factory.getConnection();
      connection.bind(bindDN, bindPassword.toCharArray());
      System.out.println("Authenticated as " + bindDN + ".");
    }
    catch (final ErrorResultException e)
    {
      System.err.println(e.getMessage());
      System.exit(e.getResult().getResultCode().intValue());
      return;
    }
    catch (final InterruptedException e)
    {
      System.err.println(e.getMessage());
      System.exit(ResultCode.CLIENT_SIDE_USER_CANCELLED.intValue());
      return;
    }
    finally
    {
      if (connection != null) connection.close();
    }
  }
  /**
   * Authenticate using StartTLS.
   */
  private static void connectStartTLS()
  {
    connect(); // Not implemented yet.
  }
  /**
   * Authenticate over LDAPS.
   */
  private static void connectSSL()
  {
    connect(); // Not implemented yet.
  }
  private static String host;
  private static int port;
  private static String bindDN;
  private static String bindPassword;
  private static boolean useStartTLS = false;
  private static boolean useSSL = false;
  /**
   * Parse command line arguments.
   * @param args host port bind-dn bind-password [ use-starttls | use-ssl ]
   */
  private static void parseArgs(String[] args)
  {
    if (args.length < 4 || args.length > 5) giveUp();
    host = args[0];
    port = Integer.parseInt(args[1]);
    bindDN = args[2];
    bindPassword = args[3];
    if (args.length == 5)
    {
      if (args[4].toLowerCase().equals("use-starttls"))
      {
        useStartTLS = true;
        useSSL = false;
      }
      else if (args[4].toLowerCase().equals("use-ssl"))
      {
        useStartTLS = false;
        useSSL = true;
      }
      else giveUp();
    }
  }
  private static void giveUp()
  {
    printUsage();
    System.exit(1);
  }
  private static void printUsage()
  {
    System.err.println(
      "Usage: host port bind-dn bind-password [ use-starttls | use-ssl ]");
    System.err.println(
      "\thost, port, bind-dn, and bind-password arguments are required.");
    System.err.println(
      "\tuse-starttls and use-ssl are optional and mutually exclusive.");
  }
}
opendj3/opendj-ldap-sdk-examples/src/main/java/org/forgerock/opendj/examples/simpleauth/package-info.java
New file
@@ -0,0 +1,36 @@
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at
 * trunk/opendj3/legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at
 * trunk/opendj3/legal-notices/CDDLv1_0.txt.  If applicable,
 * add the following below this CDDL HEADER, with the fields enclosed
 * by brackets "[]" replaced with your own identifying information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2009-2010 Sun Microsystems, Inc.
 *      Portions copyright 2011 ForgeRock AS
 */
/**
 * An example client application which performs simple authentication to a
 * Directory Server, displays a result, and closes the connection.
 */
package org.forgerock.opendj.examples.simpleauth;
opendj3/src/main/docbkx/dev-guide/chap-authenticating.xml
@@ -65,31 +65,38 @@
  the directory determines authorization for operations on the connection
  based on the users identity.</para>
  
  <programlisting language="java">// LDAP simple authentication
final LDAPConnectionFactory factory = new LDAPConnectionFactory(
    hostName, port);
Connection connection = null;
try
{
  connection = factory.getConnection();
  connection.bind(userName, password.toCharArray());
  System.out.println("Authenticated as " + userName + ".");
  // Perform LDAP operations here.
}
// Catch any exceptions here, and then close the connection.
finally
{
  if (connection != null)
  <programlisting language="java">  /**
   * Authenticate over LDAP.
   */
  private static void connect()
  {
    connection.close();
  }
}</programlisting>
    final LDAPConnectionFactory factory = new LDAPConnectionFactory(
      host, port);
    Connection connection = null;
    try
    {
      connection = factory.getConnection();
      connection.bind(bindDN, bindPassword.toCharArray());
      System.out.println("Authenticated as " + bindDN + ".");
    }
    catch (final ErrorResultException e)
    {
      System.err.println(e.getMessage());
      System.exit(e.getResult().getResultCode().intValue());
      return;
    }
    catch (final InterruptedException e)
    {
      System.err.println(e.getMessage());
      System.exit(ResultCode.CLIENT_SIDE_USER_CANCELLED.intValue());
      return;
    }
    finally
    {
      if (connection != null) connection.close();
    }
  }</programlisting>
  
  <para>If the password values do not match, a directory might nevertheless
  authenticate the client application. The LDAP specifications say that in this