Skip to content

Commit

Permalink
Preserve order when message has multiple JWTs.
Browse files Browse the repository at this point in the history
  • Loading branch information
DolphFlynn committed Jul 28, 2024
1 parent e20571d commit a9ce798
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static Optional<JOSEObject> parseJOSEObject(String text) {

private static Set<String> findCandidateJoseObjectsWithin(String text) {
Matcher m = JOSE_OBJECT_PATTERN.matcher(text);
Set<String> strings = new HashSet<>();
Set<String> strings = new LinkedHashSet<>();

while (m.find()) {
String token = m.group();
Expand Down
16 changes: 14 additions & 2 deletions src/test/java/com/blackberry/jwteditor/JWEDetectionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.blackberry.jwteditor.model.jose.JOSEObject;
import com.blackberry.jwteditor.model.jose.JWE;
import com.blackberry.jwteditor.model.jose.MutableJOSEObject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand Down Expand Up @@ -63,7 +64,7 @@ void testExtractValidJWE(String joseObjectString) {
List<MutableJOSEObject> joseObjects = extractJOSEObjects(joseObjectString);

assertThat(joseObjects).hasSize(1);
assertThat(joseObjects.get(0).getModified()).isInstanceOf(JWE.class);
assertThat(joseObjects.getFirst().getModified()).isInstanceOf(JWE.class);
}

@ParameterizedTest
Expand All @@ -74,7 +75,7 @@ void testExtractValidJWEFromWithinData(String joseObjectString) {
List<MutableJOSEObject> joseObjects = extractJOSEObjects(text);

assertThat(joseObjects).hasSize(1);
assertThat(joseObjects.get(0).getModified()).isInstanceOf(JWE.class);
assertThat(joseObjects.getFirst().getModified()).isInstanceOf(JWE.class);
}

@ParameterizedTest
Expand Down Expand Up @@ -131,4 +132,15 @@ void testParseInvalidJWE(String joseObjectString) {

assertThat(joseObject).isEmpty();
}

@Test
void testParseMultipleJWE() {
String jwe1 = "eyJlbmMiOiJBMTI4R0NNIiwiYWxnIjoiZGlyIn0..FofRkmAUlKShyhYp.1AjXmQsKwV36LSxZ5YJq7xPPTTUS_e9FyLbd-CWdX72ESWMttHm2xGDWUl-Sp9grmcINWLNwsKezYnJVncfir2o9Uq9vcXENIypU2Qwmymn5q5gJwkR4Wx_RLae9Zm8xP76LJFQe8FssUVHx65Zzvd1I6GbV6FjfbkLF1Z_Ka-olubtWCilFDjIVN7WRUAxmV8syJaM.P0XjuL8_8nK50paY09mB6g";
String jwe2 = "eyJlbmMiOiJBMTI4R0NNIiwiYWxnIjoiQTEyOEtXIn0.H3X6mT5HLgcFfzLoe4ku6Knhh9Ofv1eL.qF5-N_7K8VQ4yMSz.WXUNY6eg5fR4tc8Hqf5XDRM9ALGwcQyYG4IYwwg8Ctkx1UuxoV7t6UnemjzCj2sOYUqi3KYpDzrKVJpzokz0vcIem4lFe5N_ds8FAMpW0GSF9ePA8qvV99WaP0N2ECVPmgihvL6qwNhdptlLKtxcOpE41U5LnU22voPK55VF4_1j0WmTgWgZ7DwLDysp6EIDjrrt-DY.febBmP71KADmKRVfeSnv_g";
String data = jwe1 + " " + jwe2;

List<MutableJOSEObject> joseObjects = extractJOSEObjects(data);

assertThat(joseObjects).extracting("original").containsExactly(jwe1, jwe2);
}
}
19 changes: 16 additions & 3 deletions src/test/java/com/blackberry/jwteditor/JWSDetectionTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.blackberry.jwteditor.model.jose.JOSEObject;
import com.blackberry.jwteditor.model.jose.JWS;
import com.blackberry.jwteditor.model.jose.MutableJOSEObject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

Expand Down Expand Up @@ -88,7 +89,7 @@ void testExtractValidJWS(String joseObjectString) {
List<MutableJOSEObject> joseObjects = extractJOSEObjects(joseObjectString);

assertThat(joseObjects).hasSize(1);
assertThat(joseObjects.get(0).getModified()).isInstanceOf(JWS.class);
assertThat(joseObjects.getFirst().getModified()).isInstanceOf(JWS.class);
}

@ParameterizedTest
Expand All @@ -98,7 +99,7 @@ void testExtractValidJWSWithWhitespace(String joseObjectString) {
List<MutableJOSEObject> joseObjects = extractJOSEObjects(text);

assertThat(joseObjects).hasSize(1);
assertThat(joseObjects.get(0).getModified()).isInstanceOf(JWS.class);
assertThat(joseObjects.getFirst().getModified()).isInstanceOf(JWS.class);
}

@ParameterizedTest
Expand All @@ -108,7 +109,7 @@ void testExtractValidJWSFromWithinData(String joseObjectString) {
List<MutableJOSEObject> joseObjects = extractJOSEObjects(text);

assertThat(joseObjects).hasSize(1);
assertThat(joseObjects.get(0).getModified()).isInstanceOf(JWS.class);
assertThat(joseObjects.getFirst().getModified()).isInstanceOf(JWS.class);
}

@ParameterizedTest
Expand Down Expand Up @@ -172,4 +173,16 @@ void testParseInvalidJWS(String joseObjectString) {

assertThat(joseObject).isEmpty();
}

@Test
void testParseMultipleJWS() {
String jws1 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJUZXN0In0.Nabf3xakZubPnCzHT-fx0vG1iuNPeJKuSzHxUiQKf-8";
String jws2 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJUZXN0In0.";
String jws3 = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.17DlZn0zeYhz3uTQCRpSx9hYlUj1SJxDMeZLof8dSHw";
String data = jws1 + " " + jws2 + " " + jws3;

List<MutableJOSEObject> joseObjects = extractJOSEObjects(data);

assertThat(joseObjects).extracting("original").containsExactly(jws1, jws2, jws3);
}
}

0 comments on commit a9ce798

Please sign in to comment.