Skip to content

Commit

Permalink
Fix: Reading the username based on the login method.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ludy87 committed Nov 10, 2024
1 parent 645c786 commit 24b0d3e
Showing 1 changed file with 13 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Service;

import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.config.interfaces.DatabaseBackupInterface;
import stirling.software.SPDF.config.security.saml2.CustomSaml2AuthenticatedPrincipal;
import stirling.software.SPDF.config.security.session.SessionPersistentRegistry;
import stirling.software.SPDF.controller.api.pipeline.UserServiceInterface;
import stirling.software.SPDF.model.ApplicationProperties;
import stirling.software.SPDF.model.AuthenticationType;
import stirling.software.SPDF.model.Authority;
import stirling.software.SPDF.model.Role;
Expand All @@ -31,6 +33,7 @@
import stirling.software.SPDF.repository.UserRepository;

@Service
@Slf4j
public class UserService implements UserServiceInterface {

@Autowired private UserRepository userRepository;
Expand All @@ -45,6 +48,8 @@ public class UserService implements UserServiceInterface {

@Autowired DatabaseBackupInterface databaseBackupHelper;

@Autowired ApplicationProperties applicationProperties;

// Handle OAUTH2 login and user auto creation.
public boolean processOAuth2PostLogin(String username, boolean autoCreateUser)
throws IllegalArgumentException, IOException {
Expand Down Expand Up @@ -354,6 +359,14 @@ public String getCurrentUsername() {

if (principal instanceof UserDetails) {
return ((UserDetails) principal).getUsername();
} else if (principal instanceof OAuth2User) {
return ((OAuth2User) principal)
.getAttribute(
applicationProperties.getSecurity().getOauth2().getUseAsUsername());
} else if (principal instanceof CustomSaml2AuthenticatedPrincipal) {
return ((CustomSaml2AuthenticatedPrincipal) principal).getName();
} else if (principal instanceof String) {
return (String) principal;
} else {
return principal.toString();
}
Expand Down

0 comments on commit 24b0d3e

Please sign in to comment.