diff --git a/src/main/java/sh/libre/scim/core/BasicAuthentication.java b/src/main/java/sh/libre/scim/core/BasicAuthentication.java new file mode 100644 index 0000000000..227681d2d2 --- /dev/null +++ b/src/main/java/sh/libre/scim/core/BasicAuthentication.java @@ -0,0 +1,23 @@ +package sh.libre.scim.core; + +import java.io.IOException; +import java.util.Base64; + +import javax.ws.rs.client.ClientRequestContext; +import javax.ws.rs.client.ClientRequestFilter; + +public class BasicAuthentication implements ClientRequestFilter { + private final String user; + private final String password; + + BasicAuthentication(String user, String password) { + this.user = user; + this.password = password; + } + + @Override + public void filter(ClientRequestContext requestContext) throws IOException { + var token = Base64.getEncoder().encodeToString((user + ":" + password).getBytes()); + requestContext.getHeaders().add("Authorization", "Basic " + token); + } +} diff --git a/src/main/java/sh/libre/scim/core/ScimClient.java b/src/main/java/sh/libre/scim/core/ScimClient.java index 7b28823d0d..a85d2f0096 100644 --- a/src/main/java/sh/libre/scim/core/ScimClient.java +++ b/src/main/java/sh/libre/scim/core/ScimClient.java @@ -36,8 +36,14 @@ public class ScimClient { this.session = session; var target = client.target(model.get("endpoint")); - if (model.get("auth-mode").equals("BEARER")) { - target = target.register(new BearerAuthentication(model.get("auth-bearer-token"))); + switch (model.get("auth-mode")) { + case "BEARER": + target = target.register(new BearerAuthentication(model.get("auth-bearer-token"))); + break; + case "BASIC_AUTH": + target = target.register(new BasicAuthentication( + model.get("auth-basic-auth-user"), + model.get("auth-basic-auth-pass"))); } scimService = new ScimService(target); diff --git a/src/main/java/sh/libre/scim/storage/ScimStorageProviderFactory.java b/src/main/java/sh/libre/scim/storage/ScimStorageProviderFactory.java index 272fc48f03..3f18b7bd34 100644 --- a/src/main/java/sh/libre/scim/storage/ScimStorageProviderFactory.java +++ b/src/main/java/sh/libre/scim/storage/ScimStorageProviderFactory.java @@ -52,7 +52,7 @@ public class ScimStorageProviderFactory .type(ProviderConfigProperty.LIST_TYPE) .label("Auth mode") .helpText("Select the authorization mode") - .options("NONE", "BEARER") + .options("NONE", "BEARER", "BASIC_AUTH") .defaultValue("NONE") .add() .property() @@ -62,6 +62,16 @@ public class ScimStorageProviderFactory .helpText("Add a bearer token in the authorization header") .add() .property() + .name("auth-basic-auth-user") + .type(ProviderConfigProperty.STRING_TYPE) + .label("BasicAuth user") + .add() + .property() + .name("auth-basic-auth-pass") + .type(ProviderConfigProperty.PASSWORD) + .label("BasicAuth password") + .add() + .property() .name("sync-import") .type(ProviderConfigProperty.BOOLEAN_TYPE) .label("Enable import during sync")