|
| 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 | +} |
0 commit comments