|
| 1 | +package com.auth0.jwtdecodejava.impl; |
| 2 | + |
| 3 | +import com.auth0.jwtdecodejava.exceptions.InvalidClaimException; |
| 4 | +import com.auth0.jwtdecodejava.interfaces.JWT; |
| 5 | +import org.junit.Rule; |
| 6 | +import org.junit.Test; |
| 7 | +import org.junit.rules.ExpectedException; |
| 8 | + |
| 9 | +import java.util.Date; |
| 10 | + |
| 11 | +import static org.hamcrest.Matchers.is; |
| 12 | +import static org.hamcrest.Matchers.notNullValue; |
| 13 | +import static org.junit.Assert.assertThat; |
| 14 | + |
| 15 | +public class JWTVerifierTest { |
| 16 | + |
| 17 | + @Rule |
| 18 | + public ExpectedException exception = ExpectedException.none(); |
| 19 | + |
| 20 | + @Test |
| 21 | + public void shouldValidateIssuer() throws Exception { |
| 22 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 23 | + JWT jwt = JWTVerifier.init() |
| 24 | + .withIssuer("auth0") |
| 25 | + .verify(token); |
| 26 | + |
| 27 | + assertThat(jwt, is(notNullValue())); |
| 28 | + } |
| 29 | + |
| 30 | + @Test |
| 31 | + public void shouldThrowOnInvalidIssuer() throws Exception { |
| 32 | + exception.expect(InvalidClaimException.class); |
| 33 | + exception.expectMessage("The Claim 'iss' value doesn't match the required one."); |
| 34 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJhdXRoMCJ9.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 35 | + JWTVerifier.init() |
| 36 | + .withIssuer("invalid") |
| 37 | + .verify(token); |
| 38 | + } |
| 39 | + |
| 40 | + |
| 41 | + @Test |
| 42 | + public void shouldValidateSubject() throws Exception { |
| 43 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.Rq8IxqeX7eA6GgYxlcHdPFVRNFFZc5rEI3MQTZZbK3I"; |
| 44 | + JWT jwt = JWTVerifier.init() |
| 45 | + .withSubject("1234567890") |
| 46 | + .verify(token); |
| 47 | + |
| 48 | + assertThat(jwt, is(notNullValue())); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void shouldThrowOnInvalidSubject() throws Exception { |
| 53 | + exception.expect(InvalidClaimException.class); |
| 54 | + exception.expectMessage("The Claim 'sub' value doesn't match the required one."); |
| 55 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.Rq8IxqeX7eA6GgYxlcHdPFVRNFFZc5rEI3MQTZZbK3I"; |
| 56 | + JWTVerifier.init() |
| 57 | + .withSubject("invalid") |
| 58 | + .verify(token); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void shouldValidateAudience() throws Exception { |
| 63 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJNYXJrIn0.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 64 | + JWT jwt = JWTVerifier.init() |
| 65 | + .withAudience(new String[]{"Mark"}) |
| 66 | + .verify(token); |
| 67 | + |
| 68 | + assertThat(jwt, is(notNullValue())); |
| 69 | + |
| 70 | + String tokenArr = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOlsiTWFyayIsIkRhdmlkIl19.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 71 | + JWT jwtArr = JWTVerifier.init() |
| 72 | + .withAudience(new String[]{"Mark", "David"}) |
| 73 | + .verify(tokenArr); |
| 74 | + |
| 75 | + assertThat(jwtArr, is(notNullValue())); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void shouldThrowOnInvalidAudience() throws Exception { |
| 80 | + exception.expect(InvalidClaimException.class); |
| 81 | + exception.expectMessage("The Claim 'aud' value doesn't match the required one."); |
| 82 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.Rq8IxqeX7eA6GgYxlcHdPFVRNFFZc5rEI3MQTZZbK3I"; |
| 83 | + JWTVerifier.init() |
| 84 | + .withAudience(new String[]{"nope"}) |
| 85 | + .verify(token); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void shouldValidateExpiresAt() throws Exception { |
| 90 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0Nzc1OTJ9.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 91 | + JWT jwt = JWTVerifier.init() |
| 92 | + .withExpiresAt(new Date(1477592000)) |
| 93 | + .verify(token); |
| 94 | + |
| 95 | + assertThat(jwt, is(notNullValue())); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + public void shouldThrowOnInvalidExpiresAt() throws Exception { |
| 100 | + exception.expect(InvalidClaimException.class); |
| 101 | + exception.expectMessage("The Claim 'exp' value doesn't match the required one."); |
| 102 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE0Nzc1OTJ9.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 103 | + JWTVerifier.init() |
| 104 | + .withExpiresAt(new Date()) |
| 105 | + .verify(token); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void shouldValidateNotBefore() throws Exception { |
| 110 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0Nzc1OTJ9.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 111 | + JWT jwt = JWTVerifier.init() |
| 112 | + .withNotBefore(new Date(1477592000)) |
| 113 | + .verify(token); |
| 114 | + |
| 115 | + assertThat(jwt, is(notNullValue())); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + public void shouldThrowOnInvalidNotBefore() throws Exception { |
| 120 | + exception.expect(InvalidClaimException.class); |
| 121 | + exception.expectMessage("The Claim 'nbf' value doesn't match the required one."); |
| 122 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYmYiOjE0Nzc1OTJ9.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 123 | + JWTVerifier.init() |
| 124 | + .withNotBefore(new Date()) |
| 125 | + .verify(token); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void shouldValidateIssuedAt() throws Exception { |
| 130 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0Nzc1OTJ9.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 131 | + JWT jwt = JWTVerifier.init() |
| 132 | + .withIssuedAt(new Date(1477592000)) |
| 133 | + .verify(token); |
| 134 | + |
| 135 | + assertThat(jwt, is(notNullValue())); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void shouldThrowOnInvalidIssuedAt() throws Exception { |
| 140 | + exception.expect(InvalidClaimException.class); |
| 141 | + exception.expectMessage("The Claim 'iat' value doesn't match the required one."); |
| 142 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE0Nzc1OTJ9.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 143 | + JWTVerifier.init() |
| 144 | + .withIssuedAt(new Date()) |
| 145 | + .verify(token); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void shouldValidateJWTId() throws Exception { |
| 150 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJqd3RfaWRfMTIzIn0.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 151 | + JWT jwt = JWTVerifier.init() |
| 152 | + .withJWTId("jwt_id_123") |
| 153 | + .verify(token); |
| 154 | + |
| 155 | + assertThat(jwt, is(notNullValue())); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void shouldThrowOnInvalidJWTId() throws Exception { |
| 160 | + exception.expect(InvalidClaimException.class); |
| 161 | + exception.expectMessage("The Claim 'jti' value doesn't match the required one."); |
| 162 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJqdGkiOiJqd3RfaWRfMTIzIn0.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 163 | + JWTVerifier.init() |
| 164 | + .withJWTId("invalid") |
| 165 | + .verify(token); |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + public void shouldSkipClaimValidationsIfNoClaimsRequired() throws Exception { |
| 170 | + String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.e30.MT8JrEvIB69bH5W9RUR2ap-H3e69fM7LEQCiZF-7FbI"; |
| 171 | + JWT jwt = JWTVerifier.init() |
| 172 | + .verify(token); |
| 173 | + |
| 174 | + assertThat(jwt, is(notNullValue())); |
| 175 | + } |
| 176 | +} |
0 commit comments