From 81877a6524d8721ec30debb771e050886c37c861 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Fri, 24 Nov 2023 12:17:22 +0000
Subject: [PATCH] ContextTypeHelper: do account for the "Accept" header (per CAS specs)

---
 src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java |   26 ++++++++++----------------
 1 files changed, 10 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java b/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
index 789c8f9..f74b9f5 100644
--- a/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
+++ b/src/main/java/org/keycloak/protocol/cas/utils/ContentTypeHelper.java
@@ -1,33 +1,27 @@
 package org.keycloak.protocol.cas.utils;
 
-import jakarta.ws.rs.BadRequestException;
 import jakarta.ws.rs.core.*;
-import org.jboss.resteasy.spi.HttpRequest;
 import org.keycloak.protocol.cas.CASLoginProtocol;
+import org.keycloak.protocol.cas.representations.CASErrorCode;
 
 public class ContentTypeHelper {
-    private final HttpRequest request;
-    private final Request restRequest;
     private final UriInfo uriInfo;
 
-    public ContentTypeHelper(HttpRequest request, Request restRequest, UriInfo uriInfo) {
-        this.request = request;
-        this.restRequest = restRequest;
+    public ContentTypeHelper(UriInfo uriInfo) {
         this.uriInfo = uriInfo;
     }
 
     public MediaType selectResponseType() {
         String format = uriInfo.getQueryParameters().getFirst(CASLoginProtocol.FORMAT_PARAM);
         if (format != null && !format.isEmpty()) {
-            //if parameter is set, it overrides all header values (see spec section 2.5.1)
-            request.getMutableHeaders().putSingle(HttpHeaders.ACCEPT, "application/" + format.toLowerCase());
+            if (format.equalsIgnoreCase("json")) {
+                return MediaType.APPLICATION_JSON_TYPE;
+            } else if (format.equalsIgnoreCase("xml")) {
+                return MediaType.APPLICATION_XML_TYPE;
+            } else {
+                throw new CASValidationException(CASErrorCode.INVALID_REQUEST, "Unsupported value of parameter " + CASLoginProtocol.FORMAT_PARAM, Response.Status.BAD_REQUEST);
+            }
         }
-        try {
-            Variant variant = restRequest.selectVariant(Variant.mediaTypes(MediaType.APPLICATION_XML_TYPE, MediaType.APPLICATION_JSON_TYPE).build());
-            return variant == null ? MediaType.APPLICATION_XML_TYPE : variant.getMediaType();
-        } catch (BadRequestException e) {
-            //the default Accept header set by java.net.HttpURLConnection is invalid (cf. RESTEASY-960)
-            return MediaType.APPLICATION_XML_TYPE;
-        }
+        return MediaType.APPLICATION_XML_TYPE;
     }
 }

--
Gitblit v1.9.1