1 package org
.argeo
.security
.ldap
.jcr
;
3 import java
.security
.NoSuchAlgorithmException
;
4 import java
.security
.SecureRandom
;
5 import java
.util
.Arrays
;
6 import java
.util
.HashMap
;
8 import java
.util
.Random
;
9 import java
.util
.SortedSet
;
10 import java
.util
.concurrent
.Executor
;
12 import javax
.jcr
.Node
;
13 import javax
.jcr
.Property
;
14 import javax
.jcr
.RepositoryException
;
15 import javax
.jcr
.Session
;
17 import org
.apache
.commons
.logging
.Log
;
18 import org
.apache
.commons
.logging
.LogFactory
;
19 import org
.argeo
.ArgeoException
;
20 import org
.argeo
.jcr
.ArgeoNames
;
21 import org
.argeo
.jcr
.JcrUtils
;
22 import org
.argeo
.security
.jcr
.JcrUserDetails
;
23 import org
.springframework
.ldap
.core
.DirContextAdapter
;
24 import org
.springframework
.ldap
.core
.DirContextOperations
;
25 import org
.springframework
.security
.BadCredentialsException
;
26 import org
.springframework
.security
.GrantedAuthority
;
27 import org
.springframework
.security
.context
.SecurityContextHolder
;
28 import org
.springframework
.security
.providers
.encoding
.PasswordEncoder
;
29 import org
.springframework
.security
.userdetails
.UserDetails
;
30 import org
.springframework
.security
.userdetails
.ldap
.UserDetailsContextMapper
;
33 * Maps LDAP attributes and JCR properties. This class is meant to be robust,
34 * checks of which values should be mandatory should be performed at a higher
37 public class JcrUserDetailsContextMapper
implements UserDetailsContextMapper
,
39 private final static Log log
= LogFactory
40 .getLog(JcrUserDetailsContextMapper
.class);
42 private String usernameAttribute
;
43 private String passwordAttribute
;
44 private String homeBasePath
;
45 private String
[] userClasses
;
47 private Map
<String
, String
> propertyToAttributes
= new HashMap
<String
, String
>();
48 private Executor systemExecutor
;
49 private Session session
;
51 private PasswordEncoder passwordEncoder
;
52 private final Random random
;
54 public JcrUserDetailsContextMapper() {
55 random
= createRandom();
58 private static Random
createRandom() {
60 return SecureRandom
.getInstance("SHA1PRNG");
61 } catch (NoSuchAlgorithmException e
) {
62 return new Random(System
.currentTimeMillis());
66 public UserDetails
mapUserFromContext(final DirContextOperations ctx
,
67 final String username
, GrantedAuthority
[] authorities
) {
69 throw new ArgeoException("No LDAP information found for user "
72 final StringBuffer userHomePathT
= new StringBuffer("");
73 Runnable action
= new Runnable() {
75 String userHomepath
= mapLdapToJcr(username
, ctx
);
76 userHomePathT
.append(userHomepath
);
80 if (SecurityContextHolder
.getContext().getAuthentication() == null) {
83 systemExecutor
.execute(action
);
85 JcrUtils
.logoutQuietly(session
);
93 SortedSet
<?
> passwordAttributes
= ctx
94 .getAttributeSortedStringSet(passwordAttribute
);
96 if (passwordAttributes
== null || passwordAttributes
.size() == 0) {
97 throw new ArgeoException("No password found for user " + username
);
99 byte[] arr
= (byte[]) passwordAttributes
.first();
100 password
= new String(arr
);
102 Arrays
.fill(arr
, (byte) 0);
104 JcrUserDetails userDetails
= new JcrUserDetails(
105 userHomePathT
.toString(), username
, password
, true, true, true,
110 /** @return path to the user home node */
111 protected synchronized String
mapLdapToJcr(String username
, DirContextOperations ctx
) {
112 String usernameLdap
= ctx
.getStringAttribute(usernameAttribute
);
113 // log.debug("username=" + username + ", usernameLdap=" + usernameLdap);
114 if (!username
.equals(usernameLdap
)) {
115 String msg
= "Provided username '" + username
116 + "' is different from username stored in LDAP '"
118 // we log it because the exception may not be displayed
120 throw new BadCredentialsException(msg
);
125 Node userHome
= JcrUtils
.getUserHome(session
, username
);
126 if (userHome
== null)
127 userHome
= JcrUtils
.createUserHome(session
, homeBasePath
,
129 String userHomePath
= userHome
.getPath();
130 Node userProfile
; // = userHome.getNode(ARGEO_PROFILE);
131 if (userHome
.hasNode(ARGEO_PROFILE
)) {
132 userProfile
= userHome
.getNode(ARGEO_PROFILE
);
134 throw new ArgeoException("We should never reach this point");
135 // userProfile = userHome.addNode(ARGEO_PROFILE);
136 // userProfile.addMixin(NodeType.MIX_TITLE);
137 // userProfile.addMixin(NodeType.MIX_CREATED);
138 // userProfile.addMixin(NodeType.MIX_LAST_MODIFIED);
141 for (String jcrProperty
: propertyToAttributes
.keySet())
142 ldapToJcr(userProfile
, jcrProperty
, ctx
);
144 // assign default values
145 if (!userProfile
.hasProperty(Property
.JCR_DESCRIPTION
))
146 userProfile
.setProperty(Property
.JCR_DESCRIPTION
, "");
147 if (!userProfile
.hasProperty(Property
.JCR_TITLE
))
148 userProfile
.setProperty(Property
.JCR_TITLE
, userProfile
149 .getProperty(ARGEO_FIRST_NAME
).getString()
151 + userProfile
.getProperty(ARGEO_LAST_NAME
).getString());
154 if (log
.isTraceEnabled())
155 log
.trace("Mapped " + ctx
.getDn() + " to " + userProfile
);
157 } catch (Exception e
) {
158 JcrUtils
.discardQuietly(session
);
159 throw new ArgeoException("Cannot synchronize JCR and LDAP", e
);
163 public void mapUserToContext(UserDetails user
, final DirContextAdapter ctx
) {
164 if (!(user
instanceof JcrUserDetails
))
165 throw new ArgeoException("Unsupported user details: "
168 ctx
.setAttributeValues("objectClass", userClasses
);
169 ctx
.setAttributeValue(usernameAttribute
, user
.getUsername());
170 ctx
.setAttributeValue(passwordAttribute
,
171 encodePassword(user
.getPassword()));
173 final JcrUserDetails jcrUserDetails
= (JcrUserDetails
) user
;
175 Node userProfile
= session
.getNode(jcrUserDetails
.getHomePath()
176 + '/' + ARGEO_PROFILE
);
177 for (String jcrProperty
: propertyToAttributes
.keySet())
178 jcrToLdap(userProfile
, jcrProperty
, ctx
);
180 if (log
.isTraceEnabled())
181 log
.trace("Mapped " + userProfile
+ " to " + ctx
.getDn());
182 } catch (RepositoryException e
) {
183 throw new ArgeoException("Cannot synchronize JCR and LDAP", e
);
187 protected String
encodePassword(String password
) {
188 if (!password
.startsWith("{")) {
189 byte[] salt
= new byte[16];
190 random
.nextBytes(salt
);
191 return passwordEncoder
.encodePassword(password
, salt
);
197 protected void ldapToJcr(Node userProfile
, String jcrProperty
,
198 DirContextOperations ctx
) {
200 String ldapAttribute
;
201 if (propertyToAttributes
.containsKey(jcrProperty
))
202 ldapAttribute
= propertyToAttributes
.get(jcrProperty
);
204 throw new ArgeoException(
205 "No LDAP attribute mapped for JCR proprty "
208 String value
= ctx
.getStringAttribute(ldapAttribute
);
211 userProfile
.setProperty(jcrProperty
, value
);
212 } catch (Exception e
) {
213 throw new ArgeoException("Cannot map JCR property " + jcrProperty
218 protected void jcrToLdap(Node userProfile
, String jcrProperty
,
219 DirContextOperations ctx
) {
221 String ldapAttribute
;
222 if (propertyToAttributes
.containsKey(jcrProperty
))
223 ldapAttribute
= propertyToAttributes
.get(jcrProperty
);
225 throw new ArgeoException(
226 "No LDAP attribute mapped for JCR proprty "
229 // fix issue with empty 'sn' in LDAP
230 if (ldapAttribute
.equals("sn")
231 && (!userProfile
.hasProperty(jcrProperty
) || userProfile
232 .getProperty(jcrProperty
).getString().trim()
234 userProfile
.setProperty(jcrProperty
, "empty");
236 if (ldapAttribute
.equals("description")) {
237 String value
= userProfile
.getProperty(jcrProperty
).getString();
238 if (value
.trim().equals(""))
242 if (!userProfile
.hasProperty(jcrProperty
))
244 String value
= userProfile
.getProperty(jcrProperty
).getString();
246 ctx
.setAttributeValue(ldapAttribute
, value
);
247 } catch (Exception e
) {
248 throw new ArgeoException("Cannot map JCR property " + jcrProperty
253 public void setPropertyToAttributes(Map
<String
, String
> propertyToAttributes
) {
254 this.propertyToAttributes
= propertyToAttributes
;
257 public void setSystemExecutor(Executor systemExecutor
) {
258 this.systemExecutor
= systemExecutor
;
261 public void setHomeBasePath(String homeBasePath
) {
262 this.homeBasePath
= homeBasePath
;
265 public void setUsernameAttribute(String usernameAttribute
) {
266 this.usernameAttribute
= usernameAttribute
;
269 public void setPasswordAttribute(String passwordAttribute
) {
270 this.passwordAttribute
= passwordAttribute
;
273 public void setUserClasses(String
[] userClasses
) {
274 this.userClasses
= userClasses
;
277 public void setPasswordEncoder(PasswordEncoder passwordEncoder
) {
278 this.passwordEncoder
= passwordEncoder
;
281 public void setSession(Session session
) {
282 this.session
= session
;