|
8 | 8 | import static org.mockito.Mockito.verify; |
9 | 9 | import static org.mockito.Mockito.when; |
10 | 10 |
|
| 11 | +import com.fasterxml.jackson.annotation.JsonProperty; |
11 | 12 | import com.google.common.collect.ImmutableList; |
12 | | -import java.util.Collections; |
13 | 13 | import java.util.List; |
14 | 14 | import java.util.Optional; |
| 15 | +import lombok.AllArgsConstructor; |
| 16 | +import lombok.NoArgsConstructor; |
| 17 | +import lombok.Value; |
15 | 18 | import org.junit.jupiter.api.Test; |
16 | 19 | import org.mockito.ArgumentMatchers; |
17 | 20 |
|
18 | 21 | class JwtParserTest { |
19 | 22 | private final String testJwt = |
20 | 23 | "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MjEzNjM1OTcsImV4cCI6MTY1Mjg5OTU5NywiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJuYW1lIjoiSm9obm55IFJvY2tldCIsImVtYWlsIjoianJvY2tldEBleGFtcGxlLmNvbSIsInBpY3R1cmUiOiJ3d3cuZXhhbXBsZS5jb20iLCJyb2xlcyI6WyJzdXBlcl91c2VyIiwidXNlciIsImJpbGxpbmdfYWRtaW4iXX0.lEDjPPCjr-Epv6pNslq-HK9vmxfstp1sY85GstlbU1I"; |
21 | | - private final String emptyRolesJwt = |
22 | | - "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MjEzNjM1OTcsImV4cCI6MTY1Mjg5OTU5NywiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJuYW1lIjoiSm9obm55IFJvY2tldCIsImVtYWlsIjoianJvY2tldEBleGFtcGxlLmNvbSIsInBpY3R1cmUiOiJ3d3cuZXhhbXBsZS5jb20iLCJodHRwczovL3RyYWNlYWJsZS5haS9yb2xlcyI6W119.sFUMZNyypj379xy5P4kqTbBXBOR5XvX2nhpKx6YiiwU"; |
23 | | - private final String noRolesJwt = |
24 | | - "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MjEzNjM1OTcsImV4cCI6MTY1Mjg5OTU5NywiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJuYW1lIjoiSm9obm55IFJvY2tldCIsImVtYWlsIjoianJvY2tldEBleGFtcGxlLmNvbSIsInBpY3R1cmUiOiJ3d3cuZXhhbXBsZS5jb20ifQ.Ui1Z2RhiVe3tq6uJPgcyjsfDBdeOeINs_gXEHC6cdpU"; |
25 | 24 | private final String testJwtUserId = "jrocket@example.com"; |
26 | 25 | private final String testJwtName = "Johnny Rocket"; |
27 | 26 | private final String testJwtPictureUrl = "www.example.com"; |
@@ -65,23 +64,59 @@ void testExtractBearerTokenReturnsEmptyOnMalformed() { |
65 | 64 | } |
66 | 65 |
|
67 | 66 | @Test |
68 | | - void testRolesCanBeParsedFromToken() { |
| 67 | + void testClaimCanBeParsedFromToken() { |
69 | 68 | JwtParser parser = new JwtParser(); |
70 | 69 | Optional<Jwt> jwt = parser.fromJwt(testJwt); |
71 | | - assertEquals(Optional.of(testRoles), jwt.map(j -> j.getRoles(testRolesClaim))); |
| 70 | + assertEquals( |
| 71 | + Optional.of(testRoles), |
| 72 | + jwt.flatMap(j -> j.getClaim(testRolesClaim)).flatMap(claim -> claim.asList(String.class))); |
72 | 73 | } |
73 | 74 |
|
74 | 75 | @Test |
75 | | - void testRolesAreEmptyIfRolesArrayIsEmptyInJwt() { |
| 76 | + void testCanParseObjectClaim() { |
| 77 | + String jwtWithObjectArrayClaim = |
| 78 | + "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2MjEzNjM1OTcsImV4cCI6MTY1Mjg5OTU5NywiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6IkpvaG5ueSIsIlN1cm5hbWUiOiJSb2NrZXQiLCJuYW1lIjoiSm9obm55IFJvY2tldCIsImVtYWlsIjoianJvY2tldEBleGFtcGxlLmNvbSIsInBpY3R1cmUiOiJ3d3cuZXhhbXBsZS5jb20iLCJyb2xlcyI6W3siaWQiOiJzdXBlcl91c2VyIiwidmFsdWVzIjpbXX0seyJpZCI6InVzZXIiLCJ2YWx1ZXMiOlsiZm9vIl19XX0.EJyZWwbfbCAS4NJdwURAsOewf8V6863D1ZqXGTVZigE"; |
| 79 | + /* |
| 80 | + { |
| 81 | + "iss": "Online JWT Builder", |
| 82 | + "iat": 1621363597, |
| 83 | + "exp": 1652899597, |
| 84 | + "aud": "www.example.com", |
| 85 | + "sub": "jrocket@example.com", |
| 86 | + "GivenName": "Johnny", |
| 87 | + "Surname": "Rocket", |
| 88 | + "name": "Johnny Rocket", |
| 89 | + "email": "jrocket@example.com", |
| 90 | + "picture": "www.example.com", |
| 91 | + "roles": [ |
| 92 | + { |
| 93 | + "id": "super_user", |
| 94 | + "values": [] |
| 95 | + }, |
| 96 | + { |
| 97 | + "id": "user", |
| 98 | + "values": [ |
| 99 | + "foo" |
| 100 | + ] |
| 101 | + } |
| 102 | + ] |
| 103 | + } |
| 104 | + */ |
76 | 105 | JwtParser parser = new JwtParser(); |
77 | | - Optional<Jwt> jwt = parser.fromJwt(emptyRolesJwt); |
78 | | - assertEquals(Optional.of(Collections.emptyList()), jwt.map(j -> j.getRoles(testRolesClaim))); |
| 106 | + Optional<Jwt> jwt = parser.fromJwt(jwtWithObjectArrayClaim); |
| 107 | + |
| 108 | + assertEquals( |
| 109 | + Optional.of( |
| 110 | + List.of( |
| 111 | + new TestObject("super_user", List.of()), new TestObject("user", List.of("foo")))), |
| 112 | + jwt.flatMap(j -> j.getClaim("roles")).flatMap(claim -> claim.asList(TestObject.class))); |
79 | 113 | } |
80 | 114 |
|
81 | | - @Test |
82 | | - void testRolesAreEmptyIfRolesIfNoRolesClaimInToken() { |
83 | | - JwtParser parser = new JwtParser(); |
84 | | - Optional<Jwt> jwt = parser.fromJwt(noRolesJwt); |
85 | | - assertEquals(Optional.of(Collections.emptyList()), jwt.map(j -> j.getRoles(testRolesClaim))); |
| 115 | + @Value |
| 116 | + @NoArgsConstructor(force = true) |
| 117 | + @AllArgsConstructor |
| 118 | + private static class TestObject { |
| 119 | + @JsonProperty String id; |
| 120 | + @JsonProperty List<String> values; |
86 | 121 | } |
87 | 122 | } |
0 commit comments