jdk/src/jdk.security.auth/share/classes/com/sun/security/auth/module/LdapLoginModule.java
author weijun
Fri, 12 Aug 2016 10:20:13 +0800
changeset 43198 7fc878d49203
parent 42338 a60f280f803c
child 43243 a48dab17a356
permissions -rw-r--r--
8161743: Provide proper login context Reviewed-by: ahgross, vinnie, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21278
diff changeset
     2
 * Copyright (c) 2005, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.security.auth.module;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.security.AccessController;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.net.SocketPermission;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.Principal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.PrivilegedAction;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.util.Arrays;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.util.Hashtable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.util.Map;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.util.ResourceBundle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.util.regex.Matcher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.util.regex.Pattern;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.util.Set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.naming.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.naming.directory.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.naming.ldap.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.security.auth.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.security.auth.callback.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import javax.security.auth.login.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import javax.security.auth.spi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
import com.sun.security.auth.LdapPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
import com.sun.security.auth.UserPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * This {@link LoginModule} performs LDAP-based authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * A username and password is verified against the corresponding user
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * credentials stored in an LDAP directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 * This module requires the supplied {@link CallbackHandler} to support a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * {@link NameCallback} and a {@link PasswordCallback}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * If authentication is successful then a new {@link LdapPrincipal} is created
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * using the user's distinguished name and a new {@link UserPrincipal} is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * created using the user's username and both are associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * with the current {@link Subject}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * <p> This module operates in one of three modes: <i>search-first</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 * <i>authentication-first</i> or <i>authentication-only</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * A mode is selected by specifying a particular set of options.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * <p> In search-first mode, the LDAP directory is searched to determine the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * user's distinguished name and then authentication is attempted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * An (anonymous) search is performed using the supplied username in
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 * conjunction with a specified search filter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 * If successful then authentication is attempted using the user's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 * distinguished name and the supplied password.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    73
 * To enable this mode, set the {@code userFilter} option and omit the
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    74
 * {@code authIdentity} option.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 * Use search-first mode when the user's distinguished name is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 * known in advance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * <p> In authentication-first mode, authentication is attempted using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 * supplied username and password and then the LDAP directory is searched.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * If authentication is successful then a search is performed using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * supplied username in conjunction with a specified search filter.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    82
 * To enable this mode, set the {@code authIdentity} and the
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    83
 * {@code userFilter} options.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * Use authentication-first mode when accessing an LDAP directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * that has been configured to disallow anonymous searches.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * <p> In authentication-only mode, authentication is attempted using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * supplied username and password. The LDAP directory is not searched because
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * the user's distinguished name is already known.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    90
 * To enable this mode, set the {@code authIdentity} option to a valid
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    91
 * distinguished name and omit the {@code userFilter} option.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * Use authentication-only mode when the user's distinguished name is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 * known in advance.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
 * <p> The following option is mandatory and must be specified in this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * module's login {@link Configuration}:
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
    97
 * <dl><dd>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * <dt> <code>userProvider=<b>ldap_urls</b></code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 * </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
 * <dd> This option identifies the LDAP directory that stores user entries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
 *      <b>ldap_urls</b> is a list of space-separated LDAP URLs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
 *      (<a href="http://www.ietf.org/rfc/rfc2255.txt">RFC 2255</a>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
 *      that identifies the LDAP server to use and the position in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
 *      its directory tree where user entries are located.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
 *      When several LDAP URLs are specified then each is attempted,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
 *      in turn, until the first successful connection is established.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
 *      Spaces in the distinguished name component of the URL must be escaped
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   109
 *      using the standard mechanism of percent character ('{@code %}')
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
 *      followed by two hexadecimal digits (see {@link java.net.URI}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
 *      Query components must also be omitted from the URL.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
 *      <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
 *      Automatic discovery of the LDAP server via DNS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 *      (<a href="http://www.ietf.org/rfc/rfc2782.txt">RFC 2782</a>)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *      is supported (once DNS has been configured to support such a service).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 *      It is enabled by omitting the hostname and port number components from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
 *      the LDAP URL. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
 * </dl></dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
 * <p> This module also recognizes the following optional {@link Configuration}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
 *     options:
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   123
 * <dl><dd>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
 * <dt> <code>userFilter=<b>ldap_filter</b></code> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
 * <dd> This option specifies the search filter to use to locate a user's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
 *      entry in the LDAP directory. It is used to determine a user's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
 *      distinguished name.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   129
 *      <b>{@code ldap_filter}</b> is an LDAP filter string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
 *      (<a href="http://www.ietf.org/rfc/rfc2254.txt">RFC 2254</a>).
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   131
 *      If it contains the special token "<b>{@code {USERNAME}}</b>"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
 *      then that token will be replaced with the supplied username value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
 *      before the filter is used to search the directory. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
 * <dt> <code>authIdentity=<b>auth_id</b></code> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
 * <dd> This option specifies the identity to use when authenticating a user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
 *      to the LDAP directory.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   138
 *      <b>{@code auth_id}</b> may be an LDAP distinguished name string
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
 *      (<a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a>) or some
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
 *      other string name.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   141
 *      It must contain the special token "<b>{@code {USERNAME}}</b>"
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 *      which will be replaced with the supplied username value before the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *      name is used for authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 *      Note that if this option does not contain a distinguished name then
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   145
 *      the {@code userFilter} option must also be specified. </dd>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
 * <dt> <code>authzIdentity=<b>authz_id</b></code> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
 * <dd> This option specifies an authorization identity for the user.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   149
 *      <b>{@code authz_id}</b> is any string name.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
 *      If it comprises a single special token with curly braces then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
 *      that token is treated as a attribute name and will be replaced with a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
 *      single value of that attribute from the user's LDAP entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
 *      If the attribute cannot be found then the option is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
 *      When this option is supplied and the user has been successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
 *      authenticated then an additional {@link UserPrincipal}
21278
ef8a3a2a72f2 8022746: List of spelling errors in API doc
malenkov
parents: 20742
diff changeset
   156
 *      is created using the authorization identity and it is associated with
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
 *      the current {@link Subject}. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   159
 * <dt> {@code useSSL} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   160
 * <dd> if {@code false}, this module does not establish an SSL connection
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *      to the LDAP server before attempting authentication. SSL is used to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *      protect the privacy of the user's password because it is transmitted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *      in the clear over LDAP.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 *      By default, this module uses SSL. </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   166
 * <dt> {@code useFirstPass} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   167
 * <dd> if {@code true}, this module retrieves the username and password
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
 *      from the module's shared state, using "javax.security.auth.login.name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
 *      and "javax.security.auth.login.password" as the respective keys. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
 *      retrieved values are used for authentication. If authentication fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
 *      no attempt for a retry is made, and the failure is reported back to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
 *      the calling application.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   174
 * <dt> {@code tryFirstPass} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   175
 * <dd> if {@code true}, this module retrieves the username and password
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
 *      from the module's shared state, using "javax.security.auth.login.name"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
 *       and "javax.security.auth.login.password" as the respective keys.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 *      retrieved values are used for authentication. If authentication fails,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 *      the module uses the {@link CallbackHandler} to retrieve a new username
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
 *      and password, and another attempt to authenticate is made. If the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
 *      authentication fails, the failure is reported back to the calling
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
 *      application.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   184
 * <dt> {@code storePass} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   185
 * <dd> if {@code true}, this module stores the username and password
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
 *      obtained from the {@link CallbackHandler} in the module's shared state,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
 *      using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
 *      "javax.security.auth.login.name" and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
 *      "javax.security.auth.login.password" as the respective keys.  This is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
 *      not performed if existing values already exist for the username and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
 *      password in the shared state, or if authentication fails.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   193
 * <dt> {@code clearPass} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   194
 * <dd> if {@code true}, this module clears the username and password
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
 *      stored in the module's shared state after both phases of authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
 *      (login and commit) have completed.</dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   198
 * <dt> {@code debug} </dt>
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   199
 * <dd> if {@code true}, debug messages are displayed on the standard
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
 *      output stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
 * Arbitrary
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
 * <a href="{@docRoot}/../../../../../technotes/guides/jndi/jndi-ldap-gl.html#PROP">JNDI properties</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 * may also be specified in the {@link Configuration}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 * They are added to the environment and passed to the LDAP provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * Note that the following four JNDI properties are set by this module directly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 * and are ignored if also present in the configuration:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
 * <ul>
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   212
 * <li> {@code java.naming.provider.url}
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   213
 * <li> {@code java.naming.security.principal}
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   214
 * <li> {@code java.naming.security.credentials}
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   215
 * <li> {@code java.naming.security.protocol}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
 * Three sample {@link Configuration}s are shown below.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
 * The first one activates search-first mode. It identifies the LDAP server
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   221
 * and specifies that users' entries be located by their {@code uid} and
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   222
 * {@code objectClass} attributes. It also specifies that an identity
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   223
 * based on the user's {@code employeeNumber} attribute should be created.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
 * The second one activates authentication-first mode. It requests that the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
 * LDAP server be located dynamically, that authentication be performed using
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
 * the supplied username directly but without the protection of SSL and that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
 * users' entries be located by one of three naming attributes and their
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   228
 * {@code objectClass} attribute.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
 * The third one activates authentication-only mode. It identifies alternative
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
 * LDAP servers, it specifies the distinguished name to use for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
 * authentication and a fixed identity to use for authorization. No directory
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
 * search is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   234
 * <pre>{@literal
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 *     ExampleApplication {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 *         com.sun.security.auth.module.LdapLoginModule REQUIRED
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   238
 *              userProvider="ldap://ldap-svr/ou=people,dc=example,dc=com"
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   239
 *              userFilter="(&(uid={USERNAME})(objectClass=inetOrgPerson))"
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   240
 *              authzIdentity="{EMPLOYEENUMBER}"
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   241
 *              debug=true;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
 *     ExampleApplication {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
 *         com.sun.security.auth.module.LdapLoginModule REQUIRED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
 *             userProvider="ldap:///cn=users,dc=example,dc=com"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
 *             authIdentity="{USERNAME}"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
 *             userFilter="(&(|(samAccountName={USERNAME})(userPrincipalName={USERNAME})(cn={USERNAME}))(objectClass=user))"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
 *             useSSL=false
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
 *             debug=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
 *     ExampleApplication {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
 *         com.sun.security.auth.module.LdapLoginModule REQUIRED
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
 *             userProvider="ldap://ldap-svr1 ldap://ldap-svr2"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
 *             authIdentity="cn={USERNAME},ou=people,dc=example,dc=com"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
 *             authzIdentity="staff"
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
 *             debug=true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
 *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   261
 * }</pre>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
 * <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
 * <dt><b>Note:</b> </dt>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
 * <dd>When a {@link SecurityManager} is active then an application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
 *     that creates a {@link LoginContext} and uses a {@link LoginModule}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
 *     must be granted certain permissions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
 *     <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
 *     If the application creates a login context using an <em>installed</em>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
 *     {@link Configuration} then the application must be granted the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
 *     {@link AuthPermission} to create login contexts.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
 *     For example, the following security policy allows an application in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 *     the user's current directory to instantiate <em>any</em> login context:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 *     <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 *     grant codebase "file:${user.dir}/" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
 *         permission javax.security.auth.AuthPermission "createLoginContext.*";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
 *     </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
 *     Alternatively, if the application creates a login context using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
 *     <em>caller-specified</em> {@link Configuration} then the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
 *     must be granted the permissions required by the {@link LoginModule}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
 *     <em>This</em> module requires the following two permissions:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
 *     <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
 *     <li> The {@link SocketPermission} to connect to an LDAP server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
 *     <li> The {@link AuthPermission} to modify the set of {@link Principal}s
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
 *          associated with a {@link Subject}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
 *     </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
 *     <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
 *     For example, the following security policy grants an application in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
 *     user's current directory all the permissions required by this module:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
 *     <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 *     grant codebase "file:${user.dir}/" {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 *         permission java.net.SocketPermission "*:389", "connect";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 *         permission java.net.SocketPermission "*:636", "connect";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 *         permission javax.security.auth.AuthPermission "modifyPrincipals";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 *     };
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 *     </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 * </dd>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
 * </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
 * @since 1.6
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
public class LdapLoginModule implements LoginModule {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
    // Use the default classloader for this class to load the prompt strings.
42338
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   309
    private static final ResourceBundle rb =
a60f280f803c 8169069: Module system implementation refresh (11/2016)
alanb
parents: 36511
diff changeset
   310
        ResourceBundle.getBundle("sun.security.util.AuthResources");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    // Keys to retrieve the stored username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    private static final String USERNAME_KEY = "javax.security.auth.login.name";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    private static final String PASSWORD_KEY =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
        "javax.security.auth.login.password";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    // Option names
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
    private static final String USER_PROVIDER = "userProvider";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    private static final String USER_FILTER = "userFilter";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
    private static final String AUTHC_IDENTITY = "authIdentity";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
    private static final String AUTHZ_IDENTITY = "authzIdentity";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    // Used for the username token replacement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private static final String USERNAME_TOKEN = "{USERNAME}";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private static final Pattern USERNAME_PATTERN =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        Pattern.compile("\\{USERNAME\\}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    // Configurable options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    private String userProvider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
    private String userFilter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    private String authcIdentity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    private String authzIdentity;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    private String authzIdentityAttr = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    private boolean useSSL = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    private boolean authFirst = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
    private boolean authOnly = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private boolean useFirstPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
    private boolean tryFirstPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    private boolean storePass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
    private boolean clearPass = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
    private boolean debug = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
    // Authentication status
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    private boolean succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
    private boolean commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    // Supplied username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    private String username;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
    private char[] password;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    // User's identities
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    private LdapPrincipal ldapPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    private UserPrincipal userPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    private UserPrincipal authzPrincipal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
    // Initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    private Subject subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
    private CallbackHandler callbackHandler;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   359
    private Map<String, Object> sharedState;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    private Map<String, ?> options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
    private LdapContext ctx;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    private Matcher identityMatcher = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    private Matcher filterMatcher = null;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   364
    private Hashtable<String, Object> ldapEnvironment;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    private SearchControls constraints = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    /**
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   368
     * Initialize this {@code LoginModule}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   370
     * @param subject the {@code Subject} to be authenticated.
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   371
     * @param callbackHandler a {@code CallbackHandler} to acquire the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     *                  username and password.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   373
     * @param sharedState shared {@code LoginModule} state.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * @param options options specified in the login
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   375
     *                  {@code Configuration} for this particular
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   376
     *                  {@code LoginModule}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     */
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   378
    // Unchecked warning from (Map<String, Object>)sharedState is safe
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   379
    // since javax.security.auth.login.LoginContext passes a raw HashMap.
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   380
    @SuppressWarnings("unchecked")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
    public void initialize(Subject subject, CallbackHandler callbackHandler,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                        Map<String, ?> sharedState, Map<String, ?> options) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        this.subject = subject;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        this.callbackHandler = callbackHandler;
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   386
        this.sharedState = (Map<String, Object>)sharedState;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        this.options = options;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
10336
0bb1999251f8 7064075: Security libraries don't build with javac -Xlint:all,-deprecation -Werror
jjg
parents: 7179
diff changeset
   389
        ldapEnvironment = new Hashtable<String, Object>(9);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        ldapEnvironment.put(Context.INITIAL_CONTEXT_FACTORY,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
            "com.sun.jndi.ldap.LdapCtxFactory");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        // Add any JNDI properties to the environment
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        for (String key : options.keySet()) {
24685
215fa91e1b4c 8044461: Cleanup new Boolean and single character strings
rriggs
parents: 23010
diff changeset
   395
            if (key.indexOf('.') > -1) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                ldapEnvironment.put(key, options.get(key));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        // initialize any configured options
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        userProvider = (String)options.get(USER_PROVIDER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        if (userProvider != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
            ldapEnvironment.put(Context.PROVIDER_URL, userProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        authcIdentity = (String)options.get(AUTHC_IDENTITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        if (authcIdentity != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
            (authcIdentity.indexOf(USERNAME_TOKEN) != -1)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            identityMatcher = USERNAME_PATTERN.matcher(authcIdentity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        userFilter = (String)options.get(USER_FILTER);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        if (userFilter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
            if (userFilter.indexOf(USERNAME_TOKEN) != -1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
                filterMatcher = USERNAME_PATTERN.matcher(userFilter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
            constraints = new SearchControls();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
            constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
            constraints.setReturningAttributes(new String[0]); //return no attrs
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        authzIdentity = (String)options.get(AUTHZ_IDENTITY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        if (authzIdentity != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            authzIdentity.startsWith("{") && authzIdentity.endsWith("}")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            if (constraints != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                authzIdentityAttr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
                    authzIdentity.substring(1, authzIdentity.length() - 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
                constraints.setReturningAttributes(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
                    new String[]{authzIdentityAttr});
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
            authzIdentity = null; // set later, from the specified attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
        // determine mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        if (authcIdentity != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
            if (userFilter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
                authFirst = true; // authentication-first mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
                authOnly = true; // authentication-only mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        if ("false".equalsIgnoreCase((String)options.get("useSSL"))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            useSSL = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
            ldapEnvironment.remove(Context.SECURITY_PROTOCOL);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
            ldapEnvironment.put(Context.SECURITY_PROTOCOL, "ssl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        tryFirstPass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
                "true".equalsIgnoreCase((String)options.get("tryFirstPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        useFirstPass =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                "true".equalsIgnoreCase((String)options.get("useFirstPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        storePass = "true".equalsIgnoreCase((String)options.get("storePass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
        clearPass = "true".equalsIgnoreCase((String)options.get("clearPass"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        debug = "true".equalsIgnoreCase((String)options.get("debug"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
            if (authFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
                    "authentication-first mode; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
                    (useSSL ? "SSL enabled" : "SSL disabled"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
            } else if (authOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
                    "authentication-only mode; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
                    (useSSL ? "SSL enabled" : "SSL disabled"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
                    "search-first mode; " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
                    (useSSL ? "SSL enabled" : "SSL disabled"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * Begin user authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * <p> Acquire the user's credentials and verify them against the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * specified LDAP directory.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     *
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   486
     * @return true always, since this {@code LoginModule}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
     *          should not be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
     * @exception FailedLoginException if the authentication fails.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   489
     * @exception LoginException if this {@code LoginModule}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
     *          is unable to perform the authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    public boolean login() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
        if (userProvider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
            throw new LoginException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
                ("Unable to locate the LDAP directory service");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
            System.out.println("\t\t[LdapLoginModule] user provider: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                userProvider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
        // attempt the authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        if (tryFirstPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
                // attempt the authentication by getting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                // username and password from shared state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
                attemptAuthentication(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
                // authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
                succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
                                "tryFirstPass succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
            } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
                // authentication failed -- try again below by prompting
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
                                "tryFirstPass failed: " + le.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
        } else if (useFirstPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
                // attempt the authentication by getting the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
                // username and password from shared state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
                attemptAuthentication(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
                // authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
                succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
                                "useFirstPass succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
            } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
                // authentication failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
                                "useFirstPass failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
                throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
        // attempt the authentication by prompting for the username and pwd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
            attemptAuthentication(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
            // authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
           succeeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
                                "authentication succeeded");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
        } catch (LoginException le) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
            cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
                                "authentication failed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
            throw le;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * Complete user authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * <p> This method is called if the LoginContext's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     * overall authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     * succeeded).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * succeeded (checked by retrieving the private state saved by the
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   587
     * {@code login} method), then this method associates an
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   588
     * {@code LdapPrincipal} and one or more {@code UserPrincipal}s
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   589
     * with the {@code Subject} located in the
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   590
     * {@code LoginModule}.  If this LoginModule's own
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     * authentication attempted failed, then this method removes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * @exception LoginException if the commit fails
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     * @return true if this LoginModule's own login and commit
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     *          attempts succeeded, or false otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
    public boolean commit() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
            if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
                cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
                throw new LoginException ("Subject is read-only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
            // add Principals to the Subject
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
            Set<Principal> principals = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
            if (! principals.contains(ldapPrincipal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
                principals.add(ldapPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
                                   "added LdapPrincipal \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
                                   ldapPrincipal +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
                                   "\" to Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            if (! principals.contains(userPrincipal)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                principals.add(userPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                   "added UserPrincipal \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
                                   userPrincipal +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
                                   "\" to Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
            if (authzPrincipal != null &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
                (! principals.contains(authzPrincipal))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
                principals.add(authzPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
                                   "added UserPrincipal \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
                                   authzPrincipal +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
                                   "\" to Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
        // in any case, clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
        cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
        commitSucceeded = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
     * Abort user authentication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
     * <p> This method is called if the overall authentication failed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
     * (the relevant REQUIRED, REQUISITE, SUFFICIENT and OPTIONAL LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
     * did not succeed).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
     * <p> If this LoginModule's own authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
     * succeeded (checked by retrieving the private state saved by the
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   656
     * {@code login} and {@code commit} methods),
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * then this method cleans up any state that was originally saved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * @exception LoginException if the abort fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * @return false if this LoginModule's own login and/or commit attempts
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
     *          failed, and true otherwise.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    public boolean abort() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
        if (debug)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
            System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
                "aborted authentication");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
        if (succeeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
        } else if (succeeded == true && commitSucceeded == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
            // Clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
            succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
            cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
            ldapPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
            userPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
            authzPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
            // overall authentication succeeded and commit succeeded,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
            // but someone else's commit failed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
            logout();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
90ce3da70b43 Initial load
duke
parents:
diff changeset
   687
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
     * Logout a user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
     * <p> This method removes the Principals
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   691
     * that were added by the {@code commit} method.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
     * @exception LoginException if the logout fails.
30044
bab15bbe2ca3 8078528: clean out tidy warnings from security.auth
avstepan
parents: 25859
diff changeset
   694
     * @return true in all cases since this {@code LoginModule}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
     *          should not be ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    public boolean logout() throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        if (subject.isReadOnly()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
            throw new LoginException ("Subject is read-only");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
        Set<Principal> principals = subject.getPrincipals();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
        principals.remove(ldapPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        principals.remove(userPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
        if (authzIdentity != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
            principals.remove(authzPrincipal);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
        // clean out state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        cleanState();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        succeeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        commitSucceeded = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
        ldapPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        userPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        authzPrincipal = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
        if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
            System.out.println("\t\t[LdapLoginModule] logged out Subject");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
     * Attempt authentication
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
     * @param getPasswdFromSharedState boolean that tells this method whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     *          to retrieve the password from the sharedState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * @exception LoginException if the authentication attempt fails.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
    private void attemptAuthentication(boolean getPasswdFromSharedState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
        throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        // first get the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
        getUsernamePassword(getPasswdFromSharedState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
        if (password == null || password.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
            throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
                new FailedLoginException("No password was supplied");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        String dn = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
        if (authFirst || authOnly) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
            String id = replaceUsernameToken(identityMatcher, authcIdentity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
            // Prepare to bind using user's username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
            ldapEnvironment.put(Context.SECURITY_CREDENTIALS, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   750
            ldapEnvironment.put(Context.SECURITY_PRINCIPAL, id);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
                    "attempting to authenticate user: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
                // Connect to the LDAP server (using simple bind)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
                ctx = new InitialLdapContext(ldapEnvironment, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
            } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
                throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
                    new FailedLoginException("Cannot bind to LDAP server")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                        .initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            // Authentication has succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
            // Locate the user's distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
            if (userFilter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
                dn = findUserDN(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
                dn = id;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                // Connect to the LDAP server (using anonymous bind)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
                ctx = new InitialLdapContext(ldapEnvironment, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
            } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
                throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
                    new FailedLoginException("Cannot connect to LDAP server")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
                        .initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
            // Locate the user's distinguished name
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
            dn = findUserDN(ctx);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
                // Prepare to bind using user's distinguished name and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
                ctx.addToEnvironment(Context.SECURITY_AUTHENTICATION, "simple");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
                ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, dn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
                ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
                    System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
                        "attempting to authenticate user: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
                // Connect to the LDAP server (using simple bind)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
                ctx.reconnect(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
                // Authentication has succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
            } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
                throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
                    new FailedLoginException("Cannot bind to LDAP server")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
                        .initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
        // Save input as shared state only if authentication succeeded
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
        if (storePass &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
            !sharedState.containsKey(USERNAME_KEY) &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
            !sharedState.containsKey(PASSWORD_KEY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
            sharedState.put(USERNAME_KEY, username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
            sharedState.put(PASSWORD_KEY, password);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
        // Create the user principals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
        userPrincipal = new UserPrincipal(username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
        if (authzIdentity != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
            authzPrincipal = new UserPrincipal(authzIdentity);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
            ldapPrincipal = new LdapPrincipal(dn);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        } catch (InvalidNameException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
                                   "cannot create LdapPrincipal: bad DN");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
            throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
                new FailedLoginException("Cannot create LdapPrincipal")
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
                    .initCause(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     * Search for the user's entry.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
     * Determine the distinguished name of the user's entry and optionally
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
     * an authorization identity for the user.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
     * @param ctx an LDAP context to use for the search
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
     * @return the user's distinguished name or an empty string if none
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
     *         was found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * @exception LoginException if the user's entry cannot be found.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
    private String findUserDN(LdapContext ctx) throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
        String userDN = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
        // Locate the user's LDAP entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
        if (userFilter != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
                    "searching for entry belonging to user: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
            if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
                System.out.println("\t\t[LdapLoginModule] " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
                    "cannot search for entry belonging to user: " + username);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
                new FailedLoginException("Cannot find user's LDAP entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            NamingEnumeration<SearchResult> results = ctx.search("",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
                replaceUsernameToken(filterMatcher, userFilter), constraints);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
            // Extract the distinguished name of the user's entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
            // (Use the first entry if more than one is returned)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
            if (results.hasMore()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
                SearchResult entry = results.next();
43198
7fc878d49203 8161743: Provide proper login context
weijun
parents: 42338
diff changeset
   880
                userDN = entry.getNameInNamespace();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
                    System.out.println("\t\t[LdapLoginModule] found entry: " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
                        userDN);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
                // Extract a value from user's authorization identity attribute
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
                if (authzIdentityAttr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
                    Attribute attr =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
                        entry.getAttributes().get(authzIdentityAttr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
                    if (attr != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
                        Object val = attr.get();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
                        if (val instanceof String) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
                            authzIdentity = (String) val;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
                results.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
                // Bad username
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
                if (debug) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
                    System.out.println("\t\t[LdapLoginModule] user's entry " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
                        "not found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        if (userDN.equals("")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
            throw (LoginException)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
                new FailedLoginException("Cannot find user's LDAP entry");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            return userDN;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * Replace the username token
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * @param string the target string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * @return the modified string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
    private String replaceUsernameToken(Matcher matcher, String string) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
        return matcher != null ? matcher.replaceAll(username) : string;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
     * Get the username and password.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
     * This method does not return any value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
     * Instead, it sets global name and password variables.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
     * <p> Also note that this method will set the username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
     * values in the shared state in case subsequent LoginModules
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
     * want to use them via use/tryFirstPass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
     * @param getPasswdFromSharedState boolean that tells this method whether
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
     *          to retrieve the password from the sharedState.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
     * @exception LoginException if the username/password cannot be acquired.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
    private void getUsernamePassword(boolean getPasswdFromSharedState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
        throws LoginException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        if (getPasswdFromSharedState) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            // use the password saved by the first module in the stack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            username = (String)sharedState.get(USERNAME_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            password = (char[])sharedState.get(PASSWORD_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        // prompt for a username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        if (callbackHandler == null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            throw new LoginException("No CallbackHandler available " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                "to acquire authentication information from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
        Callback[] callbacks = new Callback[2];
7179
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   960
        callbacks[0] = new NameCallback(rb.getString("username."));
4afb81e50183 6987827: security/util/Resources.java needs improvement
weijun
parents: 5506
diff changeset
   961
        callbacks[1] = new PasswordCallback(rb.getString("password."), false);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            callbackHandler.handle(callbacks);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
            username = ((NameCallback)callbacks[0]).getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
            char[] tmpPassword = ((PasswordCallback)callbacks[1]).getPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
            password = new char[tmpPassword.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            System.arraycopy(tmpPassword, 0,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                                password, 0, tmpPassword.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            ((PasswordCallback)callbacks[1]).clearPassword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
        } catch (java.io.IOException ioe) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
            throw new LoginException(ioe.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
        } catch (UnsupportedCallbackException uce) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
            throw new LoginException("Error: " + uce.getCallback().toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                        " not available to acquire authentication information" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                        " from the user");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
     * Clean out state because of a failed authentication attempt
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
    private void cleanState() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        username = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
        if (password != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            Arrays.fill(password, ' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
            password = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            if (ctx != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                ctx.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
        } catch (NamingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            // ignore
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
        ctx = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
        if (clearPass) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
            sharedState.remove(USERNAME_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
            sharedState.remove(PASSWORD_KEY);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
}