Skip to content

Commit

Permalink
#9 HTTP 401 with preflight requests
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreyfourmis committed Jan 19, 2024
1 parent 7a4d6bd commit 3a497a0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import java.lang.reflect.Type;
import org.keycloak.Config;
import org.keycloak.connections.jpa.JpaConnectionProvider;
import org.keycloak.http.HttpRequest;
import org.keycloak.http.HttpResponse;
import org.keycloak.jose.jws.JWSInput;
import org.keycloak.jose.jws.JWSInputException;
import org.keycloak.models.ClientModel;
Expand All @@ -21,6 +23,7 @@
import org.keycloak.services.managers.AppAuthManager.BearerTokenAuthenticator;
import org.keycloak.services.managers.AuthenticationManager;
import org.keycloak.services.managers.RealmManager;
import org.keycloak.services.resources.Cors;
import org.keycloak.services.resources.admin.AdminAuth;
import org.keycloak.services.resources.admin.AdminEventBuilder;

Expand All @@ -47,6 +50,18 @@ private void setup() {
setupAuth();
setupEvents();
setupProvider();
setupCors();
}

private void setupCors() {
HttpRequest request = session.getContext().getHttpRequest();
HttpResponse response = session.getContext().getHttpResponse();
Cors.add(request)
.allowedOrigins(auth.getToken())
.allowedMethods(CorsResource.METHODS)
.exposedHeaders("Location")
.auth()
.build(response);
}

private void setupAuth() {
Expand Down Expand Up @@ -120,4 +135,4 @@ protected final void setupProvider() {
this.tenantProvider = session.getProvider(TenantProvider.class);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package dev.sultanov.keycloak.multitenancy.resource;

import jakarta.ws.rs.OPTIONS;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.core.Response;
import org.keycloak.http.HttpRequest;
import org.keycloak.models.KeycloakSession;
import org.keycloak.services.resources.Cors;

public class CorsResource {

private final KeycloakSession session;
private final HttpRequest request;

public CorsResource(KeycloakSession session, HttpRequest request) {
this.session = session;
this.request = request;
}

public static final String[] METHODS = {
"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"
};

@OPTIONS
@Path("{any:.*}")
public Response preflight() {
return Cors.add(request, Response.ok()).auth().allowedMethods(METHODS).preflight().build();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package dev.sultanov.keycloak.multitenancy.resource;

import org.keycloak.http.HttpRequest;
import org.keycloak.models.KeycloakSession;
import org.keycloak.services.resource.RealmResourceProvider;

Expand All @@ -11,9 +12,15 @@ public TenantsResourceProvider(KeycloakSession session) {
this.session = session;
}


@Override
public Object getResource() {
return new TenantsResource(session);
HttpRequest request = session.getContext().getHttpRequest();
if (request != null && "OPTIONS".equals(request.getHttpMethod())) {
return new CorsResource(session, request);
} else {
return new TenantsResource(session);
}
}

@Override
Expand Down

0 comments on commit 3a497a0

Please sign in to comment.