Skip to content

Commit

Permalink
MINOR: Adjust logging in SerializedJwt (#18523)
Browse files Browse the repository at this point in the history
Reviewers: Luke Chen <[email protected]>, Chia-Ping Tsai <[email protected]>
  • Loading branch information
mimaison committed Jan 16, 2025
1 parent 5b5c4f0 commit d04c48d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,17 @@ public SerializedJwt(String token) {
token = token.trim();

if (token.isEmpty())
throw new ValidateException("Empty JWT provided; expected three sections (header, payload, and signature)");
throw new ValidateException("Malformed JWT provided; expected three sections (header, payload, and signature)");

String[] splits = token.split("\\.");

if (splits.length != 3)
throw new ValidateException(String.format("Malformed JWT provided (%s); expected three sections (header, payload, and signature), but %d sections provided",
token, splits.length));
throw new ValidateException("Malformed JWT provided; expected three sections (header, payload, and signature)");

this.token = token.trim();
this.header = validateSection(splits[0], "header");
this.payload = validateSection(splits[1], "payload");
this.signature = validateSection(splits[2], "signature");
this.header = validateSection(splits[0]);
this.payload = validateSection(splits[1]);
this.signature = validateSection(splits[2]);
}

/**
Expand Down Expand Up @@ -93,13 +92,11 @@ public String getSignature() {
return signature;
}

private String validateSection(String section, String sectionName) throws ValidateException {
private String validateSection(String section) throws ValidateException {
section = section.trim();

if (section.isEmpty())
throw new ValidateException(String.format(
"Malformed JWT provided; expected at least three sections (header, payload, and signature), but %s section missing",
sectionName));
throw new ValidateException("Malformed JWT provided; expected three sections (header, payload, and signature)");

return section;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,25 +38,25 @@ protected AccessTokenValidator createAccessTokenValidator() throws Exception {
@Test
public void testNull() throws Exception {
AccessTokenValidator validator = createAccessTokenValidator();
assertThrowsWithMessage(ValidateException.class, () -> validator.validate(null), "Empty JWT provided");
assertThrowsWithMessage(ValidateException.class, () -> validator.validate(null), "Malformed JWT provided; expected three sections (header, payload, and signature)");
}

@Test
public void testEmptyString() throws Exception {
AccessTokenValidator validator = createAccessTokenValidator();
assertThrowsWithMessage(ValidateException.class, () -> validator.validate(""), "Empty JWT provided");
assertThrowsWithMessage(ValidateException.class, () -> validator.validate(""), "Malformed JWT provided; expected three sections (header, payload, and signature)");
}

@Test
public void testWhitespace() throws Exception {
AccessTokenValidator validator = createAccessTokenValidator();
assertThrowsWithMessage(ValidateException.class, () -> validator.validate(" "), "Empty JWT provided");
assertThrowsWithMessage(ValidateException.class, () -> validator.validate(" "), "Malformed JWT provided; expected three sections (header, payload, and signature)");
}

@Test
public void testEmptySections() throws Exception {
AccessTokenValidator validator = createAccessTokenValidator();
assertThrowsWithMessage(ValidateException.class, () -> validator.validate(".."), "Malformed JWT provided");
assertThrowsWithMessage(ValidateException.class, () -> validator.validate(".."), "Malformed JWT provided; expected three sections (header, payload, and signature)");
}

@Test
Expand Down

0 comments on commit d04c48d

Please sign in to comment.