forked from auth0/java-jwt
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoundtripTest.java
More file actions
138 lines (120 loc) · 4.79 KB
/
RoundtripTest.java
File metadata and controls
138 lines (120 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package com.auth0.jwt;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.codec.binary.Base64;
import org.junit.Test;
/**
* Test things that are difficult using signer or verifier alone. In particular, setting
* claims via Options produces output dependent on current time.
*
*/
public class RoundtripTest {
private static final String SECRET;
static {
SECRET = "my secret";
}
private static JWTSigner signer = new JWTSigner(SECRET);
private static JWTVerifier verifier = new JWTVerifier(SECRET);
/*
* Roundtrip of different datatypes.
*/
@Test
public void shouldEmpty() throws Exception {
HashMap<String, Object> claims = new HashMap<String, Object>();
String token = signer.sign(claims);
Map<String, Object> decoded = verifier.verify(token);
assertEquals(claims, decoded);
}
@Test
public void shouldString() throws Exception {
HashMap<String, Object> claims = new HashMap<String, Object>();
claims.put("foo", "bar");
String token = signer.sign(claims);
Map<String, Object> decoded = verifier.verify(token);
assertEquals(claims, decoded);
}
@Test
public void shouldShort() throws Exception {
HashMap<String, Object> claims = new HashMap<String, Object>();
claims.put("foo", (short) -10);
String token = signer.sign(claims);
Map<String, Object> decoded = verifier.verify(token);
Number fooValue = (Number) decoded.get("foo");
decoded.put("foo", fooValue.shortValue());
assertEquals(claims, decoded);
}
@Test
public void shouldLong() throws Exception {
HashMap<String, Object> claims = new HashMap<String, Object>();
claims.put("foo", Long.MAX_VALUE);
String token = signer.sign(claims);
Map<String, Object> decoded = verifier.verify(token);
assertEquals(claims, decoded);
}
@Test
public void shouldObject() throws Exception {
HashMap<String, Object> claims = new HashMap<String, Object>();
User user = new User();
user.setUsername("foo");
user.setPassword("bar");
claims.put("user", user);
String token = signer.sign(claims);
Map<String, Object> decoded = verifier.verify(token);
HashMap<String, String> expectedUser = new HashMap<String, String>();
expectedUser.put("username", "foo");
expectedUser.put("password", "bar");
HashMap<String, Object> expected = new HashMap<String, Object>();
expected.put("user", expectedUser);
assertEquals(expected, decoded);
}
@Test
public void shouldBoolean() throws Exception {
HashMap<String, Object> claims = new HashMap<String, Object>();
claims.put("foo", true);
claims.put("bar", false);
String token = signer.sign(claims);
Map<String, Object> decoded = verifier.verify(token);
assertEquals(claims, decoded);
}
/*
* Setting claims via Options
*/
@Test
public void shouldOptionsIat() throws Exception {
HashMap<String, Object> claims = new HashMap<String, Object>();
long before = System.currentTimeMillis();
String token = signer.sign(claims, new JWTSigner.Options().setIssuedAt(true));
long after = System.currentTimeMillis();
Map<String, Object> decoded = verifier.verify(token);
assertEquals(decoded.size(), 1);
long iat = ((Number) decoded.get("iat")).longValue();
assertTrue(iat >= before / 1000l);
assertTrue(iat <= after / 1000l);
}
@Test
public void shouldOptionsTimestamps() throws Exception {
HashMap<String, Object> claims = new HashMap<String, Object>();
String token = signer.sign(claims,
new JWTSigner.Options()
.setExpirySeconds(50).setNotValidBeforeLeeway(10).setIssuedAt(true));
Map<String, Object> decoded = verifier.verify(token);
assertEquals(decoded.size(), 3);
long iat = ((Number) decoded.get("iat")).longValue();
long exp = ((Number) decoded.get("exp")).longValue();
long nbf = ((Number) decoded.get("nbf")).longValue();
assertEquals(exp, iat + 50);
assertEquals(nbf, iat - 10);
}
@Test
public void shouldOptionsJti() throws Exception {
HashMap<String, Object> claims = new HashMap<String, Object>();
String token = signer.sign(claims,
new JWTSigner.Options().setJwtId(true));
Map<String, Object> decoded = verifier.verify(token);
assertEquals(decoded.size(), 1);
assertEquals(((String) decoded.get("jti")).length(), 36);
}
}