wip - refactoring & cleanup

This commit is contained in:
Dario Ghunney Ware 2025-01-24 18:14:15 +00:00
parent c439ccd02a
commit ace34004f9
9 changed files with 41 additions and 49 deletions

View File

@ -197,14 +197,14 @@ public class CustomLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler {
+ clientId + clientId
+ "&post_logout_redirect_uri=" + "&post_logout_redirect_uri="
+ response.encodeRedirectURL(redirect_url); + response.encodeRedirectURL(redirect_url);
log.info("Redirecting to Keycloak logout URL: " + logoutUrl); log.info("Redirecting to Keycloak logout URL: {}", logoutUrl);
response.sendRedirect(logoutUrl); response.sendRedirect(logoutUrl);
} }
case "github" -> { case "github" -> {
// Add GitHub specific logout URL if needed // Add GitHub specific logout URL if needed
// todo: why does the redirect go to github? shouldn't it come to Stirling PDF? // todo: why does the redirect go to github? shouldn't it come to Stirling PDF?
String githubLogoutUrl = "https://github.com/logout"; String githubLogoutUrl = "https://github.com/logout";
log.info("Redirecting to GitHub logout URL: " + redirect_url); log.info("Redirecting to GitHub logout URL: {}", redirect_url);
response.sendRedirect(redirect_url); response.sendRedirect(redirect_url);
} }
case "google" -> { case "google" -> {

View File

@ -64,19 +64,13 @@ public class OAuth2Configuration {
} }
private Optional<ClientRegistration> googleClientRegistration() { private Optional<ClientRegistration> googleClientRegistration() {
OAUTH2 oauth = applicationProperties.getSecurity().getOauth2(); if (isOauthOrClientEmpty()) {
if (oauth == null || !oauth.getEnabled()) {
return Optional.empty(); return Optional.empty();
} }
Client client = oauth.getClient(); GoogleProvider google =
applicationProperties.getSecurity().getOauth2().getClient().getGoogle();
if (client == null) {
return Optional.empty();
}
GoogleProvider google = client.getGoogle();
return google != null && google.isSettingsValid() return google != null && google.isSettingsValid()
? Optional.of( ? Optional.of(
ClientRegistration.withRegistrationId(google.getName()) ClientRegistration.withRegistrationId(google.getName())
@ -95,15 +89,13 @@ public class OAuth2Configuration {
} }
private Optional<ClientRegistration> keycloakClientRegistration() { private Optional<ClientRegistration> keycloakClientRegistration() {
OAUTH2 oauth = applicationProperties.getSecurity().getOauth2(); if (isOauthOrClientEmpty()) {
if (oauth == null || !oauth.getEnabled()) {
return Optional.empty(); return Optional.empty();
} }
Client client = oauth.getClient();
if (client == null) { KeycloakProvider keycloak =
return Optional.empty(); applicationProperties.getSecurity().getOauth2().getClient().getKeycloak();
}
KeycloakProvider keycloak = client.getKeycloak();
return keycloak != null && keycloak.isSettingsValid() return keycloak != null && keycloak.isSettingsValid()
? Optional.of( ? Optional.of(
ClientRegistrations.fromIssuerLocation(keycloak.getIssuer()) ClientRegistrations.fromIssuerLocation(keycloak.getIssuer())
@ -181,7 +173,7 @@ public class OAuth2Configuration {
Client client = oauth.getClient(); Client client = oauth.getClient();
return client != null; return client == null;
} }
/* /*

View File

@ -5,7 +5,6 @@ import java.util.Collection;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
// @Setter
@NoArgsConstructor @NoArgsConstructor
public class GithubProvider extends Provider { public class GithubProvider extends Provider {
@ -22,7 +21,7 @@ public class GithubProvider extends Provider {
public GithubProvider( public GithubProvider(
String clientId, String clientSecret, Collection<String> scopes, String useAsUsername) { String clientId, String clientSecret, Collection<String> scopes, String useAsUsername) {
super(null, NAME, CLIENT_NAME, clientId, clientSecret, scopes, useAsUsername); super(null, NAME, CLIENT_NAME, clientId, clientSecret, scopes, useAsUsername);
this.clientId = clientId; this.clientId = clientId;
this.clientSecret = clientSecret; this.clientSecret = clientSecret;
this.scopes = scopes; this.scopes = scopes;

View File

@ -5,7 +5,6 @@ import java.util.Collection;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
// @Setter
@NoArgsConstructor @NoArgsConstructor
public class GoogleProvider extends Provider { public class GoogleProvider extends Provider {

View File

@ -5,7 +5,6 @@ import java.util.Collection;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
// @Setter
@NoArgsConstructor @NoArgsConstructor
public class KeycloakProvider extends Provider { public class KeycloakProvider extends Provider {

View File

@ -4,10 +4,10 @@ import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import lombok.Getter; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
@Getter @Data
@NoArgsConstructor @NoArgsConstructor
public abstract class Provider { public abstract class Provider {
@ -37,6 +37,7 @@ public abstract class Provider {
} }
// todo: why are we passing name here if it's not used? // todo: why are we passing name here if it's not used?
// todo: use util class/method
public boolean isSettingsValid() { public boolean isSettingsValid() {
return isValid(this.getIssuer(), "issuer") return isValid(this.getIssuer(), "issuer")
&& isValid(this.getClientId(), "clientId") && isValid(this.getClientId(), "clientId")
@ -53,32 +54,8 @@ public abstract class Provider {
return value != null && !value.isEmpty(); return value != null && !value.isEmpty();
} }
public void setIssuer(String issuer) {
this.issuer = issuer;
}
public void setName(String name) {
this.name = name;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
public void setScopes(String scopes) { public void setScopes(String scopes) {
this.scopes = this.scopes =
Arrays.stream(scopes.split(",")).map(String::trim).collect(Collectors.toList()); Arrays.stream(scopes.split(",")).map(String::trim).collect(Collectors.toList());
} }
public void setUseAsUsername(String useAsUsername) {
this.useAsUsername = useAsUsername;
}
} }

View File

@ -0,0 +1,11 @@
package stirling.software.SPDF.utils.validation;
import java.util.Collection;
public class CollectionValidator implements Validator<Collection<String>> {
@Override
public boolean validate(Collection<String> input, String path) {
return input != null && !input.isEmpty();
}
}

View File

@ -0,0 +1,9 @@
package stirling.software.SPDF.utils.validation;
public class StringValidator implements Validator<String> {
@Override
public boolean validate(String input, String path) {
return input != null && !input.isBlank();
}
}

View File

@ -0,0 +1,6 @@
package stirling.software.SPDF.utils.validation;
public interface Validator<T> {
boolean validate(T input, String path);
}