[java-idp-plugin-vci] branch dev/REVIEWOFFERCALLERFLOW updated: credential offer request validation, tx code value and hints checked, malformed input refused with parse error
Codeberg
noreply at shibboleth.net
Mon Nov 16 08:44:23 UTC 2026
This is an automated email from the git hooks/post-receive script.
codeberg pushed a commit to branch dev/REVIEWOFFERCALLERFLOW
in repository java-idp-plugin-vci.
View the commit online:
https://codeberg.org/Shibboleth/java-idp-plugin-vci/commit/f4dd868e3d6a41a4cc0df466ac4fcc16c21e3d47
The following commit(s) were added to refs/heads/dev/REVIEWOFFERCALLERFLOW by this push:
new f4dd868 credential offer request validation, tx code value and hints checked, malformed input refused with parse error
f4dd868 is described below
commit f4dd868e3d6a41a4cc0df466ac4fcc16c21e3d47
Author: Janne Lauros <janne.lauros at csc.fi>
AuthorDate: Mon Nov 16 10:43:57 2026 +0200
credential offer request validation, tx code value and hints checked, malformed input refused with parse error
---
.../messaging/impl/CredentialOfferRequest.java | 40 +++----
.../impl/CredentialOfferRequestedClaim.java | 8 +-
.../impl/CredentialOfferRequestedCredential.java | 2 +-
.../messaging/impl/CredentialOfferTxCode.java | 116 ++++++++++++---------
.../messaging/impl/CredentialOfferRequestTest.java | 40 ++++++-
.../messaging/impl/CredentialOfferTxCodeTest.java | 73 ++++++++++++-
6 files changed, 204 insertions(+), 75 deletions(-)
diff --git a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequest.java b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequest.java
index 83e0344..042526e 100644
--- a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequest.java
+++ b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequest.java
@@ -22,12 +22,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import javax.annotation.Nonnull;
import javax.annotation.Nullable;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -43,10 +39,6 @@ import com.nimbusds.oauth2.sdk.http.HTTPRequest;
*/
public class CredentialOfferRequest extends AbstractOptionallyIdentifiedRequest {
- /** Class logger. */
- @Nonnull
- private static Logger log = LoggerFactory.getLogger(CredentialOfferRequest.class);
-
/** Credential offer request content in pre-auth flow. */
@Nullable
private final Map<String, List<CredentialDataset>> preAuthorizedCredentials;
@@ -58,16 +50,10 @@ public class CredentialOfferRequest extends AbstractOptionallyIdentifiedRequest
/**
* Constructor.
*
- * @param uri The URI of the endpoint (HTTP or HTTPS) for
- * which the request is intended, {@code null}
- * if not specified (if, for example, the
- * {@link #toHTTPRequest()} method will not be
- * used).
- * @param clientAuth The client authentication, {@code null} if
- * none.
- * @param preAuthorizedCredentials Credentials offered in the pre-authorized
- * code flow.
- * @param credentialOfferTxCode Transaction Code the wallet must present.
+ * @param uri endpoint URI
+ * @param clientAuth client authentication
+ * @param preAuthorizedCredentials credentials offered in the pre-authorized code flow
+ * @param credentialOfferTxCode Transaction Code the wallet must present
*/
public CredentialOfferRequest(@Nullable final URI uri, @Nullable final ClientAuthentication clientAuth,
@Nullable final Map<String, List<CredentialDataset>> preAuthorizedCredentials,
@@ -104,7 +90,6 @@ public class CredentialOfferRequest extends AbstractOptionallyIdentifiedRequest
* @return parsed request.
* @throws ParseException if parsing failed.
*/
- @SuppressWarnings("unchecked")
public static CredentialOfferRequest parse(final HTTPRequest httpRequest) throws ParseException {
httpRequest.ensureMethod(HTTPRequest.Method.POST);
httpRequest.ensureEntityContentType(ContentType.APPLICATION_JSON);
@@ -127,6 +112,9 @@ public class CredentialOfferRequest extends AbstractOptionallyIdentifiedRequest
} catch (final IllegalArgumentException | JsonProcessingException e1) {
throw new ParseException(e1.getMessage());
}
+ if (content == null) {
+ throw new ParseException("Invalid credential offer request: body is not a JSON object");
+ }
if (content.containsKey("codeCredentials")) {
throw new ParseException("Invalid credential offer request: The parameter 'codeCredentials' is not "
@@ -138,11 +126,17 @@ public class CredentialOfferRequest extends AbstractOptionallyIdentifiedRequest
"Invalid credential offer request: The parameter 'preAuthorizedCredentials' must exist");
}
- final Map<String, Object> preAuthorizedCredentialsInRequest = (Map<String, Object>) content
- .get("preAuthorizedCredentials");
+ if (!(content.get("preAuthorizedCredentials") instanceof Map<?, ?> preAuthorizedCredentialsInRequest)) {
+ throw new ParseException(
+ "Invalid credential offer request: The parameter 'preAuthorizedCredentials' must be an object");
+ }
+ if (preAuthorizedCredentialsInRequest.isEmpty()) {
+ throw new ParseException(
+ "Invalid credential offer request: The parameter 'preAuthorizedCredentials' must not be empty");
+ }
final Map<String, List<CredentialDataset>> preAuthorizedCredentials = new HashMap<>();
- for (final String key : preAuthorizedCredentialsInRequest.keySet()) {
- preAuthorizedCredentials.put(key, CredentialDataset.parse(preAuthorizedCredentialsInRequest.get(key)));
+ for (final Map.Entry<?, ?> entry : preAuthorizedCredentialsInRequest.entrySet()) {
+ preAuthorizedCredentials.put((String) entry.getKey(), CredentialDataset.parse(entry.getValue()));
}
CredentialOfferTxCode credentialOfferTxCode = null;
diff --git a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestedClaim.java b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestedClaim.java
index 2611ef2..2f3393a 100644
--- a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestedClaim.java
+++ b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestedClaim.java
@@ -21,6 +21,7 @@ import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.nimbusds.oauth2.sdk.ParseException;
@@ -101,11 +102,14 @@ public final class CredentialOfferRequestedClaim {
* @return CredentialOfferRequestedClaim instance
* @throws ParseException if something unexpected occurs
*/
- public static CredentialOfferRequestedClaim parse(@Nonnull final Object object) throws ParseException {
+ public static CredentialOfferRequestedClaim parse(@Nullable final Object object) throws ParseException {
List<String> parsedPath = null;
if (object instanceof Map<?, ?> requestedClaim) {
if (requestedClaim.get("path") instanceof List<?> path2) {
- parsedPath = path2.stream().map(element -> (String) element).collect(Collectors.toList());
+ if (!path2.stream().allMatch(String.class::isInstance)) {
+ throw new ParseException("Invalid credential offer requested claim: path elements must be strings");
+ }
+ parsedPath = path2.stream().map(String.class::cast).collect(Collectors.toList());
}
return new CredentialOfferRequestedClaim(parsedPath, requestedClaim.get("value"));
}
diff --git a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestedCredential.java b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestedCredential.java
index 7251e5f..a78d9a2 100644
--- a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestedCredential.java
+++ b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestedCredential.java
@@ -122,7 +122,7 @@ public class CredentialOfferRequestedCredential {
}
/**
- * Collect the claims of a claims object, a nested object naming the path of the claims below it.
+ * Collect the claims of a claims object, its nested objects giving the path of the claims below them.
*
* @param claims claims object to read
* @param path path of the claims object
diff --git a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferTxCode.java b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferTxCode.java
index bd4f117..09cd660 100644
--- a/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferTxCode.java
+++ b/openid-vci-impl/src/main/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferTxCode.java
@@ -31,51 +31,48 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.nimbusds.oauth2.sdk.ParseException;
/**
- * tx code in {@link CredentialOfferRequest}.
+ * Transaction Code in {@link CredentialOfferRequest}.
*/
public class CredentialOfferTxCode {
- /**
- * String specifying the input character set. Possible values are numeric (only
- * digits) and text (any characters). The default is numeric.
- */
+ /** Input mode of digits only. */
+ @Nonnull
+ public static final String INPUT_MODE_NUMERIC = "numeric";
+
+ /** Input mode of any characters. */
+ @Nonnull
+ public static final String INPUT_MODE_TEXT = "text";
+
+ /** Maximum length of the description in characters. */
+ public static final int MAX_DESCRIPTION_LENGTH = 300;
+
+ /** Input character set, numeric or text. */
@Nullable
@JsonProperty("input_mode")
@JsonInclude(Include.NON_NULL)
private final String inputMode;
- /**
- * Integer specifying the length of the Transaction Code. This helps the Wallet
- * to render the input screen and improve the user experience.
- */
+ /** Length of the Transaction Code. */
@Nullable
@JsonInclude(Include.NON_NULL)
private final Integer length;
- /**
- * String containing guidance for the Holder of the Wallet on how to obtain the
- * Transaction Code.
- */
+ /** Guidance for the Holder on how to obtain the Transaction Code. */
@Nullable
@JsonInclude(Include.NON_NULL)
private final String description;
- /** Value of tx code. */
+ /** Value of the Transaction Code. */
@Nonnull
private final String value;
/**
* Constructor.
*
- * @param inputMode String specifying the input character set. Possible values
- * are numeric (only digits) and text (any characters). The
- * default is numeric
- * @param length Integer specifying the length of the Transaction Code.
- * This helps the Wallet to render the input screen and
- * improve the user experience
- * @param description String containing guidance for the Holder of the Wallet on
- * how to obtain the Transaction Code
- * @param value Value of tx code
+ * @param inputMode input character set, numeric or text
+ * @param length length of the Transaction Code
+ * @param description guidance for the Holder on how to obtain the Transaction Code
+ * @param value value of the Transaction Code
*/
@JsonCreator
public CredentialOfferTxCode(@JsonProperty("input_mode") @Nullable final String inputMode,
@@ -90,12 +87,9 @@ public class CredentialOfferTxCode {
}
/**
- * Get string specifying the input character set. Possible values are numeric
- * (only digits) and text (any characters). The default is numeric.
+ * Get input character set.
*
- * @return String specifying the input character set. Possible values are
- * numeric (only digits) and text (any characters). The default is
- * numeric.
+ * @return input character set, numeric or text
*/
@Nullable
public String getInputMode() {
@@ -103,32 +97,31 @@ public class CredentialOfferTxCode {
}
/**
- * Get integer specifying the length of the Transaction Code. This helps the
- * Wallet to render the input screen and improve the user experience.
+ * Get length of the Transaction Code.
*
- * @return Integer specifying the length of the Transaction Code. This helps the
- * Wallet to render the input screen and improve the user experience.
+ * @return length of the Transaction Code
*/
+ @Nullable
public Integer getLength() {
return length;
}
/**
- * Get string containing guidance for the Holder of the Wallet on how to obtain
- * the Transaction Code.
+ * Get guidance for the Holder on how to obtain the Transaction Code.
*
- * @return String containing guidance for the Holder of the Wallet on how to
- * obtain the Transaction Code
+ * @return guidance for the Holder
*/
+ @Nullable
public String getDescription() {
return description;
}
/**
- * Get value of tx code.
+ * Get value of the Transaction Code.
*
- * @return value of tx code
+ * @return value of the Transaction Code
*/
+ @Nonnull
public String getValue() {
return value;
}
@@ -168,22 +161,51 @@ public class CredentialOfferTxCode {
}
+// Checkstyle: CyclomaticComplexity OFF
/**
* Parse CredentialOfferTxCode instance from Map.
*
* @param object CredentialOfferTxCode as Map
* @return CredentialOfferTxCode instance
- * @throws ParseException if something unexpected occurs
+ * @throws ParseException if the Transaction Code or its hints are invalid
*/
- public static CredentialOfferTxCode parse(@Nonnull final Object object) throws ParseException {
- if (object instanceof Map<?, ?> txCode) {
- final String txInputMode = txCode.get("input_mode") instanceof String mode ? mode : null;
- final Integer txLength = txCode.get("length") instanceof Integer len ? len : null;
- final String txDescription = txCode.get("description") instanceof String desc ? desc : null;
- final String txValue = txCode.get("value") instanceof String val ? val : null;
- return new CredentialOfferTxCode(txInputMode, txLength, txDescription, txValue);
+ public static CredentialOfferTxCode parse(@Nullable final Object object) throws ParseException {
+ if (!(object instanceof Map<?, ?> txCode)) {
+ throw new ParseException("Invalid credential offer tx code: not an object");
+ }
+ if (!(txCode.get("value") instanceof String txValue) || txValue.isEmpty()) {
+ throw new ParseException("Invalid credential offer tx code: value must be a non-empty string");
+ }
+
+ final Object mode = txCode.get("input_mode");
+ if (mode != null && !INPUT_MODE_NUMERIC.equals(mode) && !INPUT_MODE_TEXT.equals(mode)) {
+ throw new ParseException("Invalid credential offer tx code: input_mode must be numeric or text");
+ }
+ final String txInputMode = (String) mode;
+
+ final Object len = txCode.get("length");
+ if (len != null && (!(len instanceof Integer || len instanceof Long) || ((Number) len).longValue() < 1
+ || ((Number) len).longValue() > Integer.MAX_VALUE)) {
+ throw new ParseException("Invalid credential offer tx code: length must be a positive integer");
+ }
+ final Integer txLength = len == null ? null : ((Number) len).intValue();
+
+ final Object desc = txCode.get("description");
+ if (desc != null && (!(desc instanceof String text)
+ || text.codePointCount(0, text.length()) > MAX_DESCRIPTION_LENGTH)) {
+ throw new ParseException("Invalid credential offer tx code: description must be a string of at most "
+ + MAX_DESCRIPTION_LENGTH + " characters");
+ }
+ final String txDescription = (String) desc;
+
+ if (!INPUT_MODE_TEXT.equals(txInputMode) && !txValue.chars().allMatch(c -> c >= '0' && c <= '9')) {
+ throw new ParseException("Invalid credential offer tx code: value of numeric input_mode must be digits");
+ }
+ if (txLength != null && txValue.codePointCount(0, txValue.length()) != txLength) {
+ throw new ParseException("Invalid credential offer tx code: value does not have the given length");
}
- throw new ParseException("Invalid credential offer tx code: Parsing failed");
+ return new CredentialOfferTxCode(txInputMode, txLength, txDescription, txValue);
}
+// Checkstyle: CyclomaticComplexity ON
}
diff --git a/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestTest.java b/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestTest.java
index 9ee6c04..c4bb536 100644
--- a/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestTest.java
+++ b/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferRequestTest.java
@@ -103,4 +103,42 @@ public class CredentialOfferRequestTest {
request = CredentialOfferRequest.parse(httpRequest);
}
-}
\ No newline at end of file
+ @Test(expectedExceptions = ParseException.class)
+ public void testNullBodyFailure() throws ParseException {
+ httpRequest.setBody("null");
+ CredentialOfferRequest.parse(httpRequest);
+ }
+
+ @Test(expectedExceptions = ParseException.class)
+ public void testCredentialsArrayFailure() throws ParseException {
+ httpRequest.setBody("{\"preAuthorizedCredentials\":[{\"path\":[\"mail\"],\"value\":\"a at example.org\"}]}");
+ CredentialOfferRequest.parse(httpRequest);
+ }
+
+ @Test(expectedExceptions = ParseException.class)
+ public void testCredentialsNullFailure() throws ParseException {
+ httpRequest.setBody("{\"preAuthorizedCredentials\":null}");
+ CredentialOfferRequest.parse(httpRequest);
+ }
+
+ @Test(expectedExceptions = ParseException.class)
+ public void testCredentialsEmptyFailure() throws ParseException {
+ httpRequest.setBody("{\"preAuthorizedCredentials\":{}}");
+ CredentialOfferRequest.parse(httpRequest);
+ }
+
+ @Test(expectedExceptions = ParseException.class)
+ public void testNumericPathElementFailure() throws ParseException {
+ httpRequest.setBody(
+ "{\"preAuthorizedCredentials\":{\"GeantIncubatorSdJwt2\":[{\"path\":[\"degrees\",0],\"value\":\"x\"}]}}");
+ CredentialOfferRequest.parse(httpRequest);
+ }
+
+ @Test(expectedExceptions = ParseException.class)
+ public void testTxCodeWithoutValueFailure() throws ParseException {
+ httpRequest.setBody("{\"txCode\":{\"length\":5},\"preAuthorizedCredentials\":{\"GeantIncubatorSdJwt2\":"
+ + "[{\"path\":[\"mail\"],\"value\":\"a at example.org\"}]}}");
+ CredentialOfferRequest.parse(httpRequest);
+ }
+
+}
diff --git a/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferTxCodeTest.java b/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferTxCodeTest.java
index f05ef9b..fe6ba11 100644
--- a/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferTxCodeTest.java
+++ b/openid-vci-impl/src/test/java/org/geant/shibboleth/plugin/openidvci/messaging/impl/CredentialOfferTxCodeTest.java
@@ -16,11 +16,15 @@
package org.geant.shibboleth.plugin.openidvci.messaging.impl;
+import java.util.Map;
+
import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.exc.ValueInstantiationException;
+import com.nimbusds.oauth2.sdk.ParseException;
import org.testng.Assert;
@@ -61,4 +65,71 @@ public class CredentialOfferTxCodeTest {
}
-}
\ No newline at end of file
+ @Test
+ public void testParseObject() throws ParseException {
+ txCode = CredentialOfferTxCode.parse((Object) Map.of("value", "123456", "input_mode", "numeric", "length", 6,
+ "description", "Code"));
+ Assert.assertEquals(txCode.getInputMode(), "numeric");
+ Assert.assertEquals(txCode.getLength(), 6);
+ Assert.assertEquals(txCode.getDescription(), "Code");
+ Assert.assertEquals(txCode.getValue(), "123456");
+ Assert.assertEquals(txCode.toResponseMap(), Map.of("input_mode", "numeric", "length", 6, "description", "Code"));
+ }
+
+ @Test
+ public void testParseObjectOnlyValue() throws ParseException {
+ txCode = CredentialOfferTxCode.parse((Object) Map.of("value", "12345"));
+ Assert.assertNull(txCode.getInputMode());
+ Assert.assertNull(txCode.getLength());
+ Assert.assertTrue(txCode.toResponseMap().isEmpty());
+ }
+
+ @Test
+ public void testParseObjectText() throws ParseException {
+ txCode = CredentialOfferTxCode.parse((Object) Map.of("value", "ab1C", "input_mode", "text", "length", 4));
+ Assert.assertEquals(txCode.getValue(), "ab1C");
+ }
+
+ @Test
+ public void testParseObjectLongLength() throws ParseException {
+ txCode = CredentialOfferTxCode.parse((Object) Map.of("value", "12345", "length", 5L));
+ Assert.assertEquals(txCode.getLength(), 5);
+ }
+
+ @Test
+ public void testParseObjectDescriptionAtLimit() throws ParseException {
+ txCode = CredentialOfferTxCode.parse((Object) Map.of("value", "1", "description", "d".repeat(300)));
+ Assert.assertEquals(txCode.getDescription().length(), 300);
+ }
+
+ @DataProvider
+ public Object[][] invalid() {
+ return new Object[][] {
+ { "not an object" },
+ { Map.of("length", 5) },
+ { Map.of("value", "") },
+ { Map.of("value", 12345) },
+ { Map.of("value", "12345", "input_mode", "alphanumeric") },
+ { Map.of("value", "12345", "input_mode", 1) },
+ { Map.of("value", "12345", "length", "5") },
+ { Map.of("value", "12345", "length", 5.0) },
+ { Map.of("value", "12345", "length", 0) },
+ { Map.of("value", "12345", "length", 6) },
+ { Map.of("value", "1", "description", "d".repeat(301)) },
+ { Map.of("value", "1", "description", 1) },
+ { Map.of("value", "12a45") },
+ { Map.of("value", "12a45", "input_mode", "numeric") },
+ };
+ }
+
+ @Test(dataProvider = "invalid", expectedExceptions = ParseException.class)
+ public void testParseObjectInvalid(final Object object) throws ParseException {
+ CredentialOfferTxCode.parse(object);
+ }
+
+ @Test(expectedExceptions = ParseException.class)
+ public void testParseObjectNull() throws ParseException {
+ CredentialOfferTxCode.parse((Object) null);
+ }
+
+}
--
To stop receiving notification emails like this one, please contact
the administrator of this repository.
More information about the commits
mailing list