|
| 1 | +package io.quarkus.test.security.jwt; |
| 2 | + |
| 3 | +import java.io.StringReader; |
| 4 | +import java.lang.annotation.Annotation; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.Collections; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.Set; |
| 9 | +import java.util.stream.Collectors; |
| 10 | + |
| 11 | +import jakarta.json.Json; |
| 12 | +import jakarta.json.JsonArray; |
| 13 | +import jakarta.json.JsonObject; |
| 14 | +import jakarta.json.JsonReader; |
| 15 | + |
| 16 | +import org.eclipse.microprofile.jwt.Claims; |
| 17 | +import org.eclipse.microprofile.jwt.JsonWebToken; |
| 18 | + |
| 19 | +import io.quarkus.security.identity.SecurityIdentity; |
| 20 | +import io.quarkus.security.runtime.QuarkusSecurityIdentity; |
| 21 | +import io.quarkus.test.security.TestSecurityIdentityAugmentor; |
| 22 | + |
| 23 | +public class JwtTestSecurityIdentityAugmentor implements TestSecurityIdentityAugmentor { |
| 24 | + private static Converter<Long> longConverter = new LongConverter(); |
| 25 | + private static Converter<Integer> intConverter = new IntegerConverter(); |
| 26 | + private static Converter<Boolean> booleanConverter = new BooleanConverter(); |
| 27 | + private static Map<String, Converter<?>> standardClaimConverteres = Map.of( |
| 28 | + Claims.exp.name(), longConverter, |
| 29 | + Claims.iat.name(), longConverter, |
| 30 | + Claims.nbf.name(), longConverter, |
| 31 | + Claims.auth_time.name(), longConverter, |
| 32 | + Claims.email_verified.name(), booleanConverter); |
| 33 | + |
| 34 | + private static Map<Class<?>, Converter<?>> converters = Map.of( |
| 35 | + String.class, new StringConverter(), |
| 36 | + Integer.class, intConverter, |
| 37 | + int.class, intConverter, |
| 38 | + Long.class, longConverter, |
| 39 | + long.class, longConverter, |
| 40 | + Boolean.class, booleanConverter, |
| 41 | + boolean.class, booleanConverter, |
| 42 | + JsonArray.class, new JsonArrayConverter(), |
| 43 | + JsonObject.class, new JsonObjectConverter()); |
| 44 | + |
| 45 | + @Override |
| 46 | + public SecurityIdentity augment(final SecurityIdentity identity, final Annotation[] annotations) { |
| 47 | + QuarkusSecurityIdentity.Builder builder = QuarkusSecurityIdentity.builder(identity); |
| 48 | + |
| 49 | + final JwtSecurity jwtSecurity = findJwtSecurity(annotations); |
| 50 | + builder.setPrincipal(new JsonWebToken() { |
| 51 | + |
| 52 | + @Override |
| 53 | + public String getName() { |
| 54 | + return identity.getPrincipal().getName(); |
| 55 | + } |
| 56 | + |
| 57 | + @SuppressWarnings("unchecked") |
| 58 | + @Override |
| 59 | + public <T> T getClaim(String claimName) { |
| 60 | + if (Claims.groups.name().equals(claimName)) { |
| 61 | + return (T) identity.getRoles(); |
| 62 | + } |
| 63 | + if (jwtSecurity != null && jwtSecurity.claims() != null) { |
| 64 | + for (Claim claim : jwtSecurity.claims()) { |
| 65 | + if (claim.key().equals(claimName)) { |
| 66 | + return convertClaimValue(claim); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return null; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Set<String> getClaimNames() { |
| 75 | + if (jwtSecurity != null && jwtSecurity.claims() != null) { |
| 76 | + return Arrays.stream(jwtSecurity.claims()).map(Claim::key).collect(Collectors.toSet()); |
| 77 | + } |
| 78 | + return Collections.emptySet(); |
| 79 | + } |
| 80 | + |
| 81 | + }); |
| 82 | + |
| 83 | + return builder.build(); |
| 84 | + } |
| 85 | + |
| 86 | + private static JwtSecurity findJwtSecurity(Annotation[] annotations) { |
| 87 | + for (Annotation ann : annotations) { |
| 88 | + if (ann instanceof JwtSecurity) { |
| 89 | + return (JwtSecurity) ann; |
| 90 | + } |
| 91 | + } |
| 92 | + return null; |
| 93 | + } |
| 94 | + |
| 95 | + @SuppressWarnings("unchecked") |
| 96 | + private <T> T convertClaimValue(Claim claim) { |
| 97 | + if (claim.type() != Object.class) { |
| 98 | + Converter<?> converter = converters.get(claim.type()); |
| 99 | + if (converter != null) { |
| 100 | + return (T) converter.convert(claim.value()); |
| 101 | + } else { |
| 102 | + throw new RuntimeException("Unsupported claim type: " + claim.type().getName()); |
| 103 | + } |
| 104 | + } else if (standardClaimConverteres.containsKey(claim.key())) { |
| 105 | + Converter<?> converter = standardClaimConverteres.get(claim.key()); |
| 106 | + return (T) converter.convert(claim.value()); |
| 107 | + } else { |
| 108 | + return (T) claim.value(); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + private static interface Converter<T> { |
| 113 | + T convert(String value); |
| 114 | + } |
| 115 | + |
| 116 | + private static class StringConverter implements Converter<String> { |
| 117 | + @Override |
| 118 | + public String convert(String value) { |
| 119 | + return value; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + private static class IntegerConverter implements Converter<Integer> { |
| 124 | + @Override |
| 125 | + public Integer convert(String value) { |
| 126 | + return Integer.valueOf(value); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static class LongConverter implements Converter<Long> { |
| 131 | + @Override |
| 132 | + public Long convert(String value) { |
| 133 | + return Long.valueOf(value); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static class BooleanConverter implements Converter<Boolean> { |
| 138 | + @Override |
| 139 | + public Boolean convert(String value) { |
| 140 | + return Boolean.valueOf(value); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + private static class JsonObjectConverter implements Converter<JsonObject> { |
| 145 | + @Override |
| 146 | + public JsonObject convert(String value) { |
| 147 | + try (JsonReader jsonReader = Json.createReader(new StringReader(value))) { |
| 148 | + return jsonReader.readObject(); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private static class JsonArrayConverter implements Converter<JsonArray> { |
| 154 | + @Override |
| 155 | + public JsonArray convert(String value) { |
| 156 | + try (JsonReader jsonReader = Json.createReader(new StringReader(value))) { |
| 157 | + return jsonReader.readArray(); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | +} |
0 commit comments