Skip to content

Commit 8c1fa59

Browse files
committed
Support Boolean and boolean as a valid injection point.
1 parent 28e147e commit 8c1fa59

4 files changed

Lines changed: 179 additions & 1 deletion

File tree

implementation/src/main/java/io/smallrye/jwt/auth/cdi/RawClaimTypeProducer.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import javax.inject.Inject;
2727
import javax.json.JsonNumber;
2828
import javax.json.JsonString;
29+
import javax.json.JsonValue;
2930

3031
import org.eclipse.microprofile.jwt.Claim;
3132
import org.eclipse.microprofile.jwt.Claims;
@@ -120,6 +121,33 @@ Double getClaimAsDouble(InjectionPoint ip) {
120121
return returnValue;
121122
}
122123

124+
@Produces
125+
@Claim("")
126+
Boolean getClaimAsBoolean(InjectionPoint ip) {
127+
log.debugf("getClaimAsBoolean(%s)", ip);
128+
if (currentToken == null) {
129+
return null;
130+
}
131+
132+
String name = getName(ip);
133+
Optional<Object> optValue = currentToken.claim(name);
134+
Boolean returnValue = null;
135+
if (optValue.isPresent()) {
136+
Object value = optValue.get();
137+
if (value instanceof JsonValue) {
138+
final JsonValue.ValueType valueType = ((JsonValue) value).getValueType();
139+
if (valueType.equals(JsonValue.ValueType.TRUE)) {
140+
returnValue = true;
141+
} else if (valueType.equals(JsonValue.ValueType.FALSE)) {
142+
returnValue = false;
143+
}
144+
} else {
145+
returnValue = Boolean.valueOf(value.toString());
146+
}
147+
}
148+
return returnValue;
149+
}
150+
123151
/**
124152
* Produces a *raw* Optional value.
125153
*

implementation/src/main/java/io/smallrye/jwt/auth/principal/DefaultJWTCallerPrincipal.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import javax.json.JsonArray;
2626
import javax.json.JsonNumber;
2727
import javax.json.JsonObject;
28+
import javax.json.JsonValue;
2829

2930
import org.eclipse.microprofile.jwt.Claims;
3031
import org.jboss.logging.Logger;
@@ -161,6 +162,8 @@ private void fixJoseTypes() {
161162
replaceMap(name);
162163
} else if (claimValue instanceof Number) {
163164
replaceNumber(name);
165+
} else if (claimValue instanceof Boolean) {
166+
replaceBoolean(name);
164167
}
165168
}
166169
}
@@ -218,4 +221,14 @@ protected void replaceNumber(String name) {
218221
LOGGER.warn("replaceNumber failure for: " + name, e);
219222
}
220223
}
221-
}
224+
225+
protected void replaceBoolean(String name) {
226+
try {
227+
Boolean bool = claimsSet.getClaimValue(name, Boolean.class);
228+
JsonValue jsonBoolean = JsonUtils.wrapValue(bool);
229+
claimsSet.setClaim(name, jsonBoolean);
230+
} catch (MalformedClaimException e) {
231+
LOGGER.warn("replaceNumber failure for: " + name, e);
232+
}
233+
}
234+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package io.smallrye.jwt.auth.cdi;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import javax.enterprise.context.RequestScoped;
7+
import javax.enterprise.inject.Produces;
8+
import javax.inject.Inject;
9+
import javax.json.JsonNumber;
10+
11+
import org.eclipse.microprofile.jwt.Claim;
12+
import org.eclipse.microprofile.jwt.ClaimValue;
13+
import org.eclipse.microprofile.jwt.JsonWebToken;
14+
import org.jboss.weld.junit4.WeldInitiator;
15+
import org.jose4j.jws.JsonWebSignature;
16+
import org.jose4j.jwt.JwtClaims;
17+
import org.junit.Rule;
18+
import org.junit.Test;
19+
20+
import io.smallrye.jwt.KeyUtils;
21+
import io.smallrye.jwt.auth.principal.DefaultJWTCallerPrincipal;
22+
import io.smallrye.jwt.build.Jwt;
23+
24+
@SuppressWarnings("CdiUnproxyableBeanTypesInspection")
25+
public class ClaimInjectionTest {
26+
@Rule
27+
public WeldInitiator weld = WeldInitiator.from(
28+
ClaimInjectionTest.class,
29+
ClaimsBean.class,
30+
RawClaimTypeProducer.class,
31+
ClaimValueProducer.class,
32+
CommonJwtProducer.class)
33+
.addBeans()
34+
.activate(RequestScoped.class)
35+
.inject(this)
36+
.build();
37+
38+
@Inject
39+
private JsonWebToken jsonWebToken;
40+
@Inject
41+
private ClaimsBean claimsBean;
42+
43+
@Test
44+
public void inject() {
45+
assertTrue(claimsBean.isBooleanClaim());
46+
assertTrue(claimsBean.getBooleanClaimWrapper());
47+
//assertTrue(claimsBean.getBooleanClaimValue().getValue()); // does not unwrap json to wrapper type
48+
assertEquals(999, claimsBean.getLongClaim());
49+
assertEquals(999, claimsBean.getLongClaimWrapper().longValue());
50+
//assertEquals(999, claimsBean.getLongClaimValue().getValue().longValue()); // does not unwrap json to wrapper type
51+
assertEquals(999, claimsBean.getLongClaimValueJson().getValue().longValue());
52+
}
53+
54+
@Produces
55+
@RequestScoped
56+
private static JsonWebToken jwt() throws Exception {
57+
String jwt = Jwt.claims("/token-claims.json").sign();
58+
JsonWebSignature jws = new JsonWebSignature();
59+
jws.setKey(KeyUtils.readPublicKey("/publicKey.pem"));
60+
jws.setCompactSerialization(jwt);
61+
JwtClaims claims = JwtClaims.parse(jws.getPayload());
62+
return new DefaultJWTCallerPrincipal(jwt, claims);
63+
}
64+
65+
@RequestScoped
66+
private static class ClaimsBean {
67+
@Inject
68+
@Claim("boolean")
69+
private boolean booleanClaim;
70+
@Inject
71+
@Claim("boolean")
72+
private Boolean booleanClaimWrapper;
73+
//@Inject
74+
//(@Claim("boolean")
75+
private ClaimValue<Boolean> booleanClaimValue;
76+
@Inject
77+
@Claim("long")
78+
private long longClaim;
79+
@Inject
80+
@Claim("long")
81+
private Long longClaimWrapper;
82+
//@Inject
83+
//@Claim("long")
84+
private ClaimValue<Long> longClaimValue;
85+
@Inject
86+
@Claim("long")
87+
private ClaimValue<JsonNumber> longClaimValueJson;
88+
89+
boolean isBooleanClaim() {
90+
return booleanClaim;
91+
}
92+
93+
Boolean getBooleanClaimWrapper() {
94+
return booleanClaimWrapper;
95+
}
96+
97+
ClaimValue<Boolean> getBooleanClaimValue() {
98+
return booleanClaimValue;
99+
}
100+
101+
long getLongClaim() {
102+
return longClaim;
103+
}
104+
105+
Long getLongClaimWrapper() {
106+
return longClaimWrapper;
107+
}
108+
109+
ClaimValue<Long> getLongClaimValue() {
110+
return longClaimValue;
111+
}
112+
113+
ClaimValue<JsonNumber> getLongClaimValueJson() {
114+
return longClaimValueJson;
115+
}
116+
}
117+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"iss": "https://server.example.com",
3+
"jti": "a-123",
4+
"sub": "24400320",
5+
"upn": "jdoe@example.com",
6+
"preferred_username": "jdoe",
7+
"aud": "s6BhdRkqt3",
8+
"exp": 1311281970,
9+
"iat": 1311280970,
10+
"auth_time": 1311280969,
11+
12+
"byte": 1,
13+
"short": 9,
14+
"integer": 99,
15+
"long": 999,
16+
"float": 99.9,
17+
"double": 99.99,
18+
"boolean": true,
19+
"char": "y"
20+
}

0 commit comments

Comments
 (0)