add basic auth support

This commit is contained in:
Hugo Renard 2022-04-29 11:09:51 +02:00
parent ff58a5664b
commit 2bb7eba346
Signed by: hougo
GPG key ID: 3A285FD470209C59
3 changed files with 42 additions and 3 deletions

View file

@ -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);
}
}

View file

@ -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);

View file

@ -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")