[java-idp-plugin-vci] 01/06: Align and use helpers for Accept and Vary header handling in issuer metadata and status list
Codeberg
noreply at shibboleth.net
Thu Nov 12 14:29:36 UTC 2026
This is an automated email from the git hooks/post-receive script.
codeberg pushed a commit to branch main
in repository java-idp-plugin-vci.
View the commit online:
https://codeberg.org/Shibboleth/java-idp-plugin-vci/commit/cbd1de90593f569984d4d9d815834b940e0a5b9f
commit cbd1de90593f569984d4d9d815834b940e0a5b9f
Author: Janne Lauros <janne.lauros at csc.fi>
AuthorDate: Thu Nov 12 15:20:43 2026 +0200
Align and use helpers for Accept and Vary header handling in issuer metadata and status list
---
.../openidvci/statuslist/StatusListParameters.java | 33 +++---
.../FormOutboundIssuerMetadataResponseMessage.java | 37 ++-----
.../openidvci/profile/impl/HttpHeaderSupport.java | 120 +++++++++++++++++++++
.../logic/StatusListCwtRequestedPredicate.java | 16 ++-
...mOutboundIssuerMetadataResponseMessageTest.java | 13 +++
.../profile/impl/HttpHeaderSupportTest.java | 111 +++++++++++++++++++
.../logic/StatusListCwtRequestedPredicateTest.java | 25 +++++
7 files changed, 294 insertions(+), 61 deletions(-)
diff --git a/openid-vci-api/src/main/java/org/geant/shibboleth/plugin/openidvci/statuslist/StatusListParameters.java b/openid-vci-api/src/main/java/org/geant/shibboleth/plugin/openidvci/statuslist/StatusListParameters.java
index 4acc57f..7b3a01d 100644
--- a/openid-vci-api/src/main/java/org/geant/shibboleth/plugin/openidvci/statuslist/StatusListParameters.java
+++ b/openid-vci-api/src/main/java/org/geant/shibboleth/plugin/openidvci/statuslist/StatusListParameters.java
@@ -25,8 +25,7 @@ import javax.annotation.Nonnull;
* <a href="https://datatracker.ietf.org/doc/draft-ietf-oauth-status-list/">
* draft-ietf-oauth-status-list</a>.
*
- * Allocation and publishing use the same values. Changing any of these breaks
- * credentials already issued.
+ * Changing the values makes the build backwards incompatible.
*/
public final class StatusListParameters {
@@ -42,15 +41,7 @@ public final class StatusListParameters {
/** Length in bytes of the uncompressed status list. */
public static final int BYTE_LENGTH = (int) (CAPACITY * STATUS_BITS / 8);
- /**
- * Number of pieces a list is stored in.
- *
- * Revoking a slot rewrites one piece only and no stored value grows to the size
- * of a fully revoked list. We need chunks as there is maximun field size 'text'
- * type of sql fieds and we try not to cross it.
- *
- * Changing the parameter makes old storage records incompatible.
- */
+ /** Number of pieces a list is stored in. */
public static final int CHUNKS = 8;
/** Length in bytes of one piece of list. */
@@ -59,22 +50,24 @@ public final class StatusListParameters {
/** Number of slots described by one piece of list. */
public static final long CHUNK_CAPACITY = CAPACITY / CHUNKS;
- /**
- * Time a list is kept in storage after the bucket has ended. Only for clock
- * skew at verifiers.
- */
+ /** Time a list is kept in storage after the bucket has ended.*/
@Nonnull
public static final Duration RETENTION = Duration.ofDays(7);
- /**
- * Query parameter for the list a request is for. Appears in the url of a
- * credential.
- */
+ /** Query parameter for the list a request is for.*/
public static final String BUCKET_PARAMETER = "id";
- /** Member for the slot of a list, named as in a credential status reference. */
+ /** Member for the slot of a list, as in a credential status reference. */
public static final String INDEX_PARAMETER = "idx";
+ /** Media type of a Status List Token in JWT format. */
+ @Nonnull
+ public static final String MEDIA_TYPE_JWT = "application/statuslist+jwt";
+
+ /** Media type of a Status List Token in CWT format. */
+ @Nonnull
+ public static final String MEDIA_TYPE_CWT = "application/statuslist+cwt";
+
/** Constructor. */
private StatusListParameters() {
}
diff --git a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/profile/impl/FormOutboundIssuerMetadataResponseMessage.java b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/profile/impl/FormOutboundIssuerMetadataResponseMessage.java
index e82ae9b..27157ef 100644
--- a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/profile/impl/FormOutboundIssuerMetadataResponseMessage.java
+++ b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/profile/impl/FormOutboundIssuerMetadataResponseMessage.java
@@ -58,7 +58,7 @@ import net.shibboleth.shared.resolver.ResolverException;
/**
* Action forming {@link JSONSuccessResponse}, or
- * {@link SignedCredentialIssuerMetadataSuccessResponse} when the request names
+ * {@link SignedCredentialIssuerMetadataSuccessResponse} when the request asks for
* signed metadata and signing is configured, from the document the attached
* {@link CredentialIssuerMetadataResolver} resolves.
*
@@ -275,7 +275,7 @@ public class FormOutboundIssuerMetadataResponseMessage extends AbstractProfileAc
* @param response response to set the headers of
*/
private void setHeaders(@Nonnull final HttpServletResponse response) {
- response.setHeader(HttpHeaders.VARY, HttpHeaders.ACCEPT);
+ HttpHeaderSupport.addVary(response, HttpHeaders.ACCEPT);
response.setHeader(HttpHeaders.CACHE_CONTROL, cacheControl);
response.setHeader(HttpHeaders.X_CONTENT_TYPE_OPTIONS, FormOutboundMetadataDocumentResponseMessage.NOSNIFF);
response.setHeader(HttpHeaders.CONTENT_SECURITY_POLICY,
@@ -283,42 +283,17 @@ public class FormOutboundIssuerMetadataResponseMessage extends AbstractProfileAc
}
/**
- * Get whether the request names signed metadata and not the unsigned document.
+ * Get whether the request asks for signed metadata and not the unsigned document.
*
- * Only an exact media type names a representation, a wildcard names neither.
+ * Only an exact media type selects a representation, a wildcard selects neither.
*
* @return whether to publish signed metadata
*/
private boolean prefersSignedMetadata() {
final HttpServletRequest request = getHttpServletRequest();
- if (request == null) {
- return false;
- }
-
- final String accept = request.getHeader("Accept");
- if (accept == null || accept.isBlank()) {
- return false;
- }
-
- return names(accept, SIGNED_MEDIA_TYPE) && !names(accept, UNSIGNED_MEDIA_TYPE);
- }
-
- /**
- * Get whether an Accept header names a media type.
- *
- * @param accept header to read
- * @param mediaType media type to look for
- * @return whether the header names the media type
- */
- private boolean names(@Nonnull final String accept, @Nonnull final String mediaType) {
-
- for (final String entry : accept.split(",")) {
- if (mediaType.equalsIgnoreCase(entry.split(";")[0].trim())) {
- return true;
- }
- }
- return false;
+ return HttpHeaderSupport.accepts(request, SIGNED_MEDIA_TYPE)
+ && !HttpHeaderSupport.accepts(request, UNSIGNED_MEDIA_TYPE);
}
/**
diff --git a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/profile/impl/HttpHeaderSupport.java b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/profile/impl/HttpHeaderSupport.java
new file mode 100644
index 0000000..8f5077e
--- /dev/null
+++ b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/profile/impl/HttpHeaderSupport.java
@@ -0,0 +1,120 @@
+/*
+ * Copyright (c) 2025, GÉANT
+ *
+ * Licensed under the Apache License, Version 2.0 (the “License”); you may not
+ * use this file except in compliance with the License. You may obtain a copy
+ * of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an “AS IS” BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.geant.shibboleth.plugin.openidvci.profile.impl;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+import com.google.common.net.HttpHeaders;
+
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import net.shibboleth.shared.annotation.constraint.NotEmpty;
+
+/** Support for the headers of a request and a response. */
+public final class HttpHeaderSupport {
+
+ /** Vary value matching every request header field. */
+ @Nonnull
+ @NotEmpty
+ private static final String ANY = "*";
+
+ /** Name of the quality parameter of a media range. */
+ @Nonnull
+ @NotEmpty
+ private static final String QUALITY = "q";
+
+ /** Constructor. */
+ private HttpHeaderSupport() {
+ }
+
+ /**
+ * Get whether the Accept header of a request lists a media type with a quality
+ * other than zero. A wildcard matches no media type.
+ *
+ * @param request request to read
+ * @param mediaType media type to look for
+ * @return whether the request lists the media type
+ */
+ public static boolean accepts(@Nullable final HttpServletRequest request,
+ @Nonnull @NotEmpty final String mediaType) {
+ if (request == null || request.getHeaders(HttpHeaders.ACCEPT) == null) {
+ return false;
+ }
+ for (final String value : Collections.list(request.getHeaders(HttpHeaders.ACCEPT))) {
+ for (final String range : value.split(",")) {
+ final String[] parts = range.split(";");
+ if (mediaType.equalsIgnoreCase(parts[0].trim()) && !isRefused(parts)) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Get whether a media range carries a quality of zero.
+ *
+ * @param parts the media type and its parameters
+ * @return whether the quality is zero
+ */
+ private static boolean isRefused(@Nonnull final String[] parts) {
+ for (int i = 1; i < parts.length; i++) {
+ final String[] parameter = parts[i].split("=", 2);
+ if (parameter.length == 2 && QUALITY.equalsIgnoreCase(parameter[0].trim())) {
+ try {
+ return Double.parseDouble(parameter[1].trim()) == 0;
+ } catch (final NumberFormatException e) {
+ return false;
+ }
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Add a request header field to the Vary header, as one header keeping the
+ * fields already in it.
+ *
+ * @param response response to set the header of
+ * @param field name of the request header field
+ */
+ public static void addVary(@Nonnull final HttpServletResponse response, @Nonnull @NotEmpty final String field) {
+ final List<String> fields = new ArrayList<>();
+ for (final String value : response.getHeaders(HttpHeaders.VARY)) {
+ for (final String part : value.split(",")) {
+ final String name = part.trim();
+ if (ANY.equals(name)) {
+ response.setHeader(HttpHeaders.VARY, ANY);
+ return;
+ }
+ if (!name.isEmpty() && fields.stream().noneMatch(name::equalsIgnoreCase)) {
+ fields.add(name);
+ }
+ }
+ }
+ if (fields.stream().noneMatch(field::equalsIgnoreCase)) {
+ fields.add(field);
+ }
+ response.setHeader(HttpHeaders.VARY, String.join(", ", fields));
+ }
+
+}
diff --git a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/profile/logic/StatusListCwtRequestedPredicate.java b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/profile/logic/StatusListCwtRequestedPredicate.java
index 73e18d7..db606b1 100644
--- a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/profile/logic/StatusListCwtRequestedPredicate.java
+++ b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/profile/logic/StatusListCwtRequestedPredicate.java
@@ -15,13 +15,14 @@
*/
package org.geant.shibboleth.plugin.openidvci.profile.logic;
-import java.util.Locale;
import java.util.function.Predicate;
import java.util.function.Supplier;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
+import org.geant.shibboleth.plugin.openidvci.profile.impl.HttpHeaderSupport;
+import org.geant.shibboleth.plugin.openidvci.statuslist.StatusListParameters;
import org.opensaml.profile.context.ProfileRequestContext;
import jakarta.servlet.http.HttpServletRequest;
@@ -29,17 +30,17 @@ import net.shibboleth.shared.logic.Constraint;
/**
* Whether the request asks for a Status List Token in CWT format: its 'Accept'
- * header names 'application/statuslist+cwt' and not 'application/statuslist+jwt'.
+ * header lists the media type of the CWT format and not the one of the JWT format.
*/
public class StatusListCwtRequestedPredicate implements Predicate<ProfileRequestContext> {
/** Media type of the CWT format. */
@Nonnull
- private static final String CWT = "application/statuslist+cwt";
+ private static final String CWT = StatusListParameters.MEDIA_TYPE_CWT;
/** Media type of the JWT format. */
@Nonnull
- private static final String JWT = "application/statuslist+jwt";
+ private static final String JWT = StatusListParameters.MEDIA_TYPE_JWT;
/** Supplier of the servlet request the header is read from. */
@Nonnull
@@ -59,12 +60,7 @@ public class StatusListCwtRequestedPredicate implements Predicate<ProfileRequest
public boolean test(@Nullable final ProfileRequestContext profileRequestContext) {
final HttpServletRequest request = httpServletRequestSupplier.get();
- final String accept = request == null ? null : request.getHeader("Accept");
- if (accept == null) {
- return false;
- }
- final String types = accept.toLowerCase(Locale.ROOT);
- return types.contains(CWT) && !types.contains(JWT);
+ return HttpHeaderSupport.accepts(request, CWT) && !HttpHeaderSupport.accepts(request, JWT);
}
}
diff --git a/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/profile/impl/FormOutboundIssuerMetadataResponseMessageTest.java b/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/profile/impl/FormOutboundIssuerMetadataResponseMessageTest.java
index ca2c57a..bfd4c72 100644
--- a/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/profile/impl/FormOutboundIssuerMetadataResponseMessageTest.java
+++ b/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/profile/impl/FormOutboundIssuerMetadataResponseMessageTest.java
@@ -235,6 +235,16 @@ public class FormOutboundIssuerMetadataResponseMessageTest {
EventIds.IO_ERROR);
}
+ @Test
+ public void testVaryKeepsNamedFields() throws Exception {
+ httpResponse.addHeader("Vary", "Accept-Encoding");
+ final FormOutboundIssuerMetadataResponseMessage action = actionOf(CredentialIssuerMetadata.parse(members()));
+
+ ActionTestingSupport.assertProceedEvent(action.execute(requestCtx));
+
+ Assert.assertEquals(httpResponse.getHeaders("Vary"), List.of("Accept-Encoding, Accept"));
+ }
+
@Test
public void testUnsignedCarriesVaryAccept() throws Exception {
final FormOutboundIssuerMetadataResponseMessage action = actionOf(CredentialIssuerMetadata.parse(members()));
@@ -268,6 +278,8 @@ public class FormOutboundIssuerMetadataResponseMessageTest {
{"application/json"},
{"application/json, application/jwt"},
{"text/html"},
+ {"application/jwt;q=0"},
+ {"application/jwtx"},
};
}
@@ -291,6 +303,7 @@ public class FormOutboundIssuerMetadataResponseMessageTest {
{"application/jwt, text/html"},
{"application/jwt;q=0.9"},
{" application/jwt ; charset=utf-8 "},
+ {"application/jwt, application/json;q=0"},
};
}
diff --git a/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/profile/impl/HttpHeaderSupportTest.java b/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/profile/impl/HttpHeaderSupportTest.java
new file mode 100644
index 0000000..a91cff3
--- /dev/null
+++ b/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/profile/impl/HttpHeaderSupportTest.java
@@ -0,0 +1,111 @@
+/*
+ * Copyright (c) 2025, GÉANT
+ *
+ * Licensed under the Apache License, Version 2.0 (the “License”); you may not
+ * use this file except in compliance with the License. You may obtain a copy
+ * of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an “AS IS” BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.geant.shibboleth.plugin.openidvci.profile.impl;
+
+import java.util.List;
+
+import org.springframework.mock.web.MockHttpServletRequest;
+import org.springframework.mock.web.MockHttpServletResponse;
+import org.testng.Assert;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/** Tests for {@link HttpHeaderSupport}. */
+public class HttpHeaderSupportTest {
+
+ private MockHttpServletResponse response;
+
+ @BeforeMethod
+ public void setUp() {
+ response = new MockHttpServletResponse();
+ }
+
+ private static MockHttpServletRequest requestOf(final String... accept) {
+ final MockHttpServletRequest request = new MockHttpServletRequest();
+ for (final String value : accept) {
+ request.addHeader("Accept", value);
+ }
+ return request;
+ }
+
+ @Test
+ public void testAccepts() {
+ Assert.assertTrue(HttpHeaderSupport.accepts(requestOf("application/jwt"), "application/jwt"));
+ Assert.assertTrue(HttpHeaderSupport.accepts(requestOf("text/html, Application/JWT;q=0.5"), "application/jwt"));
+ Assert.assertTrue(HttpHeaderSupport.accepts(requestOf("text/html", "application/jwt"), "application/jwt"));
+ Assert.assertTrue(HttpHeaderSupport.accepts(requestOf("application/jwt;q=abc"), "application/jwt"));
+ Assert.assertTrue(HttpHeaderSupport.accepts(requestOf("application/jwt;charset=utf-8;Q=1"), "application/jwt"));
+ }
+
+ @Test
+ public void testDoesNotAccept() {
+ Assert.assertFalse(HttpHeaderSupport.accepts(null, "application/jwt"));
+ Assert.assertFalse(HttpHeaderSupport.accepts(requestOf(), "application/jwt"));
+ Assert.assertFalse(HttpHeaderSupport.accepts(requestOf(""), "application/jwt"));
+ Assert.assertFalse(HttpHeaderSupport.accepts(requestOf("*/*"), "application/jwt"));
+ Assert.assertFalse(HttpHeaderSupport.accepts(requestOf("application/*"), "application/jwt"));
+ Assert.assertFalse(HttpHeaderSupport.accepts(requestOf("application/jwtx"), "application/jwt"));
+ Assert.assertFalse(HttpHeaderSupport.accepts(requestOf("application/jwt;q=0"), "application/jwt"));
+ Assert.assertFalse(HttpHeaderSupport.accepts(requestOf("application/jwt; q=0.000"), "application/jwt"));
+ }
+
+ @Test
+ public void testNoVary() {
+ HttpHeaderSupport.addVary(response, "Accept");
+
+ Assert.assertEquals(response.getHeaders("Vary"), List.of("Accept"));
+ }
+
+ @Test
+ public void testKeepsNamedFields() {
+ response.addHeader("Vary", "Accept-Encoding");
+
+ HttpHeaderSupport.addVary(response, "Accept");
+
+ Assert.assertEquals(response.getHeaders("Vary"), List.of("Accept-Encoding, Accept"));
+ }
+
+ @Test
+ public void testSeveralHeadersBecomeOne() {
+ response.addHeader("Vary", "Accept-Encoding, Origin");
+ response.addHeader("Vary", "origin");
+
+ HttpHeaderSupport.addVary(response, "Accept");
+
+ Assert.assertEquals(response.getHeaders("Vary"), List.of("Accept-Encoding, Origin, Accept"));
+ }
+
+ @Test
+ public void testAlreadyNamed() {
+ response.addHeader("Vary", "accept, Accept-Encoding");
+
+ HttpHeaderSupport.addVary(response, "Accept");
+
+ Assert.assertEquals(response.getHeaders("Vary"), List.of("accept, Accept-Encoding"));
+ }
+
+ @Test
+ public void testAny() {
+ response.addHeader("Vary", "Accept-Encoding");
+ response.addHeader("Vary", "*");
+
+ HttpHeaderSupport.addVary(response, "Accept");
+
+ Assert.assertEquals(response.getHeaders("Vary"), List.of("*"));
+ }
+
+}
diff --git a/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/profile/logic/StatusListCwtRequestedPredicateTest.java b/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/profile/logic/StatusListCwtRequestedPredicateTest.java
index 6e50976..b6b2d2b 100644
--- a/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/profile/logic/StatusListCwtRequestedPredicateTest.java
+++ b/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/profile/logic/StatusListCwtRequestedPredicateTest.java
@@ -41,4 +41,29 @@ public class StatusListCwtRequestedPredicateTest {
Assert.assertFalse(test("*/*"));
Assert.assertFalse(test(null));
}
+
+ @Test
+ public void exactMediaType() {
+ Assert.assertFalse(test("application/statuslist+cwt2"));
+ Assert.assertFalse(test("x-application/statuslist+cwt"));
+ Assert.assertTrue(test(" application/statuslist+cwt ; q=0.5 "));
+ }
+
+ @Test
+ public void refused() {
+ Assert.assertFalse(test("application/statuslist+cwt;q=0"));
+ Assert.assertFalse(test("application/statuslist+cwt;q=0.0, text/html"));
+ Assert.assertTrue(test("application/statuslist+cwt, application/statuslist+jwt;q=0"));
+ }
+
+ @Test
+ public void severalHeaders() {
+ final MockHttpServletRequest request = new MockHttpServletRequest();
+ request.addHeader("Accept", "text/html");
+ request.addHeader("Accept", "application/statuslist+cwt");
+ Assert.assertTrue(new StatusListCwtRequestedPredicate(() -> request).test(null));
+
+ request.addHeader("Accept", "application/statuslist+jwt");
+ Assert.assertFalse(new StatusListCwtRequestedPredicate(() -> request).test(null));
+ }
}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list