mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-02-07 00:17:07 +01:00
wip
This commit is contained in:
parent
0631e3071c
commit
db8bc633b2
@ -410,33 +410,41 @@ 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()) {
|
|
||||||
// Create new user with API role
|
existingUser.ifPresentOrElse(
|
||||||
User user = new User();
|
user -> {
|
||||||
user.setUsername(username);
|
// Update API key if it has changed
|
||||||
user.setPassword(UUID.randomUUID().toString());
|
User updatedUser = existingUser.get();
|
||||||
user.setEnabled(true);
|
|
||||||
user.setFirstLogin(false);
|
if (!customApiKey.equals(updatedUser.getApiKey())) {
|
||||||
user.setAuthenticationType(AuthenticationType.WEB);
|
updatedUser.setApiKey(customApiKey);
|
||||||
user.setApiKey(customApiKey);
|
userRepository.save(updatedUser);
|
||||||
user.addAuthority(new Authority(Role.INTERNAL_API_USER.getRoleId(), user));
|
}
|
||||||
userRepository.save(user);
|
},
|
||||||
|
() -> {
|
||||||
|
// Create new user with API role
|
||||||
|
User user = new User();
|
||||||
|
user.setUsername(username);
|
||||||
|
user.setPassword(UUID.randomUUID().toString());
|
||||||
|
user.setEnabled(true);
|
||||||
|
user.setFirstLogin(false);
|
||||||
|
user.setAuthenticationType(AuthenticationType.WEB);
|
||||||
|
user.setApiKey(customApiKey);
|
||||||
|
user.addAuthority(new Authority(Role.INTERNAL_API_USER.getRoleId(), 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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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;
|
||||||
|
Loading…
Reference in New Issue
Block a user