|
| 1 | +package org.kohsuke.github.extras.authorization; |
| 2 | + |
| 3 | +import io.jsonwebtoken.JwtBuilder; |
| 4 | +import io.jsonwebtoken.Jwts; |
| 5 | +import io.jsonwebtoken.io.Serializer; |
| 6 | +import io.jsonwebtoken.jackson.io.JacksonSerializer; |
| 7 | +import io.jsonwebtoken.security.SignatureAlgorithm; |
| 8 | +import org.kohsuke.github.GHException; |
| 9 | + |
| 10 | +import java.lang.reflect.InvocationTargetException; |
| 11 | +import java.lang.reflect.Method; |
| 12 | +import java.security.Key; |
| 13 | +import java.security.PrivateKey; |
| 14 | +import java.time.Instant; |
| 15 | +import java.util.Date; |
| 16 | +import java.util.logging.Logger; |
| 17 | + |
| 18 | +/** |
| 19 | + * This is a util to build a JWT. |
| 20 | + * |
| 21 | + * <p> |
| 22 | + * This class is used to build a JWT using the jjwt library. It uses reflection to support older versions of jjwt. The |
| 23 | + * class may be removed once we are sure we no longer need to support pre-0.12.x versions of jjwt. |
| 24 | + * </p> |
| 25 | + */ |
| 26 | +final class JwtBuilderUtil { |
| 27 | + |
| 28 | + private static final Logger LOGGER = Logger.getLogger(JwtBuilderUtil.class.getName()); |
| 29 | + |
| 30 | + private static IJwtBuilder builder; |
| 31 | + |
| 32 | + /** |
| 33 | + * Build a JWT. |
| 34 | + * |
| 35 | + * @param issuedAt |
| 36 | + * issued at |
| 37 | + * @param expiration |
| 38 | + * expiration |
| 39 | + * @param applicationId |
| 40 | + * application id |
| 41 | + * @param privateKey |
| 42 | + * private key |
| 43 | + * @return JWT |
| 44 | + */ |
| 45 | + static String buildJwt(Instant issuedAt, Instant expiration, String applicationId, PrivateKey privateKey) { |
| 46 | + if (builder == null) { |
| 47 | + createBuilderImpl(issuedAt, expiration, applicationId, privateKey); |
| 48 | + } |
| 49 | + return builder.buildJwt(issuedAt, expiration, applicationId, privateKey); |
| 50 | + } |
| 51 | + |
| 52 | + private static void createBuilderImpl(Instant issuedAt, |
| 53 | + Instant expiration, |
| 54 | + String applicationId, |
| 55 | + PrivateKey privateKey) { |
| 56 | + // Figure out which builder to use and cache it. We don't worry about thread safety here because we're fine if |
| 57 | + // the builder is assigned multiple times. The end result will be the same. |
| 58 | + try { |
| 59 | + builder = new DefaultBuilderImpl(); |
| 60 | + } catch (NoSuchMethodError | NoClassDefFoundError e) { |
| 61 | + LOGGER.warning( |
| 62 | + "You are using an outdated version of the io.jsonwebtoken:jjwt-* suite. v0.12.x or later is recommended."); |
| 63 | + |
| 64 | + try { |
| 65 | + ReflectionBuilderImpl reflectionBuider = new ReflectionBuilderImpl(); |
| 66 | + // Build a JWT to eagerly check for any reflection errors. |
| 67 | + reflectionBuider.buildJwtWithReflection(issuedAt, expiration, applicationId, privateKey); |
| 68 | + |
| 69 | + builder = reflectionBuider; |
| 70 | + } catch (ReflectiveOperationException re) { |
| 71 | + throw new GHException( |
| 72 | + "Could not build JWT using reflection on io.jsonwebtoken:jjwt-* suite." |
| 73 | + + "The minimum supported version is v0.11.x, v0.12.x or later is recommended.", |
| 74 | + re); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * IJwtBuilder interface to isolate loading of JWT classes allowing us to catch and handle linkage errors. |
| 81 | + */ |
| 82 | + interface IJwtBuilder { |
| 83 | + /** |
| 84 | + * Build a JWT. |
| 85 | + * |
| 86 | + * @param issuedAt |
| 87 | + * issued at |
| 88 | + * @param expiration |
| 89 | + * expiration |
| 90 | + * @param applicationId |
| 91 | + * application id |
| 92 | + * @param privateKey |
| 93 | + * private key |
| 94 | + * @return JWT |
| 95 | + */ |
| 96 | + String buildJwt(Instant issuedAt, Instant expiration, String applicationId, PrivateKey privateKey); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * A class to isolate loading of JWT classes allowing us to catch and handle linkage errors. |
| 101 | + * |
| 102 | + * Without this class, JwtBuilderUtil.buildJwt() immediately throws NoClassDefFoundError when called. With this |
| 103 | + * class the error is thrown when DefaultBuilder.build() is called allowing us to catch and handle it. |
| 104 | + */ |
| 105 | + private static class DefaultBuilderImpl implements IJwtBuilder { |
| 106 | + /** |
| 107 | + * This method builds a JWT using 0.12.x or later versions of jjwt library |
| 108 | + * |
| 109 | + * @param issuedAt |
| 110 | + * issued at |
| 111 | + * @param expiration |
| 112 | + * expiration |
| 113 | + * @param applicationId |
| 114 | + * application id |
| 115 | + * @param privateKey |
| 116 | + * private key |
| 117 | + * @return JWT |
| 118 | + */ |
| 119 | + public String buildJwt(Instant issuedAt, Instant expiration, String applicationId, PrivateKey privateKey) { |
| 120 | + |
| 121 | + // io.jsonwebtoken.security.SignatureAlgorithm is not present in v0.11.x and below. |
| 122 | + // Trying to call a method that uses it causes "NoClassDefFoundError" if v0.11.x is being used. |
| 123 | + SignatureAlgorithm rs256 = Jwts.SIG.RS256; |
| 124 | + |
| 125 | + JwtBuilder jwtBuilder = Jwts.builder(); |
| 126 | + jwtBuilder = jwtBuilder.issuedAt(Date.from(issuedAt)) |
| 127 | + .expiration(Date.from(expiration)) |
| 128 | + .issuer(applicationId) |
| 129 | + .signWith(privateKey, rs256) |
| 130 | + .json(new JacksonSerializer<>()); |
| 131 | + return jwtBuilder.compact(); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * A class to encapsulate building a JWT using reflection. |
| 137 | + */ |
| 138 | + private static class ReflectionBuilderImpl implements IJwtBuilder { |
| 139 | + |
| 140 | + private Method setIssuedAtMethod; |
| 141 | + private Method setExpirationMethod; |
| 142 | + private Method setIssuerMethod; |
| 143 | + private Enum<?> rs256SignatureAlgorithm; |
| 144 | + private Method signWithMethod; |
| 145 | + private Method serializeToJsonMethod; |
| 146 | + |
| 147 | + ReflectionBuilderImpl() throws ReflectiveOperationException { |
| 148 | + JwtBuilder jwtBuilder = Jwts.builder(); |
| 149 | + Class<?> jwtReflectionClass = jwtBuilder.getClass(); |
| 150 | + |
| 151 | + setIssuedAtMethod = jwtReflectionClass.getMethod("setIssuedAt", Date.class); |
| 152 | + setIssuerMethod = jwtReflectionClass.getMethod("setIssuer", String.class); |
| 153 | + setExpirationMethod = jwtReflectionClass.getMethod("setExpiration", Date.class); |
| 154 | + Class<?> signatureAlgorithmClass = Class.forName("io.jsonwebtoken.SignatureAlgorithm"); |
| 155 | + rs256SignatureAlgorithm = createEnumInstance(signatureAlgorithmClass, "RS256"); |
| 156 | + signWithMethod = jwtReflectionClass.getMethod("signWith", Key.class, signatureAlgorithmClass); |
| 157 | + serializeToJsonMethod = jwtReflectionClass.getMethod("serializeToJsonWith", Serializer.class); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * This method builds a JWT using older (pre 0.12.x) versions of jjwt library by leveraging reflection. |
| 162 | + * |
| 163 | + * @param issuedAt |
| 164 | + * issued at |
| 165 | + * @param expiration |
| 166 | + * expiration |
| 167 | + * @param applicationId |
| 168 | + * application id |
| 169 | + * @param privateKey |
| 170 | + * private key |
| 171 | + * @return JWTBuilder |
| 172 | + */ |
| 173 | + public String buildJwt(Instant issuedAt, Instant expiration, String applicationId, PrivateKey privateKey) { |
| 174 | + |
| 175 | + try { |
| 176 | + return buildJwtWithReflection(issuedAt, expiration, applicationId, privateKey); |
| 177 | + } catch (ReflectiveOperationException e) { |
| 178 | + // This should never happen. Reflection errors should have been caught during initialization. |
| 179 | + throw new GHException("Reflection errors during JWT creation should have been checked already.", e); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + private String buildJwtWithReflection(Instant issuedAt, |
| 184 | + Instant expiration, |
| 185 | + String applicationId, |
| 186 | + PrivateKey privateKey) throws IllegalAccessException, InvocationTargetException { |
| 187 | + JwtBuilder jwtBuilder = Jwts.builder(); |
| 188 | + Object builderObj = jwtBuilder; |
| 189 | + builderObj = setIssuedAtMethod.invoke(builderObj, Date.from(issuedAt)); |
| 190 | + builderObj = setExpirationMethod.invoke(builderObj, Date.from(expiration)); |
| 191 | + builderObj = setIssuerMethod.invoke(builderObj, applicationId); |
| 192 | + builderObj = signWithMethod.invoke(builderObj, privateKey, rs256SignatureAlgorithm); |
| 193 | + builderObj = serializeToJsonMethod.invoke(builderObj, new JacksonSerializer<>()); |
| 194 | + return ((JwtBuilder) builderObj).compact(); |
| 195 | + } |
| 196 | + |
| 197 | + @SuppressWarnings("unchecked") |
| 198 | + private static <T extends Enum<T>> T createEnumInstance(Class<?> type, String name) { |
| 199 | + return Enum.valueOf((Class<T>) type, name); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments