mirror of https://github.com/jwtk/jjwt.git
Added expectSubject convenience method.
This commit is contained in:
parent
fd04a357cb
commit
f3c8f10f32
|
@ -27,6 +27,14 @@ public interface JwtParser {
|
|||
|
||||
public static final char SEPARATOR_CHAR = '.';
|
||||
|
||||
/**
|
||||
* Sets an expected value for the subject claim.
|
||||
*
|
||||
* @param subject
|
||||
* @return the parser for method chaining.
|
||||
*/
|
||||
JwtParser expectSubject(String subject);
|
||||
|
||||
/**
|
||||
* Sets an expected value for the audience claim.
|
||||
*
|
||||
|
|
|
@ -87,6 +87,13 @@ public class DefaultJwtParser implements JwtParser {
|
|||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JwtParser expectSubject(String subject) {
|
||||
expect(Claims.SUBJECT, subject);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JwtParser expect(String claimName, Object value) {
|
||||
if (claimName != null && claimName.length() > 0 && value != null) {
|
||||
|
|
|
@ -1046,4 +1046,67 @@ class JwtParserTest {
|
|||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseExpectSubject_Success() {
|
||||
def subject = 'A Most Awesome Subject'
|
||||
|
||||
byte[] key = randomKey()
|
||||
|
||||
String compact = Jwts.builder().signWith(SignatureAlgorithm.HS256, key).
|
||||
setSubject(subject).
|
||||
compact()
|
||||
|
||||
Jwt<Header,Claims> jwt = Jwts.parser().setSigningKey(key).
|
||||
expectSubject(subject).
|
||||
parseClaimsJws(compact)
|
||||
|
||||
assertEquals jwt.getBody().getSubject(), subject
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseExpectSubject_Incorrect_Fail() {
|
||||
def goodSubject = 'A Most Awesome Subject'
|
||||
def badSubject = 'A Most Bogus Subject'
|
||||
|
||||
byte[] key = randomKey()
|
||||
|
||||
String compact = Jwts.builder().signWith(SignatureAlgorithm.HS256, key).
|
||||
setSubject(badSubject).
|
||||
compact()
|
||||
|
||||
try {
|
||||
Jwts.parser().setSigningKey(key).
|
||||
expectSubject(goodSubject).
|
||||
parseClaimsJws(compact)
|
||||
fail()
|
||||
} catch(IncorrectClaimException e) {
|
||||
assertEquals(
|
||||
String.format(INCORRECT_EXPECTED_CLAIM_MESSAGE_TEMPLATE, Claims.SUBJECT, goodSubject, badSubject),
|
||||
e.getMessage()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void testParseExpectSubject_Missing_Fail() {
|
||||
def subject = 'A Most Awesome Subject'
|
||||
|
||||
byte[] key = randomKey()
|
||||
|
||||
String compact = Jwts.builder().signWith(SignatureAlgorithm.HS256, key).
|
||||
setId('id').
|
||||
compact()
|
||||
|
||||
try {
|
||||
Jwts.parser().setSigningKey(key).
|
||||
expectSubject(subject).
|
||||
parseClaimsJws(compact)
|
||||
fail()
|
||||
} catch(MissingClaimException e) {
|
||||
assertEquals(
|
||||
String.format(MISSING_EXPECTED_CLAIM_MESSAGE_TEMPLATE, Claims.SUBJECT, subject),
|
||||
e.getMessage()
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue