diff --git a/core/src/main/java/org/keycloak/sdjwt/vp/SdJwtVP.java b/core/src/main/java/org/keycloak/sdjwt/vp/SdJwtVP.java index 7c693a7f35..7b663ef02d 100644 --- a/core/src/main/java/org/keycloak/sdjwt/vp/SdJwtVP.java +++ b/core/src/main/java/org/keycloak/sdjwt/vp/SdJwtVP.java @@ -104,8 +104,16 @@ public class SdJwtVP { int disclosureStart = sdJwtString.indexOf(SdJwt.DELIMITER); int disclosureEnd = sdJwtString.lastIndexOf(SdJwt.DELIMITER); + if (disclosureStart == -1) { + throw new IllegalArgumentException("SD-JWT is malformed, expected to contain a '" + SdJwt.DELIMITER + "'"); + } + String issuerSignedJWTString = sdJwtString.substring(0, disclosureStart); - String disclosuresString = sdJwtString.substring(disclosureStart + 1, disclosureEnd); + String disclosuresString = ""; + + if (disclosureEnd > disclosureStart) { + disclosuresString = sdJwtString.substring(disclosureStart + 1, disclosureEnd); + } IssuerSignedJWT issuerSignedJWT = IssuerSignedJWT.fromJws(issuerSignedJWTString); diff --git a/core/src/test/java/org/keycloak/sdjwt/sdjwtvp/SdJwtVPTest.java b/core/src/test/java/org/keycloak/sdjwt/sdjwtvp/SdJwtVPTest.java index f787076b29..2ae3488431 100644 --- a/core/src/test/java/org/keycloak/sdjwt/sdjwtvp/SdJwtVPTest.java +++ b/core/src/test/java/org/keycloak/sdjwt/sdjwtvp/SdJwtVPTest.java @@ -32,6 +32,8 @@ import org.keycloak.sdjwt.vp.SdJwtVP; import java.util.Arrays; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; /** @@ -196,4 +198,24 @@ public abstract class SdJwtVPTest { .verifySignature(TestSettings.verifierContextFrom(presenteSdJwtVP.getCnfClaim(), "ES256")); } + + @Test + public void testOf_validInput() { + String sdJwtString = TestUtils.readFileAsString(getClass(), "sdjwt/s6.2-presented-sdjwtvp.txt"); + SdJwtVP sdJwtVP = SdJwtVP.of(sdJwtString); + + assertNotNull(sdJwtVP); + assertEquals(4, sdJwtVP.getDisclosures().size()); + } + + @Test + public void testOf_MalformedSdJwt_ThrowsIllegalArgumentException() { + // Given + String malformedSdJwt = "issuer-signed-jwt"; + + // When & Then + IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> SdJwtVP.of(malformedSdJwt)); + assertEquals("SD-JWT is malformed, expected to contain a '~'", exception.getMessage()); + } + }