1010import java .security .InvalidKeyException ;
1111import java .security .NoSuchAlgorithmException ;
1212import java .security .SignatureException ;
13+ import java .util .ArrayList ;
1314import java .util .Arrays ;
1415import java .util .HashMap ;
1516import java .util .Map ;
@@ -62,11 +63,12 @@ public JWTVerifier(String secret) {
6263 *
6364 * @param token token to verify
6465 * @throws SignatureException when signature is invalid
65- * @throws IllegalStateException when token's structure, expiration, issuer or audience are invalid
66+ * @throws JWTVerifyException when expiration, issuer or audience are invalid
67+ * @throws IllegalStateException when token's structure is invalid
6668 */
6769 public Map <String , Object > verify (String token )
6870 throws NoSuchAlgorithmException , InvalidKeyException , IllegalStateException ,
69- IOException , SignatureException {
71+ IOException , SignatureException , JWTVerifyException {
7072 if (token == null || "" .equals (token )) {
7173 throw new IllegalStateException ("token not set" );
7274 }
@@ -107,23 +109,23 @@ void verifySignature(String[] pieces, String algorithm) throws NoSuchAlgorithmEx
107109 }
108110 }
109111
110- void verifyExpiration (JsonNode jwtClaims ) {
112+ void verifyExpiration (JsonNode jwtClaims ) throws JWTExpiredException {
111113 final long expiration = jwtClaims .has ("exp" ) ? jwtClaims .get ("exp" ).asLong (0 ) : 0 ;
112114
113115 if (expiration != 0 && System .currentTimeMillis () / 1000L >= expiration ) {
114- throw new IllegalStateException ("jwt expired" );
116+ throw new JWTExpiredException ("jwt expired" , expiration );
115117 }
116118 }
117119
118- void verifyIssuer (JsonNode jwtClaims ) {
120+ void verifyIssuer (JsonNode jwtClaims ) throws JWTIssuerException {
119121 final String issuerFromToken = jwtClaims .has ("iss" ) ? jwtClaims .get ("iss" ).asText () : null ;
120122
121123 if (issuerFromToken != null && issuer != null && !issuer .equals (issuerFromToken )) {
122- throw new IllegalStateException ("jwt issuer invalid" );
124+ throw new JWTIssuerException ("jwt issuer invalid" , issuerFromToken );
123125 }
124126 }
125127
126- void verifyAudience (JsonNode jwtClaims ) {
128+ void verifyAudience (JsonNode jwtClaims ) throws JWTAudienceException {
127129 if (audience == null )
128130 return ;
129131 JsonNode audNode = jwtClaims .get ("aud" );
@@ -138,7 +140,7 @@ void verifyAudience(JsonNode jwtClaims) {
138140 if (audience .equals (audNode .textValue ()))
139141 return ;
140142 }
141- throw new IllegalStateException ("jwt audience invalid" );
143+ throw new JWTAudienceException ("jwt audience invalid" , audNode );
142144 }
143145
144146 String getAlgorithm (JsonNode jwtHeader ) {
0 commit comments