Files
Stirling-PDF/src/main/java/stirling/software/SPDF/model/provider/Provider.java

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

141 lines
4.7 KiB
Java
Raw Normal View History

2025-01-22 15:14:06 +00:00
package stirling.software.SPDF.model.provider;
import static stirling.software.SPDF.model.UsernameAttribute.EMAIL;
2025-01-24 18:14:15 +00:00
import java.util.ArrayList;
2025-01-22 15:14:06 +00:00
import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;
2025-01-24 18:14:15 +00:00
import lombok.Data;
2025-01-22 15:14:06 +00:00
import lombok.NoArgsConstructor;
import stirling.software.SPDF.model.UsernameAttribute;
import stirling.software.SPDF.model.exception.UnsupportedUsernameAttribute;
2025-01-22 15:14:06 +00:00
2025-01-24 18:14:15 +00:00
@Data
2025-01-22 15:14:06 +00:00
@NoArgsConstructor
2025-01-24 18:14:15 +00:00
public class Provider {
2025-01-22 15:14:06 +00:00
private String issuer;
private String name;
private String clientName;
private String clientId;
private String clientSecret;
private Collection<String> scopes;
private UsernameAttribute useAsUsername;
2025-01-24 18:14:15 +00:00
private String authorizationUri;
private String tokenUri;
private String userInfoUri;
2025-01-22 15:14:06 +00:00
public Provider(
String issuer,
String name,
String clientName,
String clientId,
String clientSecret,
Collection<String> scopes,
UsernameAttribute useAsUsername,
2025-01-24 18:14:15 +00:00
String authorizationUri,
String tokenUri,
String userInfoUri) {
2025-01-22 15:14:06 +00:00
this.issuer = issuer;
this.name = name;
this.clientName = clientName;
this.clientId = clientId;
this.clientSecret = clientSecret;
2025-01-24 18:14:15 +00:00
this.scopes = scopes == null ? new ArrayList<>() : scopes;
this.useAsUsername =
useAsUsername != null ? validateUsernameAttribute(useAsUsername) : EMAIL;
2025-01-24 18:14:15 +00:00
this.authorizationUri = authorizationUri;
this.tokenUri = tokenUri;
this.userInfoUri = userInfoUri;
2025-01-22 15:14:06 +00:00
}
public void setScopes(String scopes) {
2025-01-24 18:14:15 +00:00
if (scopes != null && !scopes.isBlank()) {
this.scopes =
Arrays.stream(scopes.split(",")).map(String::trim).collect(Collectors.toList());
}
2025-01-22 15:14:06 +00:00
}
private UsernameAttribute validateUsernameAttribute(UsernameAttribute usernameAttribute) {
switch (name) {
case "google" -> {
return validateGoogleUsernameAttribute(usernameAttribute);
}
case "github" -> {
return validateGitHubUsernameAttribute(usernameAttribute);
}
case "keycloak" -> {
return validateKeycloakUsernameAttribute(usernameAttribute);
}
default -> {
return usernameAttribute;
}
}
}
private UsernameAttribute validateKeycloakUsernameAttribute(
UsernameAttribute usernameAttribute) {
switch (usernameAttribute) {
case EMAIL, PREFERRED_NAME -> {
return usernameAttribute;
}
default ->
throw new UnsupportedUsernameAttribute(
"The attribute "
+ usernameAttribute
+ "is not supported for "
+ clientName
+ ".");
}
}
private UsernameAttribute validateGoogleUsernameAttribute(UsernameAttribute usernameAttribute) {
switch (usernameAttribute) {
case EMAIL, NAME, GIVEN_NAME, PREFERRED_NAME -> {
return usernameAttribute;
}
default ->
throw new UnsupportedUsernameAttribute(
"The attribute "
+ usernameAttribute
+ "is not supported for "
+ clientName
+ ".");
}
}
private UsernameAttribute validateGitHubUsernameAttribute(UsernameAttribute usernameAttribute) {
switch (usernameAttribute) {
case EMAIL, NAME, LOGIN -> {
return usernameAttribute;
}
default ->
throw new UnsupportedUsernameAttribute(
"The attribute "
+ usernameAttribute
+ "is not supported for "
+ clientName
+ ".");
}
}
2025-01-24 18:14:15 +00:00
@Override
public String toString() {
return "Provider [name="
+ getName()
+ ", clientName="
+ getClientName()
+ ", clientId="
+ getClientId()
+ ", clientSecret="
+ (getClientSecret() != null && !getClientSecret().isEmpty() ? "*****" : "NULL")
+ ", scopes="
+ getScopes()
+ ", useAsUsername="
+ getUseAsUsername()
+ "]";
2025-01-22 15:14:06 +00:00
}
}