2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.security
.ldap
;
18 import java
.security
.NoSuchAlgorithmException
;
19 import java
.security
.SecureRandom
;
20 import java
.util
.ArrayList
;
21 import java
.util
.Collections
;
22 import java
.util
.Iterator
;
23 import java
.util
.List
;
24 import java
.util
.Random
;
26 import java
.util
.TreeSet
;
28 import org
.argeo
.ArgeoException
;
29 import org
.argeo
.security
.UserAdminService
;
30 import org
.springframework
.ldap
.core
.ContextSource
;
31 import org
.springframework
.security
.Authentication
;
32 import org
.springframework
.security
.GrantedAuthority
;
33 import org
.springframework
.security
.context
.SecurityContextHolder
;
34 import org
.springframework
.security
.providers
.encoding
.PasswordEncoder
;
35 import org
.springframework
.security
.userdetails
.UserDetails
;
36 import org
.springframework
.security
.userdetails
.ldap
.LdapUserDetailsManager
;
38 /** Extends {@link LdapUserDetailsManager} by adding password encoding support. */
39 public class ArgeoLdapUserDetailsManager
extends LdapUserDetailsManager
40 implements UserAdminService
{
41 private String superUsername
= "root";
42 private ArgeoUserAdminDaoLdap userAdminDao
;
43 private PasswordEncoder passwordEncoder
;
44 private final Random random
;
46 public ArgeoLdapUserDetailsManager(ContextSource contextSource
) {
48 this.random
= createRandom();
51 private static Random
createRandom() {
53 return SecureRandom
.getInstance("SHA1PRNG");
54 } catch (NoSuchAlgorithmException e
) {
55 return new Random(System
.currentTimeMillis());
60 public void changePassword(String oldPassword
, String newPassword
) {
61 Authentication authentication
= SecurityContextHolder
.getContext()
63 if (authentication
== null)
64 throw new ArgeoException(
65 "Cannot change password without authentication");
66 String username
= authentication
.getName();
67 UserDetails userDetails
= loadUserByUsername(username
);
68 String currentPassword
= userDetails
.getPassword();
69 if (currentPassword
== null)
70 throw new ArgeoException("Cannot access current password");
72 .isPasswordValid(currentPassword
, oldPassword
, null))
73 throw new ArgeoException("Old password invalid");
74 // Spring Security LDAP 2.0 is buggy when used with OpenLDAP and called
75 // with oldPassword argument
76 super.changePassword(null, encodePassword(newPassword
));
79 public void newRole(String role
) {
80 userAdminDao
.createRole(role
, superUsername
);
83 public void synchronize() {
84 for (String username
: userAdminDao
.listUsers())
85 loadUserByUsername(username
);
86 // TODO: find a way to remove from JCR
89 public void deleteRole(String role
) {
90 userAdminDao
.deleteRole(role
);
93 public Set
<String
> listUsers() {
94 return userAdminDao
.listUsers();
97 public Set
<String
> listUsersInRole(String role
) {
98 Set
<String
> lst
= new TreeSet
<String
>(
99 userAdminDao
.listUsersInRole(role
));
100 Iterator
<String
> it
= lst
.iterator();
101 while (it
.hasNext()) {
102 if (it
.next().equals(superUsername
)) {
110 public List
<String
> listUserRoles(String username
) {
111 UserDetails userDetails
= loadUserByUsername(username
);
112 List
<String
> roles
= new ArrayList
<String
>();
113 for (GrantedAuthority ga
: userDetails
.getAuthorities()) {
114 roles
.add(ga
.getAuthority());
116 return Collections
.unmodifiableList(roles
);
119 public Set
<String
> listEditableRoles() {
120 return userAdminDao
.listEditableRoles();
123 protected String
encodePassword(String password
) {
124 if (!password
.startsWith("{")) {
125 byte[] salt
= new byte[16];
126 random
.nextBytes(salt
);
127 return passwordEncoder
.encodePassword(password
, salt
);
133 public void setPasswordEncoder(PasswordEncoder passwordEncoder
) {
134 this.passwordEncoder
= passwordEncoder
;
137 public void setSuperUsername(String superUsername
) {
138 this.superUsername
= superUsername
;
141 public void setUserAdminDao(ArgeoUserAdminDaoLdap userAdminDao
) {
142 this.userAdminDao
= userAdminDao
;