Skip to content

Commit

Permalink
Added error handling if resumption token is not a valid base64 string
Browse files Browse the repository at this point in the history
  • Loading branch information
bumann-sbb committed Oct 1, 2024
1 parent 1ad5614 commit 8d1545d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,16 @@ public String format(ResumptionToken.Value resumptionToken) {
* @param value The Base64 encoded string
* @return A decoded String (may be empty)
*/
static String base64Decode(String value) {
static String base64Decode(String value) throws BadResumptionTokenException {
if (value == null) {
return null;
}
byte[] decodedValue = Base64.getDecoder().decode(value);
return new String(decodedValue, StandardCharsets.UTF_8);
try {
byte[] decodedValue = Base64.getDecoder().decode(value);
return new String(decodedValue, StandardCharsets.UTF_8);
} catch (IllegalArgumentException e) {
throw new BadResumptionTokenException("Token has no valid base64 encoding", e);
}
}

static String base64Encode(String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
Expand Down Expand Up @@ -73,4 +74,16 @@ void validParse(String token) {
String encoded = SimpleResumptionTokenFormat.base64Encode(token);
assertDoesNotThrow(() -> format.parse(encoded));
}

@Test
void validBase64Decoding() {
assertDoesNotThrow(() -> SimpleResumptionTokenFormat.base64Decode("b2Zmc2V0OjoxMDA="));
}

@Test
void invalidBase64Decoding() {
assertThrows(
BadResumptionTokenException.class,
() -> SimpleResumptionTokenFormat.base64Decode("b2Zmc2V0OjoMDA="));
}
}

0 comments on commit 8d1545d

Please sign in to comment.