|
| 1 | +package org.vss.auth; |
| 2 | + |
| 3 | +import com.auth0.jwt.JWT; |
| 4 | +import com.auth0.jwt.algorithms.Algorithm; |
| 5 | +import com.auth0.jwt.exceptions.JWTVerificationException; |
| 6 | +import com.auth0.jwt.interfaces.DecodedJWT; |
| 7 | +import com.auth0.jwt.interfaces.JWTVerifier; |
| 8 | +import jakarta.ws.rs.core.HttpHeaders; |
| 9 | +import org.vss.exception.AuthException; |
| 10 | + |
| 11 | +import java.security.KeyFactory; |
| 12 | +import java.security.PublicKey; |
| 13 | +import java.security.interfaces.RSAPublicKey; |
| 14 | +import java.security.spec.X509EncodedKeySpec; |
| 15 | +import java.util.Base64; |
| 16 | + |
| 17 | +// A JWT (https://datatracker.ietf.org/doc/html/rfc7519) based authorizer, |
| 18 | +public class JwtAuthorizer implements Authorizer { |
| 19 | + |
| 20 | + private final PublicKey publicKey; |
| 21 | + private final JWTVerifier verifier; |
| 22 | + |
| 23 | + private static final String BEARER_PREFIX = "Bearer "; |
| 24 | + private static final int MAX_USER_TOKEN_LENGTH = 120; |
| 25 | + |
| 26 | + // `pemFormatRSAPublicKey` is RSA public key used by JWT Auth server for creating signed JWT tokens. |
| 27 | + // Refer to OpenSSL(https://docs.openssl.org/1.1.1/man1/rsa/) docs for generating valid key pairs. |
| 28 | + // Example: |
| 29 | + // * To generate private key, run : `openssl genpkey -algorithm RSA -out private_key.pem -pkeyopt rsa_keygen_bits:2048` |
| 30 | + // * To generate public key, run: `openssl rsa -pubout -in private_key.pem -out public_key.pem` |
| 31 | + public JwtAuthorizer(String pemFormatRSAPublicKey) throws Exception { |
| 32 | + this.publicKey = loadPublicKey(pemFormatRSAPublicKey); |
| 33 | + |
| 34 | + Algorithm algorithm = Algorithm.RSA256((RSAPublicKey) publicKey, null); |
| 35 | + this.verifier = JWT.require(algorithm).build(); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public AuthResponse verify(HttpHeaders headers) throws AuthException { |
| 40 | + |
| 41 | + try { |
| 42 | + String authorizationHeader = headers.getHeaderString(HttpHeaders.AUTHORIZATION); |
| 43 | + if (authorizationHeader == null || !authorizationHeader.startsWith(BEARER_PREFIX)) { |
| 44 | + throw new AuthException("Missing or invalid Authorization header."); |
| 45 | + } |
| 46 | + |
| 47 | + // Extract token by excluding BEARER_PREFIX. |
| 48 | + String token = authorizationHeader.substring(BEARER_PREFIX.length()); |
| 49 | + |
| 50 | + DecodedJWT jwt = verifier.verify(token); |
| 51 | + |
| 52 | + // Extract the user identity from the token. |
| 53 | + String userToken = jwt.getSubject(); |
| 54 | + |
| 55 | + if (userToken == null || userToken.isBlank()) { |
| 56 | + throw new AuthException("Invalid JWT token."); |
| 57 | + } else if (userToken.length() > MAX_USER_TOKEN_LENGTH) { |
| 58 | + throw new AuthException("UserToken is too long"); |
| 59 | + } |
| 60 | + |
| 61 | + return new AuthResponse(userToken); |
| 62 | + |
| 63 | + } catch (JWTVerificationException e) { |
| 64 | + throw new AuthException("Invalid JWT token."); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private PublicKey loadPublicKey(String pemFormatRSAPublicKey) throws Exception { |
| 69 | + String key = pemFormatRSAPublicKey |
| 70 | + .replaceAll("\\n", "") |
| 71 | + .replace("-----BEGIN PUBLIC KEY-----", "") |
| 72 | + .replace("-----END PUBLIC KEY-----", ""); |
| 73 | + |
| 74 | + byte[] keyBytes = Base64.getDecoder().decode(key); |
| 75 | + |
| 76 | + X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes); |
| 77 | + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); |
| 78 | + return keyFactory.generatePublic(spec); |
| 79 | + } |
| 80 | +} |
0 commit comments