|
| 1 | +package info.unterrainer.commons.httpserver.accessmanager; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.UncheckedIOException; |
| 5 | +import java.net.URI; |
| 6 | +import java.net.URISyntaxException; |
| 7 | +import java.net.http.HttpClient; |
| 8 | +import java.net.http.HttpRequest; |
| 9 | +import java.net.http.HttpResponse; |
| 10 | +import java.net.http.HttpResponse.BodyHandlers; |
| 11 | +import java.security.PublicKey; |
| 12 | +import java.util.Set; |
| 13 | + |
| 14 | +import org.eclipse.jetty.http.HttpHeader; |
| 15 | +import org.keycloak.TokenVerifier; |
| 16 | +import org.keycloak.common.VerificationException; |
| 17 | +import org.keycloak.representations.AccessToken; |
| 18 | +import org.keycloak.representations.idm.PublishedRealmRepresentation; |
| 19 | + |
| 20 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | + |
| 22 | +import info.unterrainer.commons.httpserver.exceptions.ForbiddenException; |
| 23 | +import info.unterrainer.commons.httpserver.exceptions.UnauthorizedException; |
| 24 | +import io.javalin.core.security.AccessManager; |
| 25 | +import io.javalin.core.security.Role; |
| 26 | +import io.javalin.http.Context; |
| 27 | +import io.javalin.http.Handler; |
| 28 | + |
| 29 | +public class HttpAccessManager implements AccessManager { |
| 30 | + |
| 31 | + private PublicKey publicKey; |
| 32 | + |
| 33 | + public HttpAccessManager() { |
| 34 | + try { |
| 35 | + String authUrl = "http://localhost:8080" + "/auth/realms/" + "nexus"; |
| 36 | + |
| 37 | + HttpClient client = HttpClient.newHttpClient(); |
| 38 | + HttpRequest request = HttpRequest.newBuilder().uri(new URI(authUrl)).build(); |
| 39 | + ObjectMapper objectMapper = new ObjectMapper(); |
| 40 | + |
| 41 | + client.sendAsync(request, BodyHandlers.ofString()).thenApply(HttpResponse::body).thenAccept(body -> { |
| 42 | + try { |
| 43 | + publicKey = objectMapper.readValue(body, PublishedRealmRepresentation.class).getPublicKey(); |
| 44 | + } catch (IOException e) { |
| 45 | + throw new UncheckedIOException(e); |
| 46 | + } |
| 47 | + }).join(); |
| 48 | + } catch (URISyntaxException e) { |
| 49 | + throw new IllegalStateException(e); |
| 50 | + } |
| 51 | + |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void manage(Handler handler, Context ctx, Set<Role> permittedRoles) throws Exception { |
| 56 | + |
| 57 | + checkAccess(ctx, permittedRoles); |
| 58 | + |
| 59 | + handler.handle(ctx); |
| 60 | + } |
| 61 | + |
| 62 | + private void checkAccess(Context ctx, Set<Role> permittedRoles) { |
| 63 | + |
| 64 | + if (permittedRoles.isEmpty() || permittedRoles.contains(DefaultRoles.PUBLIC)) { |
| 65 | + return; |
| 66 | + } |
| 67 | + |
| 68 | + String authorizationHeader = ctx.header(HttpHeader.AUTHORIZATION.asString()); |
| 69 | + TokenVerifier<AccessToken> accessToken = TokenVerifier.create(authorizationHeader, AccessToken.class); |
| 70 | + accessToken.publicKey(publicKey); |
| 71 | + |
| 72 | + try { |
| 73 | + accessToken.verifySignature(); |
| 74 | + } catch (VerificationException e) { |
| 75 | + throw new UnauthorizedException(); |
| 76 | + } |
| 77 | + |
| 78 | + try { |
| 79 | + accessToken.verify(); |
| 80 | + } catch (VerificationException e) { |
| 81 | + throw new ForbiddenException(); |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments