rename package
Co-authored-by: kritzl <kritzl@kritzl.dev>
This commit is contained in:
parent
e318c3b386
commit
c518d4a514
4 changed files with 4 additions and 15 deletions
|
|
@ -5,7 +5,7 @@
|
|||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>de.ccc.hamburg.keycloak</groupId>
|
||||
<groupId>de.ccc.hamburg.keycloak.ssh_key</groupId>
|
||||
<artifactId>ssh-key-provider</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,50 @@
|
|||
package de.ccc.hamburg.keycloak.ssh_key;
|
||||
|
||||
import jakarta.ws.rs.ForbiddenException;
|
||||
import jakarta.ws.rs.NotAuthorizedException;
|
||||
import org.keycloak.models.ClientModel;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.services.managers.AppAuthManager;
|
||||
import org.keycloak.services.managers.Auth;
|
||||
import org.keycloak.services.managers.AuthenticationManager;
|
||||
|
||||
import java.util.function.Function;
|
||||
|
||||
public class AuthHelper {
|
||||
|
||||
public static AuthenticationManager.AuthResult getAuthResult(KeycloakSession session, Function<AuthenticationManager.AuthResult, Boolean> authFn) {
|
||||
AuthenticationManager.AuthResult auth = new AppAuthManager.BearerTokenAuthenticator(session).authenticate();
|
||||
|
||||
if (auth == null) {
|
||||
throw new NotAuthorizedException("Bearer");
|
||||
} else if (!authFn.apply(auth)) {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
return auth;
|
||||
}
|
||||
|
||||
public static Auth getAuth(KeycloakSession session, Function<AuthenticationManager.AuthResult, Boolean> authFn) {
|
||||
return getAuth(session, getAuthResult(session, authFn));
|
||||
}
|
||||
|
||||
public static Auth getAuth(KeycloakSession session, String clientId, Function<AuthenticationManager.AuthResult, Boolean> authFn) {
|
||||
return getAuth(session, getAuthResult(session, authFn), clientId);
|
||||
}
|
||||
|
||||
public static Auth getAuth(KeycloakSession session, AuthenticationManager.AuthResult authResult) {
|
||||
return getAuth(session, authResult, null);
|
||||
}
|
||||
|
||||
public static Auth getAuth(KeycloakSession session, AuthenticationManager.AuthResult authResult, String clientId) {
|
||||
RealmModel realm = session.getContext().getRealm();
|
||||
ClientModel client;
|
||||
if (clientId == null) {
|
||||
client = authResult.getClient();
|
||||
} else {
|
||||
client = realm.getClientByClientId(clientId);
|
||||
}
|
||||
return new Auth(realm, authResult.getToken(), authResult.getUser(), client, authResult.getSession(), false);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,78 @@
|
|||
package de.ccc.hamburg.keycloak.ssh_key;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import jakarta.ws.rs.GET;
|
||||
import jakarta.ws.rs.Path;
|
||||
import jakarta.ws.rs.Produces;
|
||||
import jakarta.ws.rs.core.Response;
|
||||
import jakarta.ws.rs.core.MediaType;
|
||||
import org.keycloak.models.GroupModel;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.models.UserModel;
|
||||
import org.keycloak.models.UserProvider;
|
||||
import org.keycloak.services.managers.Auth;
|
||||
import org.keycloak.services.resource.RealmResourceProvider;
|
||||
|
||||
public class SSHKeyResourceProvider implements RealmResourceProvider {
|
||||
private static final Logger LOG = Logger.getLogger(SSHKeyResourceProvider.class);
|
||||
private final KeycloakSession session;
|
||||
|
||||
public SSHKeyResourceProvider(KeycloakSession keycloakSession) {
|
||||
this.session = keycloakSession;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getResource() {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("hello")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
@Operation(summary = "Public hello endpoint", description = "This endpoint returns hello and the name of the requested realm.")
|
||||
@APIResponse(responseCode = "200", description = "", content = {
|
||||
@Content(schema = @Schema(implementation = Response.class, type = SchemaType.OBJECT)) })
|
||||
public Response helloAnonymous() {
|
||||
return Response.ok(Map.of("hello", session.getContext().getRealm().getName())).build();
|
||||
}
|
||||
|
||||
@GET
|
||||
@Path("hello-auth")
|
||||
@Produces(MediaType.APPLICATION_JSON)
|
||||
public Response helloAuthenticated() {
|
||||
UserProvider userProvider = session.users();
|
||||
|
||||
try {
|
||||
Auth auth = AuthHelper.getAuth(session,
|
||||
authResult -> authResult.getToken().getIssuedFor().equals("admin-cli"));
|
||||
|
||||
RealmModel realm = session.getContext().getRealm();
|
||||
GroupModel group = realm.getGroupById("fbf5f78b-d2be-49dd-b04f-11a5e8ee583f");
|
||||
|
||||
LOG.info(String.format("Getting Users from Group \"%s\" with ID %s", group.getName(), group.getId()));
|
||||
|
||||
Stream<UserModel> users = userProvider.getGroupMembersStream(realm, group);
|
||||
|
||||
users.forEach(user -> {
|
||||
String sshKey = user.getAttributeStream("ssh-key-1").findFirst().get();
|
||||
LOG.info(String.format("SSH Key of %s: %s", user.getUsername(), sshKey));
|
||||
});
|
||||
|
||||
|
||||
return Response.ok(Map.of("hello", auth.getUser().getUsername())).build();
|
||||
} catch (Exception e) {
|
||||
System.err.println(e);
|
||||
return Response.status(401, e.getMessage()).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
package de.ccc.hamburg.keycloak.ssh_key;
|
||||
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.KeycloakSessionFactory;
|
||||
import org.keycloak.services.resource.RealmResourceProvider;
|
||||
import org.keycloak.services.resource.RealmResourceProviderFactory;
|
||||
import com.google.auto.service.AutoService;
|
||||
import org.keycloak.Config;
|
||||
|
||||
@AutoService(RealmResourceProviderFactory.class)
|
||||
public class SSHKeyResourceProviderFactory implements RealmResourceProviderFactory {
|
||||
static final String PROVIDER_ID = "ssh-key-provider";
|
||||
|
||||
@Override
|
||||
public RealmResourceProvider create(KeycloakSession keycloakSession) {
|
||||
return new SSHKeyResourceProvider(keycloakSession);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(Config.Scope scope) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void postInit(KeycloakSessionFactory keycloakSessionFactory) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getId() {
|
||||
return PROVIDER_ID;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue