|  | 
| 4 | 4 | import name.nkonev.aaa.converter.UserAccountConverter; | 
| 5 | 5 | import name.nkonev.aaa.dto.UserAccountDetailsDTO; | 
| 6 | 6 | import name.nkonev.aaa.entity.jdbc.UserAccount; | 
|  | 7 | +import name.nkonev.aaa.entity.ldap.LdapEntity; | 
| 7 | 8 | import name.nkonev.aaa.exception.UserAlreadyPresentException; | 
| 8 | 9 | import name.nkonev.aaa.repository.jdbc.UserAccountRepository; | 
| 9 | 10 | import name.nkonev.aaa.services.CheckService; | 
|  | 
| 27 | 28 | import java.util.Set; | 
| 28 | 29 | import java.util.concurrent.atomic.AtomicBoolean; | 
| 29 | 30 | 
 | 
| 30 |  | -import static name.nkonev.aaa.converter.UserAccountConverter.normalizeEmail; | 
| 31 |  | -import static name.nkonev.aaa.utils.ConvertUtils.convertToStrings; | 
| 32 |  | - | 
| 33 |  | -import name.nkonev.aaa.utils.NullUtils; | 
| 34 |  | - | 
| 35 |  | -import javax.naming.NamingException; | 
| 36 |  | - | 
| 37 | 31 | // https://spring.io/guides/gs/authenticating-ldap | 
| 38 | 32 | @Component | 
| 39 | 33 | public class LdapAuthenticationProvider implements AuthenticationProvider { | 
| @@ -76,37 +70,38 @@ public Authentication authenticate(Authentication authentication) throws Authent | 
| 76 | 70 |                 var userAccount = transactionTemplate.execute(status -> { | 
| 77 | 71 |                     var lq = LdapQueryBuilder.query().base(aaaProperties.ldap().auth().base()).filter(aaaProperties.ldap().auth().filter(), userName); | 
| 78 | 72 |                     ldapOperations.authenticate(lq, encodedPassword); | 
| 79 |  | -                    var ldapEntry = ldapOperations.searchForContext(lq).getAttributes(); | 
| 80 | 73 | 
 | 
| 81 |  | -                    var ldapUserId = NullUtils.getOrNullWrapException(() -> ldapEntry.get(aaaProperties.ldap().attributeNames().id()).get().toString()); | 
|  | 74 | +                    var ldapAttributes = ldapOperations.searchForContext(lq).getAttributes(); | 
|  | 75 | +                    var ldapEntry = new LdapEntity(aaaProperties.ldap().attributeNames(), ldapAttributes); | 
|  | 76 | + | 
|  | 77 | +                    var ldapUserId = ldapEntry.id(); | 
|  | 78 | +                    if (ldapUserId == null) { | 
|  | 79 | +                        LOGGER.warn("Got null ldap id for username={}", userName); | 
|  | 80 | +                        return null; | 
|  | 81 | +                    } | 
| 82 | 82 | 
 | 
| 83 | 83 |                     UserAccount byLdapId = userAccountRepository | 
| 84 | 84 |                         .findByLdapId(ldapUserId) | 
| 85 | 85 |                         .orElseGet(() -> { | 
| 86 | 86 |                             // create a new | 
|  | 87 | + | 
|  | 88 | +                            // check conflict by username | 
| 87 | 89 |                             userAccountRepository.findByUsername(userName).ifPresent(ua -> { | 
| 88 | 90 |                                 throw new UserAlreadyPresentException("User with login '" + userName + "' is already present"); | 
| 89 | 91 |                             }); | 
| 90 | 92 | 
 | 
| 91 | 93 |                             String email = null; | 
| 92 | 94 |                             if (StringUtils.hasLength(aaaProperties.ldap().attributeNames().email())) { | 
| 93 |  | -                                var ldapEmail = NullUtils.getOrNullWrapException(() -> ldapEntry.get(aaaProperties.ldap().attributeNames().email()).get().toString()); | 
| 94 |  | -                                email = normalizeEmail(ldapEmail); | 
|  | 95 | +                                email = ldapEntry.email(); | 
| 95 | 96 |                             } | 
| 96 | 97 | 
 | 
| 97 |  | -                            final Set<String> rawRoles = new HashSet<>(); | 
|  | 98 | +                            Set<String> rawRoles = new HashSet<>(); | 
| 98 | 99 |                             if (StringUtils.hasLength(aaaProperties.ldap().attributeNames().role())) { | 
| 99 |  | -                                try { | 
| 100 |  | -                                    var groups = ldapEntry.get(aaaProperties.ldap().attributeNames().role()).getAll(); | 
| 101 |  | -                                    if (groups != null) { | 
| 102 |  | -                                        rawRoles.addAll(convertToStrings(groups)); | 
| 103 |  | -                                    } | 
| 104 |  | -                                } catch (NamingException e) { | 
| 105 |  | -                                    LOGGER.error(e.getMessage(), e); | 
| 106 |  | -                                } | 
|  | 100 | +                                rawRoles = ldapEntry.roles(); | 
| 107 | 101 |                             } | 
| 108 | 102 |                             var mappedRoles = RoleMapper.map(aaaProperties.roleMappings().ldap(), rawRoles); | 
| 109 | 103 | 
 | 
|  | 104 | +                            // check conflict by email | 
| 110 | 105 |                             if (StringUtils.hasLength(email)) { | 
| 111 | 106 |                                 if (!userService.checkEmailIsFree(email)){ | 
| 112 | 107 |                                     throw new UserAlreadyPresentException("User with email '" + email + "' is already present"); | 
|  | 
0 commit comments