|
| 1 | +package com.auth0.jwt; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.LinkedList; |
| 8 | + |
| 9 | +import static org.junit.Assert.assertEquals; |
| 10 | + |
| 11 | +public class JWTSignerTest_Bytes { |
| 12 | + private static JWTSigner signer = new JWTSigner(new byte[] { 109, 121, 32, 115, 101, 99, 114, 101, 116}); |
| 13 | + |
| 14 | + @Test |
| 15 | + public void shouldSignEmpty() throws Exception { |
| 16 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 17 | + String token = signer.sign(claims); |
| 18 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.e30.86pkOAQxvnSDd91EThNNpOTbO-hbvxdssnFjQqT04NU", token); |
| 19 | + } |
| 20 | + |
| 21 | + @Test |
| 22 | + public void shouldSignEmptyTwoParams() throws Exception { |
| 23 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 24 | + String token = signer.sign(claims); |
| 25 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.e30.86pkOAQxvnSDd91EThNNpOTbO-hbvxdssnFjQqT04NU", token); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void shouldSignStringOrURI1() throws Exception { |
| 30 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 31 | + claims.put("iss", "foo"); |
| 32 | + String token = signer.sign(claims); |
| 33 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJmb28ifQ.UbvkKJx4ubG9SQYs3Hpe6FJl1ix89jSLw0I9GNTnLgY", token); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void shouldSignStringOrURI2() throws Exception { |
| 38 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 39 | + claims.put("sub", "http://foo"); |
| 40 | + String token = signer.sign(claims); |
| 41 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJodHRwOi8vZm9vIn0.EaYoTXJWUNd_1tWfZo4EZoKUP8hVMJm1LHBQNo4Xfwg", token); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void shouldSignStringOrURI3() throws Exception { |
| 46 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 47 | + claims.put("aud", ""); |
| 48 | + String token = signer.sign(claims); |
| 49 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOiIifQ.T2EKheH_WVVwybctic8Sqk89miYVKADW0AeXOicDbz8", token); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void shouldSignStringOrURICollection() throws Exception { |
| 54 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 55 | + LinkedList<String> aud = new LinkedList<String>(); |
| 56 | + aud.add("xyz"); |
| 57 | + aud.add("ftp://foo"); |
| 58 | + claims.put("aud", aud); |
| 59 | + String token = signer.sign(claims); |
| 60 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdWQiOlsieHl6IiwiZnRwOi8vZm9vIl19.WGpsdOnLJ2k7Rr4WeEuabHO4wNQIhJfPMZot1DrTUgA", token); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + public void shouldSignIntDate1() throws Exception { |
| 65 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 66 | + claims.put("exp", 123); |
| 67 | + String token = signer.sign(claims); |
| 68 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjEyM30.FzAXEHf0LVQPOyRQFftA1VBAj8RmZGEfwQIPSfg_DUg", token); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void bytes_shouldSignIntDate1() throws Exception { |
| 73 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 74 | + claims.put("exp", 123); |
| 75 | + String token = signer.sign(claims); |
| 76 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjEyM30.FzAXEHf0LVQPOyRQFftA1VBAj8RmZGEfwQIPSfg_DUg", token); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void shouldSignIntDate2() throws Exception { |
| 81 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 82 | + claims.put("nbf", 0); |
| 83 | + String token = signer.sign(claims); |
| 84 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYmYiOjB9.ChHEHjtyr4qOUMu6KDsa2BjGXtkGurboD5ljr99gVzw", token); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void shouldSignIntDate3() throws Exception { |
| 89 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 90 | + claims.put("iat", Long.MAX_VALUE); |
| 91 | + String token = signer.sign(claims); |
| 92 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjkyMjMzNzIwMzY4NTQ3NzU4MDd9.7yrsheXoAuqk5hDcbKmT3l6aDNNr7RMnbVe6kVkvv4M", token); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void bytes_shouldSignIntDate2() throws Exception { |
| 97 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 98 | + claims.put("nbf", 0); |
| 99 | + String token = signer.sign(claims); |
| 100 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJuYmYiOjB9.ChHEHjtyr4qOUMu6KDsa2BjGXtkGurboD5ljr99gVzw", token); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void shouldSignString() throws Exception { |
| 105 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 106 | + claims.put("jti", "foo"); |
| 107 | + String token = signer.sign(claims); |
| 108 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJqdGkiOiJmb28ifQ.CriA-W8LKO4bCxy3e2Nu7kx2MxgcHGyFu_GVLMX3bko", token); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void shouldSignNullEqualsMissing() throws Exception { |
| 113 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 114 | + for (String claimName : Arrays.asList("iss", "sub", "aud", "exp", "nbf", "iat", "jti")) { |
| 115 | + claims.put(claimName, null); |
| 116 | + } |
| 117 | + String token = signer.sign(claims); |
| 118 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.e30.86pkOAQxvnSDd91EThNNpOTbO-hbvxdssnFjQqT04NU", token); |
| 119 | + } |
| 120 | + |
| 121 | + @Test(expected = Exception.class) |
| 122 | + public void shouldFailExpectStringOrURI1() throws Exception { |
| 123 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 124 | + claims.put("iss", 0); |
| 125 | + signer.sign(claims); |
| 126 | + } |
| 127 | + |
| 128 | + @Test(expected = Exception.class) |
| 129 | + public void shouldFailExpectStringOrURI2() throws Exception { |
| 130 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 131 | + claims.put("sub", ":"); |
| 132 | + signer.sign(claims); |
| 133 | + } |
| 134 | + |
| 135 | + @Test(expected = Exception.class) |
| 136 | + public void shouldFailExpectStringOrURICollection1() throws Exception { |
| 137 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 138 | + claims.put("aud", 0); |
| 139 | + signer.sign(claims); |
| 140 | + } |
| 141 | + |
| 142 | + @Test(expected = Exception.class) |
| 143 | + public void shouldFailExpectStringOrURICollection2() throws Exception { |
| 144 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 145 | + claims.put("aud", Arrays.asList(0)); |
| 146 | + signer.sign(claims); |
| 147 | + } |
| 148 | + |
| 149 | + @Test(expected = Exception.class) |
| 150 | + public void shouldFailExpectStringOrURICollection3() throws Exception { |
| 151 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 152 | + claims.put("aud", Arrays.asList(":")); |
| 153 | + signer.sign(claims); |
| 154 | + } |
| 155 | + |
| 156 | + @Test(expected = Exception.class) |
| 157 | + public void shouldFailExpectIntDate1() throws Exception { |
| 158 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 159 | + claims.put("exp", -1); |
| 160 | + signer.sign(claims); |
| 161 | + } |
| 162 | + |
| 163 | + @Test(expected = Exception.class) |
| 164 | + public void shouldFailExpectIntDate2() throws Exception { |
| 165 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 166 | + claims.put("nbf", "100"); |
| 167 | + signer.sign(claims); |
| 168 | + } |
| 169 | + |
| 170 | + @Test(expected = Exception.class) |
| 171 | + public void shouldFailExpectString() throws Exception { |
| 172 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 173 | + claims.put("jti", 100); |
| 174 | + signer.sign(claims); |
| 175 | + } |
| 176 | + |
| 177 | + @Test |
| 178 | + public void shouldOptionsNone() throws Exception { |
| 179 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 180 | + String token = signer.sign(claims, new JWTSigner.Options()); |
| 181 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.e30.86pkOAQxvnSDd91EThNNpOTbO-hbvxdssnFjQqT04NU", token); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void shouldOptionsAll() throws Exception { |
| 186 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 187 | + signer.sign(claims, new JWTSigner.Options() |
| 188 | + .setExpirySeconds(1000).setNotValidBeforeLeeway(5) |
| 189 | + .setIssuedAt(true).setJwtId(true)); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + public void shouldOptionsAlgorithm() throws Exception { |
| 194 | + HashMap<String, Object> claims = new HashMap<String, Object>(); |
| 195 | + String token = signer.sign(claims, |
| 196 | + new JWTSigner.Options().setAlgorithm(Algorithm.HS512)); |
| 197 | + assertEquals("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.e30.11MgCe-_uiheyy_kARCwhSZbeq3IkMn40GLQkczQ4Bjn_lkCYfSeqz0HeeYpitksiQ2bW47N0oGKCOYOlmQPyg", token); |
| 198 | + } |
| 199 | + |
| 200 | +} |
0 commit comments