1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.acegisecurity.providers.ldap.authenticator;
17
18 import org.acegisecurity.BadCredentialsException;
19
20 import org.acegisecurity.ldap.InitialDirContextFactory;
21 import org.acegisecurity.ldap.LdapTemplate;
22 import org.acegisecurity.ldap.LdapUtils;
23
24 import org.acegisecurity.providers.encoding.PasswordEncoder;
25
26 import org.acegisecurity.userdetails.UsernameNotFoundException;
27 import org.acegisecurity.userdetails.ldap.LdapUserDetails;
28 import org.acegisecurity.userdetails.ldap.LdapUserDetailsImpl;
29
30 import org.apache.commons.logging.Log;
31 import org.apache.commons.logging.LogFactory;
32
33 import org.springframework.util.Assert;
34
35 import java.util.Iterator;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public final class PasswordComparisonAuthenticator extends AbstractLdapAuthenticator {
56
57
58 private static final Log logger = LogFactory.getLog(PasswordComparisonAuthenticator.class);
59
60
61
62 private PasswordEncoder passwordEncoder = new LdapShaPasswordEncoder();
63 private String passwordAttributeName = "userPassword";
64
65
66
67 public PasswordComparisonAuthenticator(InitialDirContextFactory initialDirContextFactory) {
68 super(initialDirContextFactory);
69 }
70
71
72
73 public LdapUserDetails authenticate(final String username, final String password) {
74
75 LdapUserDetails user = null;
76
77 Iterator dns = getUserDns(username).iterator();
78
79 LdapTemplate ldapTemplate = new LdapTemplate(getInitialDirContextFactory());
80
81 while (dns.hasNext() && (user == null)) {
82 final String userDn = (String) dns.next();
83
84 if (ldapTemplate.nameExists(userDn)) {
85 LdapUserDetailsImpl.Essence userEssence = (LdapUserDetailsImpl.Essence)
86 ldapTemplate.retrieveEntry(userDn, getUserDetailsMapper(), getUserAttributes());
87 userEssence.setUsername(username);
88 user = userEssence.createUserDetails();
89 }
90 }
91
92 if ((user == null) && (getUserSearch() != null)) {
93 user = getUserSearch().searchForUser(username);
94 }
95
96 if (user == null) {
97 throw new UsernameNotFoundException(username);
98 }
99
100 String retrievedPassword = user.getPassword();
101
102 if (retrievedPassword != null) {
103 if (!verifyPassword(password, retrievedPassword)) {
104 throw new BadCredentialsException(messages.getMessage(
105 "PasswordComparisonAuthenticator.badCredentials", "Bad credentials"));
106 }
107
108 return user;
109 }
110
111 if (logger.isDebugEnabled()) {
112 logger.debug("Password attribute wasn't retrieved for user '" + username + "' using mapper "
113 + getUserDetailsMapper() + ". Performing LDAP compare of password attribute '" + passwordAttributeName
114 + "'");
115 }
116
117 String encodedPassword = passwordEncoder.encodePassword(password, null);
118 byte[] passwordBytes = LdapUtils.getUtf8Bytes(encodedPassword);
119
120 if (!ldapTemplate.compare(user.getDn(), passwordAttributeName, passwordBytes)) {
121 throw new BadCredentialsException(messages.getMessage("PasswordComparisonAuthenticator.badCredentials",
122 "Bad credentials"));
123 }
124
125 return user;
126 }
127
128 public void setPasswordAttributeName(String passwordAttribute) {
129 Assert.hasLength(passwordAttribute, "passwordAttributeName must not be empty or null");
130 this.passwordAttributeName = passwordAttribute;
131 }
132
133 public void setPasswordEncoder(PasswordEncoder passwordEncoder) {
134 Assert.notNull(passwordEncoder, "passwordEncoder must not be null.");
135 this.passwordEncoder = passwordEncoder;
136 }
137
138
139
140
141
142
143
144
145
146 private boolean verifyPassword(String password, String ldapPassword) {
147 if (ldapPassword.equals(password)) {
148 return true;
149 }
150
151 if (passwordEncoder.isPasswordValid(ldapPassword, password, null)) {
152 return true;
153 }
154
155 return false;
156 }
157 }