Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#9 HTTP 401 with preflight requests #15

Merged
merged 2 commits into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,26 @@
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.services.resources.Cors;

public class CorsResource {

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

private final HttpRequest request;

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

@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 @@ -13,7 +14,12 @@ public TenantsResourceProvider(KeycloakSession session) {

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

@Override
Expand Down
Loading