This commit is contained in:
DarioGii 2025-01-21 11:43:44 +00:00 committed by Dario Ghunney Ware
parent 0631e3071c
commit db8bc633b2
3 changed files with 34 additions and 28 deletions

View File

@ -410,14 +410,25 @@ public class UserService implements UserServiceInterface {
} }
@Transactional @Transactional
public void syncCustomApiUser(String customApiKey) public void syncCustomApiUser(String customApiKey) {
throws SQLException, UnsupportedProviderException { if (customApiKey == null || customApiKey.trim().isBlank()) {
if (customApiKey == null || customApiKey.trim().length() == 0) {
return; return;
} }
String username = "CUSTOM_API_USER"; String username = "CUSTOM_API_USER";
Optional<User> existingUser = findByUsernameIgnoreCase(username); Optional<User> existingUser = findByUsernameIgnoreCase(username);
if (!existingUser.isPresent()) {
existingUser.ifPresentOrElse(
user -> {
// Update API key if it has changed
User updatedUser = existingUser.get();
if (!customApiKey.equals(updatedUser.getApiKey())) {
updatedUser.setApiKey(customApiKey);
userRepository.save(updatedUser);
}
},
() -> {
// Create new user with API role // Create new user with API role
User user = new User(); User user = new User();
user.setUsername(username); user.setUsername(username);
@ -428,15 +439,12 @@ public class UserService implements UserServiceInterface {
user.setApiKey(customApiKey); user.setApiKey(customApiKey);
user.addAuthority(new Authority(Role.INTERNAL_API_USER.getRoleId(), user)); user.addAuthority(new Authority(Role.INTERNAL_API_USER.getRoleId(), user));
userRepository.save(user); userRepository.save(user);
});
try {
databaseService.exportDatabase(); databaseService.exportDatabase();
} else { } catch (SQLException | UnsupportedProviderException e) {
// Update API key if it has changed log.error("Error exporting database after synchronising custom API user", e);
User user = existingUser.get();
if (!customApiKey.equals(user.getApiKey())) {
user.setApiKey(customApiKey);
userRepository.save(user);
databaseService.exportDatabase();
}
} }
} }

View File

@ -47,11 +47,9 @@ public class CustomOAuth2AuthenticationSuccessHandler
Object principal = authentication.getPrincipal(); Object principal = authentication.getPrincipal();
String username = ""; String username = "";
if (principal instanceof OAuth2User) { if (principal instanceof OAuth2User oauthUser) {
OAuth2User oauthUser = (OAuth2User) principal;
username = oauthUser.getName(); username = oauthUser.getName();
} else if (principal instanceof UserDetails) { } else if (principal instanceof UserDetails oauthUser) {
UserDetails oauthUser = (UserDetails) principal;
username = oauthUser.getUsername(); username = oauthUser.getUsername();
} }

View File

@ -33,8 +33,8 @@ import stirling.software.SPDF.model.provider.KeycloakProvider;
@Slf4j @Slf4j
@ConditionalOnProperty( @ConditionalOnProperty(
value = "security.oauth2.enabled", value = "security.oauth2.enabled",
havingValue = "true", havingValue = "true"
matchIfMissing = false) )
public class OAuth2Configuration { public class OAuth2Configuration {
private final ApplicationProperties applicationProperties; private final ApplicationProperties applicationProperties;