Skip to content

Commit df3ddc0

Browse files
committed
Use enum to represent Claim types
1 parent c22a7b4 commit df3ddc0

File tree

9 files changed

+160
-187
lines changed

9 files changed

+160
-187
lines changed

test-framework/security-jwt/pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
<dependency>
3232
<groupId>io.quarkus</groupId>
3333
<artifactId>quarkus-jsonp</artifactId>
34+
<scope>compile</scope>
35+
<optional>true</optional>
3436
</dependency>
3537
<dependency>
3638
<groupId>org.eclipse.microprofile.jwt</groupId>

test-framework/security-jwt/src/main/java/io/quarkus/test/security/jwt/Claim.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
String value();
1919

2020
/**
21-
* Claim value type, the value will be converted to String if this type is set to Object.class.
22-
* Supported types: String, Integer, int, Long, long, Boolean, boolean, jakarta.json.JsonArray, jakarta.json.JsonObject.
21+
* Claim value type.
22+
* If this type is set to {@link ClaimType#DEFAULT} then the value will be converted to String unless the claim
23+
* is a standard claim such as `exp` (expiry), `iat` (issued at), `nbf` (not before), `auth_time` (authentication time)
24+
* whose value will be converted to Long or `email_verified` whose value will be converted to Boolean.
2325
*/
24-
Class<?> type() default Object.class;
26+
ClaimType type() default ClaimType.DEFAULT;
2527
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.quarkus.test.security.jwt;
2+
3+
import java.io.StringReader;
4+
5+
import jakarta.json.Json;
6+
import jakarta.json.JsonArray;
7+
import jakarta.json.JsonObject;
8+
import jakarta.json.JsonReader;
9+
10+
public enum ClaimType {
11+
LONG {
12+
@Override
13+
public Long convert(String value) {
14+
return Long.parseLong(value);
15+
}
16+
},
17+
INTEGER {
18+
@Override
19+
public Integer convert(String value) {
20+
return Integer.parseInt(value);
21+
}
22+
},
23+
BOOLEAN {
24+
@Override
25+
public Boolean convert(String value) {
26+
return Boolean.parseBoolean(value);
27+
}
28+
},
29+
STRING {
30+
@Override
31+
public String convert(String value) {
32+
return value;
33+
}
34+
},
35+
JSON_ARRAY {
36+
@Override
37+
public JsonArray convert(String value) {
38+
try (JsonReader jsonReader = Json.createReader(new StringReader(value))) {
39+
return jsonReader.readArray();
40+
}
41+
}
42+
},
43+
JSON_OBJECT {
44+
@Override
45+
public JsonObject convert(String value) {
46+
try (JsonReader jsonReader = Json.createReader(new StringReader(value))) {
47+
return jsonReader.readObject();
48+
}
49+
}
50+
},
51+
DEFAULT {
52+
@Override
53+
public String convert(String value) {
54+
return value;
55+
}
56+
};
57+
58+
abstract Object convert(String value);
59+
}
Lines changed: 11 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.quarkus.test.security.jwt;
22

3-
import java.io.StringReader;
43
import java.lang.annotation.Annotation;
54
import java.util.Arrays;
65
import java.util.Collections;
@@ -9,9 +8,6 @@
98
import java.util.stream.Collectors;
109

1110
import jakarta.json.Json;
12-
import jakarta.json.JsonArray;
13-
import jakarta.json.JsonObject;
14-
import jakarta.json.JsonReader;
1511
import jakarta.json.JsonValue;
1612

1713
import org.eclipse.microprofile.jwt.Claims;
@@ -22,26 +18,12 @@
2218
import io.quarkus.test.security.TestSecurityIdentityAugmentor;
2319

2420
public class JwtTestSecurityIdentityAugmentor implements TestSecurityIdentityAugmentor {
25-
private static Converter<Long> longConverter = new LongConverter();
26-
private static Converter<Integer> intConverter = new IntegerConverter();
27-
private static Converter<Boolean> booleanConverter = new BooleanConverter();
28-
private static Map<String, Converter<?>> standardClaimConverteres = Map.of(
29-
Claims.exp.name(), longConverter,
30-
Claims.iat.name(), longConverter,
31-
Claims.nbf.name(), longConverter,
32-
Claims.auth_time.name(), longConverter,
33-
Claims.email_verified.name(), booleanConverter);
34-
35-
private static Map<Class<?>, Converter<?>> converters = Map.of(
36-
String.class, new StringConverter(),
37-
Integer.class, intConverter,
38-
int.class, intConverter,
39-
Long.class, longConverter,
40-
long.class, longConverter,
41-
Boolean.class, booleanConverter,
42-
boolean.class, booleanConverter,
43-
JsonArray.class, new JsonArrayConverter(),
44-
JsonObject.class, new JsonObjectConverter());
21+
private static Map<String, ClaimType> standardClaimTypes = Map.of(
22+
Claims.exp.name(), ClaimType.LONG,
23+
Claims.iat.name(), ClaimType.LONG,
24+
Claims.nbf.name(), ClaimType.LONG,
25+
Claims.auth_time.name(), ClaimType.LONG,
26+
Claims.email_verified.name(), ClaimType.BOOLEAN);
4527

4628
@Override
4729
public SecurityIdentity augment(final SecurityIdentity identity, final Annotation[] annotations) {
@@ -117,70 +99,12 @@ protected Claims getClaimType(String claimName) {
11799
return claimType;
118100
}
119101

120-
@SuppressWarnings("unchecked")
121-
private <T> T convertClaimValue(Claim claim) {
122-
if (claim.type() != Object.class) {
123-
Converter<?> converter = converters.get(claim.type());
124-
if (converter != null) {
125-
return (T) converter.convert(claim.value());
126-
} else {
127-
throw new RuntimeException("Unsupported claim type: " + claim.type().getName());
128-
}
129-
} else if (standardClaimConverteres.containsKey(claim.key())) {
130-
Converter<?> converter = standardClaimConverteres.get(claim.key());
131-
return (T) converter.convert(claim.value());
132-
} else {
133-
return (T) claim.value();
134-
}
135-
}
136-
137-
private static interface Converter<T> {
138-
T convert(String value);
139-
}
140-
141-
private static class StringConverter implements Converter<String> {
142-
@Override
143-
public String convert(String value) {
144-
return value;
145-
}
146-
}
147-
148-
private static class IntegerConverter implements Converter<Integer> {
149-
@Override
150-
public Integer convert(String value) {
151-
return Integer.valueOf(value);
152-
}
153-
}
154-
155-
private static class LongConverter implements Converter<Long> {
156-
@Override
157-
public Long convert(String value) {
158-
return Long.valueOf(value);
159-
}
160-
}
161-
162-
private static class BooleanConverter implements Converter<Boolean> {
163-
@Override
164-
public Boolean convert(String value) {
165-
return Boolean.valueOf(value);
102+
private Object convertClaimValue(Claim claim) {
103+
ClaimType type = claim.type();
104+
if (type == ClaimType.DEFAULT && standardClaimTypes.containsKey(claim.key())) {
105+
type = standardClaimTypes.get(claim.key());
166106
}
107+
return type.convert(claim.value());
167108
}
168109

169-
private static class JsonObjectConverter implements Converter<JsonObject> {
170-
@Override
171-
public JsonObject convert(String value) {
172-
try (JsonReader jsonReader = Json.createReader(new StringReader(value))) {
173-
return jsonReader.readObject();
174-
}
175-
}
176-
}
177-
178-
private static class JsonArrayConverter implements Converter<JsonArray> {
179-
@Override
180-
public JsonArray convert(String value) {
181-
try (JsonReader jsonReader = Json.createReader(new StringReader(value))) {
182-
return jsonReader.readArray();
183-
}
184-
}
185-
}
186110
}

test-framework/security-jwt/src/test/java/io/quarkus/test/security/jwt/JwtTestSecurityIdentityAugmentorTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ public class JwtTestSecurityIdentityAugmentorTest {
2727
@Claim(key = "iat", value = "123456788"),
2828
@Claim(key = "nbf", value = "123456787"),
2929
@Claim(key = "auth_time", value = "123456786"),
30-
@Claim(key = "customlong", value = "123456785", type = Long.class),
30+
@Claim(key = "customlong", value = "123456785", type = ClaimType.LONG),
3131
@Claim(key = "email", value = "[email protected]"),
3232
@Claim(key = "email_verified", value = "true"),
33-
@Claim(key = "email_checked", value = "false", type = Boolean.class),
34-
@Claim(key = "jsonarray_claim", value = "[\"1\", \"2\"]", type = JsonArray.class),
35-
@Claim(key = "jsonobject_claim", value = "{\"a\":\"1\", \"b\":\"2\"}", type = JsonObject.class)
33+
@Claim(key = "email_checked", value = "false", type = ClaimType.BOOLEAN),
34+
@Claim(key = "jsonarray_claim", value = "[\"1\", \"2\"]", type = ClaimType.JSON_ARRAY),
35+
@Claim(key = "jsonobject_claim", value = "{\"a\":\"1\", \"b\":\"2\"}", type = ClaimType.JSON_OBJECT)
3636
})
3737
public void testClaimValues() throws Exception {
3838
SecurityIdentity identity = QuarkusSecurityIdentity.builder()

test-framework/security-oidc/src/main/java/io/quarkus/test/security/oidc/Claim.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@
1818
String value();
1919

2020
/**
21-
* Claim value type, the value will be converted to String if this type is set to Object.class.
22-
* Supported types: String, Integer, int, Long, long, Boolean, boolean, jakarta.json.JsonArray, jakarta.json.JsonObject.
21+
* Claim value type.
22+
* If this type is set to {@link ClaimType#DEFAULT} then the value will be converted to String unless the claim
23+
* is a standard claim such as `exp` (expiry), `iat` (issued at), `nbf` (not before), `auth_time` (authentication time)
24+
* whose value will be converted to Long or `email_verified` whose value will be converted to Boolean.
2325
*/
24-
Class<?> type() default Object.class;
26+
ClaimType type() default ClaimType.DEFAULT;
2527
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package io.quarkus.test.security.oidc;
2+
3+
import java.io.StringReader;
4+
5+
import jakarta.json.Json;
6+
import jakarta.json.JsonArray;
7+
import jakarta.json.JsonObject;
8+
import jakarta.json.JsonReader;
9+
10+
public enum ClaimType {
11+
LONG {
12+
@Override
13+
public Long convert(String value) {
14+
return Long.parseLong(value);
15+
}
16+
},
17+
INTEGER {
18+
@Override
19+
public Integer convert(String value) {
20+
return Integer.parseInt(value);
21+
}
22+
},
23+
BOOLEAN {
24+
@Override
25+
public Boolean convert(String value) {
26+
return Boolean.parseBoolean(value);
27+
}
28+
},
29+
STRING {
30+
@Override
31+
public String convert(String value) {
32+
return value;
33+
}
34+
},
35+
JSON_ARRAY {
36+
@Override
37+
public JsonArray convert(String value) {
38+
try (JsonReader jsonReader = Json.createReader(new StringReader(value))) {
39+
return jsonReader.readArray();
40+
}
41+
}
42+
},
43+
JSON_OBJECT {
44+
@Override
45+
public JsonObject convert(String value) {
46+
try (JsonReader jsonReader = Json.createReader(new StringReader(value))) {
47+
return jsonReader.readObject();
48+
}
49+
}
50+
},
51+
DEFAULT {
52+
@Override
53+
public String convert(String value) {
54+
return value;
55+
}
56+
};
57+
58+
abstract Object convert(String value);
59+
}

0 commit comments

Comments
 (0)