Skip to content

Commit

Permalink
Merge pull request #127 from peter-szrnka/125-gms-64-refactor-and-sim…
Browse files Browse the repository at this point in the history
…plify-ldap-auth-code-part

GMS-64 Refactor done
  • Loading branch information
peter-szrnka authored Oct 17, 2023
2 parents 17a2b4a + 4d17291 commit 596ee45
Show file tree
Hide file tree
Showing 6 changed files with 258 additions and 177 deletions.
3 changes: 2 additions & 1 deletion code/gms-backend.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"java.configuration.updateBuildConfiguration": "automatic",
"jest.disabledWorkspaceFolders": [
"gms-backend"
]
],
"java.debug.settings.onBuildFailureProceed": true
}
}
Original file line number Diff line number Diff line change
@@ -1,53 +1,33 @@
package io.github.gms.auth.ldap;

import static io.github.gms.common.util.Constants.CONFIG_AUTH_TYPE_LDAP;
import static io.github.gms.common.util.Constants.LDAP_CRYPT_PREFIX;

import java.time.Clock;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.query.LdapQueryBuilder;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import io.github.gms.auth.UserAuthService;
import io.github.gms.auth.model.GmsUserDetails;
import io.github.gms.common.enums.EntityStatus;
import io.github.gms.secure.entity.UserEntity;
import io.github.gms.secure.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;

/**
* @author Peter Szrnka
* @since 1.0
*/
@Slf4j
@Service
@Profile(value = { CONFIG_AUTH_TYPE_LDAP })
public class LdapUserAuthServiceImpl implements UserAuthService {

private final Clock clock;
private final UserRepository repository;
private final LdapTemplate ldapTemplate;
private final LdapUserPersistenceService ldapUserPersistenceService;

private final boolean storeLdapCredential;

public LdapUserAuthServiceImpl(
Clock clock,
UserRepository repository,
LdapTemplate ldapTemplate,
@Value("${config.store.ldap.credential:false}") boolean storeLdapCredential) {
this.clock = clock;
this.repository = repository;
public LdapUserAuthServiceImpl(LdapTemplate ldapTemplate, LdapUserPersistenceService ldapUserPersistenceService) {
this.ldapTemplate = ldapTemplate;
this.storeLdapCredential = storeLdapCredential;
this.ldapUserPersistenceService = ldapUserPersistenceService;
}

@Override
Expand All @@ -59,43 +39,6 @@ public UserDetails loadUserByUsername(String username) throws UsernameNotFoundEx
throw new UsernameNotFoundException("User not found!");
}

GmsUserDetails foundUser = result.get(0);

repository.findByUsername(username).ifPresentOrElse(userEntity -> saveExistingUser(foundUser, userEntity),
() -> saveNewUser(foundUser));
return foundUser;
}

private void saveExistingUser(GmsUserDetails foundUser, UserEntity userEntity) {
foundUser.setUserId(userEntity.getId());

if (storeLdapCredential && !userEntity.getCredential().equals(foundUser.getCredential())) {
userEntity.setCredential(getCredential(foundUser));
repository.save(userEntity);
log.info("Credential has been updated for user={}", foundUser.getUsername());
}
}

private void saveNewUser(GmsUserDetails foundUser) {
UserEntity userEntity = new UserEntity();
userEntity.setStatus(EntityStatus.ACTIVE);
userEntity.setName(foundUser.getName());
userEntity.setUsername(foundUser.getUsername());
userEntity.setCredential(getCredential(foundUser));
userEntity.setCreationDate(ZonedDateTime.now(clock));
userEntity.setEmail(foundUser.getEmail());
userEntity.setRoles(foundUser.getAuthorities().stream().map(GrantedAuthority::getAuthority)
.collect(Collectors.joining(",")));
userEntity.setMfaEnabled(foundUser.isMfaEnabled());
userEntity.setMfaSecret(foundUser.getMfaSecret());
userEntity = repository.save(userEntity);

foundUser.setUserId(userEntity.getId());
log.info("User data has been saved into DB for user={}", foundUser.getUsername());
}

private String getCredential(GmsUserDetails foundUser) {
return storeLdapCredential ? foundUser.getCredential().replace(LDAP_CRYPT_PREFIX, "")
: "*PROVIDED_BY_LDAP*";
return ldapUserPersistenceService.saveUserIfRequired(username, result.get(0));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.gms.auth.ldap;

import io.github.gms.auth.model.GmsUserDetails;

/**
* @author Peter Szrnka
* @since 1.0
*/
public interface LdapUserPersistenceService {

GmsUserDetails saveUserIfRequired(String username, GmsUserDetails foundUser);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.github.gms.auth.ldap;

import static io.github.gms.common.util.Constants.CONFIG_AUTH_TYPE_LDAP;
import static io.github.gms.common.util.Constants.LDAP_CRYPT_PREFIX;

import java.time.Clock;
import java.time.ZonedDateTime;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Profile;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;

import io.github.gms.auth.model.GmsUserDetails;
import io.github.gms.common.enums.EntityStatus;
import io.github.gms.secure.entity.UserEntity;
import io.github.gms.secure.repository.UserRepository;
import lombok.extern.slf4j.Slf4j;

/**
* @author Peter Szrnka
* @since 1.0
*/
@Slf4j
@Service
@Profile(value = { CONFIG_AUTH_TYPE_LDAP })
public class LdapUserPersistenceServiceImpl implements LdapUserPersistenceService {

private final Clock clock;
private final UserRepository repository;
private final boolean storeLdapCredential;

public LdapUserPersistenceServiceImpl(Clock clock, UserRepository repository, @Value("${config.store.ldap.credential:false}") boolean storeLdapCredential) {
this.clock = clock;
this.repository = repository;
this.storeLdapCredential = storeLdapCredential;
}

@Override
public GmsUserDetails saveUserIfRequired(String username, GmsUserDetails foundUser) {
repository.findByUsername(username).ifPresentOrElse(userEntity -> saveExistingUser(foundUser, userEntity),
() -> saveNewUser(foundUser));

return foundUser;
}

private void saveExistingUser(GmsUserDetails foundUser, UserEntity userEntity) {
foundUser.setUserId(userEntity.getId());

if (storeLdapCredential && !userEntity.getCredential().equals(foundUser.getCredential())) {
userEntity.setCredential(getCredential(foundUser));
repository.save(userEntity);
log.info("Credential has been updated for user={}", foundUser.getUsername());
}
}

private void saveNewUser(GmsUserDetails foundUser) {
UserEntity userEntity = new UserEntity();
userEntity.setStatus(EntityStatus.ACTIVE);
userEntity.setName(foundUser.getName());
userEntity.setUsername(foundUser.getUsername());
userEntity.setCredential(getCredential(foundUser));
userEntity.setCreationDate(ZonedDateTime.now(clock));
userEntity.setEmail(foundUser.getEmail());
userEntity.setRoles(foundUser.getAuthorities().stream().map(GrantedAuthority::getAuthority)
.collect(Collectors.joining(",")));
userEntity.setMfaEnabled(foundUser.isMfaEnabled());
userEntity.setMfaSecret(foundUser.getMfaSecret());
userEntity = repository.save(userEntity);

foundUser.setUserId(userEntity.getId());
log.info("User data has been saved into DB for user={}", foundUser.getUsername());
}

private String getCredential(GmsUserDetails foundUser) {
return storeLdapCredential ? foundUser.getCredential().replace(LDAP_CRYPT_PREFIX, "")
: "*PROVIDED_BY_LDAP*";
}
}
Loading

0 comments on commit 596ee45

Please sign in to comment.