diff --git a/pom.xml b/pom.xml index 8bfb64c..1c2f85d 100644 --- a/pom.xml +++ b/pom.xml @@ -1,66 +1,69 @@ - - - 4.0.0 - - edu.uoc.elc.lti - lti-1.3-jwt - 0.0.2 - jar - - lti-1.3-jwt - Library with JWT implementations of LTI's JWT interfaces - - - UTF-8 - UTF-8 - 1.8 - - - - - edu.uoc.elc.lti - lti-1.3-core - 0.0.2 - - - com.auth0 - jwks-rsa - 0.6.1 - - - io.jsonwebtoken - jjwt-api - 0.10.5 - - - io.jsonwebtoken - jjwt-impl - 0.10.5 - compile - - - io.jsonwebtoken - jjwt-jackson - 0.10.5 - compile - - - org.projectlombok - lombok - 1.16.10 - provided - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - 1.8 - 1.8 - - - - - + + + 4.0.0 + + edu.uoc.elc.lti + lti-1.3-jwt + 0.0.2 + jar + + lti-1.3-jwt + Library with JWT implementations of LTI's JWT interfaces + + + UTF-8 + UTF-8 + 1.8 + + + + + edu.uoc.elc.lti + lti-1.3-core + 0.0.2 + + + com.auth0 + jwks-rsa + 0.6.1 + + + io.jsonwebtoken + jjwt-api + 0.10.5 + + + io.jsonwebtoken + jjwt-impl + 0.10.5 + compile + + + io.jsonwebtoken + jjwt-jackson + 0.10.5 + compile + + + org.projectlombok + lombok + 1.18.12 + provided + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.1 + + ${java.version} + ${java.version} + + + + + diff --git a/src/main/java/edu/uoc/lti/jwt/AlgorithmFactory.java b/src/main/java/edu/uoc/lti/jwt/AlgorithmFactory.java index 481d877..3ab62d8 100644 --- a/src/main/java/edu/uoc/lti/jwt/AlgorithmFactory.java +++ b/src/main/java/edu/uoc/lti/jwt/AlgorithmFactory.java @@ -1,59 +1,60 @@ -package edu.uoc.lti.jwt; - -import lombok.Getter; -import sun.security.util.DerInputStream; -import sun.security.util.DerValue; - -import java.io.IOException; -import java.math.BigInteger; -import java.security.GeneralSecurityException; -import java.security.KeyFactory; -import java.security.interfaces.RSAPrivateKey; -import java.security.interfaces.RSAPublicKey; -import java.security.spec.RSAPrivateCrtKeySpec; -import java.security.spec.X509EncodedKeySpec; -import java.util.Base64; - -/** - * @author xaracil@uoc.edu - */ -public class AlgorithmFactory { - private final RSAPublicKey publicKey; - @Getter - private final RSAPrivateKey privateKey; - - public AlgorithmFactory(String publicKey, String privateKey) { - KeyFactory kf; - try { - kf = KeyFactory.getInstance("RSA"); - byte[] encodedPb = Base64.getDecoder().decode(publicKey); - X509EncodedKeySpec keySpecPb = new X509EncodedKeySpec(encodedPb); - this.publicKey = (RSAPublicKey) kf.generatePublic(keySpecPb); - - DerInputStream derReader = new DerInputStream(Base64.getDecoder().decode(privateKey)); - - DerValue[] seq = derReader.getSequence(0); - - if (seq.length < 9) { - throw new GeneralSecurityException("Could not parse a PKCS1 private key."); - } - - // skip version seq[0]; - BigInteger modulus = seq[1].getBigInteger(); - BigInteger publicExp = seq[2].getBigInteger(); - BigInteger privateExp = seq[3].getBigInteger(); - BigInteger prime1 = seq[4].getBigInteger(); - BigInteger prime2 = seq[5].getBigInteger(); - BigInteger exp1 = seq[6].getBigInteger(); - BigInteger exp2 = seq[7].getBigInteger(); - BigInteger crtCoef = seq[8].getBigInteger(); - - RSAPrivateCrtKeySpec keySpecPv = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, exp1, exp2, crtCoef); - - this.privateKey = (RSAPrivateKey) kf.generatePrivate(keySpecPv); - - } catch (GeneralSecurityException | IOException e) { - throw new BadToolProviderConfigurationException(e); - } - } -} +package edu.uoc.lti.jwt; + +import java.io.IOException; +import java.math.BigInteger; +import java.security.GeneralSecurityException; +import java.security.KeyFactory; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.RSAPrivateCrtKeySpec; +import java.security.spec.X509EncodedKeySpec; +import java.util.Base64; + +import lombok.Getter; +import sun.security.util.DerInputStream; +import sun.security.util.DerValue; + +/** + * @author xaracil@uoc.edu + */ +@Getter +public class AlgorithmFactory { + private final RSAPublicKey publicKey; + private final RSAPrivateKey privateKey; + + public AlgorithmFactory(String publicKey, String privateKey) { + KeyFactory kf; + try { + kf = KeyFactory.getInstance("RSA"); + byte[] encodedPb = Base64.getDecoder().decode(publicKey); + X509EncodedKeySpec keySpecPb = new X509EncodedKeySpec(encodedPb); + this.publicKey = (RSAPublicKey) kf.generatePublic(keySpecPb); + + DerInputStream derReader = new DerInputStream(Base64.getDecoder().decode(privateKey)); + + DerValue[] seq = derReader.getSequence(0); + + if (seq.length < 9) { + throw new GeneralSecurityException("Could not parse a PKCS1 private key."); + } + + // skip version seq[0]; + BigInteger modulus = seq[1].getBigInteger(); + BigInteger publicExp = seq[2].getBigInteger(); + BigInteger privateExp = seq[3].getBigInteger(); + BigInteger prime1 = seq[4].getBigInteger(); + BigInteger prime2 = seq[5].getBigInteger(); + BigInteger exp1 = seq[6].getBigInteger(); + BigInteger exp2 = seq[7].getBigInteger(); + BigInteger crtCoef = seq[8].getBigInteger(); + + RSAPrivateCrtKeySpec keySpecPv = new RSAPrivateCrtKeySpec(modulus, publicExp, privateExp, prime1, prime2, + exp1, exp2, crtCoef); + + this.privateKey = (RSAPrivateKey) kf.generatePrivate(keySpecPv); + + } catch (GeneralSecurityException | IOException e) { + throw new BadToolProviderConfigurationException(e); + } + } +} diff --git a/src/main/java/edu/uoc/lti/jwt/LtiSigningKeyResolver.java b/src/main/java/edu/uoc/lti/jwt/LtiSigningKeyResolver.java index ab1cb6b..68f0cd7 100644 --- a/src/main/java/edu/uoc/lti/jwt/LtiSigningKeyResolver.java +++ b/src/main/java/edu/uoc/lti/jwt/LtiSigningKeyResolver.java @@ -1,43 +1,44 @@ -package edu.uoc.lti.jwt; - -import com.auth0.jwk.Jwk; -import com.auth0.jwk.JwkException; -import com.auth0.jwk.JwkProvider; -import com.auth0.jwk.UrlJwkProvider; -import io.jsonwebtoken.Claims; -import io.jsonwebtoken.JwsHeader; -import io.jsonwebtoken.SigningKeyResolverAdapter; -import lombok.RequiredArgsConstructor; - -import java.net.MalformedURLException; -import java.net.URL; -import java.security.Key; - -/** - * @author xaracil@uoc.edu - */ -@RequiredArgsConstructor -public class LtiSigningKeyResolver extends SigningKeyResolverAdapter { - private final String keysetUrl; - - @Override - public Key resolveSigningKey(JwsHeader header, Claims claims) { - String keyId = header.getKeyId(); - - if (keyId == null) { - return null; - } - - Key key = null; - try { - JwkProvider provider = new UrlJwkProvider(new URL(keysetUrl)); - Jwk jwk = provider.get(keyId); - key = jwk.getPublicKey(); - } catch (MalformedURLException e) { - e.printStackTrace(); - } catch (JwkException e) { - e.printStackTrace(); - } - return key; - } -} +package edu.uoc.lti.jwt; + +import java.net.MalformedURLException; +import java.net.URL; +import java.security.Key; + +import com.auth0.jwk.Jwk; +import com.auth0.jwk.JwkException; +import com.auth0.jwk.JwkProvider; +import com.auth0.jwk.UrlJwkProvider; + +import io.jsonwebtoken.Claims; +import io.jsonwebtoken.JwsHeader; +import io.jsonwebtoken.SigningKeyResolverAdapter; +import lombok.RequiredArgsConstructor; +import lombok.extern.java.Log; + +/** + * @author xaracil@uoc.edu + */ +@Log +@RequiredArgsConstructor +public class LtiSigningKeyResolver extends SigningKeyResolverAdapter { + private final String keysetUrl; + + @Override + public Key resolveSigningKey(JwsHeader header, Claims claims) { + String keyId = header.getKeyId(); + + if (keyId == null) { + return null; + } + + Key key = null; + try { + JwkProvider provider = new UrlJwkProvider(new URL(keysetUrl)); + Jwk jwk = provider.get(keyId); + key = jwk.getPublicKey(); + } catch (MalformedURLException | JwkException e) { + log.warning("Signing key cannot be resolved: " + e.getMessage()); + } + return key; + } +} diff --git a/src/main/java/edu/uoc/lti/jwt/claims/JWSClaimAccessor.java b/src/main/java/edu/uoc/lti/jwt/claims/JWSClaimAccessor.java index 062b36e..e991393 100644 --- a/src/main/java/edu/uoc/lti/jwt/claims/JWSClaimAccessor.java +++ b/src/main/java/edu/uoc/lti/jwt/claims/JWSClaimAccessor.java @@ -1,5 +1,7 @@ package edu.uoc.lti.jwt.claims; +import java.util.Date; + import com.fasterxml.jackson.databind.ObjectMapper; import edu.uoc.lti.claims.ClaimAccessor; @@ -9,8 +11,6 @@ import io.jsonwebtoken.Jws; import io.jsonwebtoken.Jwts; -import java.util.Date; - /** * @author xaracil@uoc.edu */ diff --git a/src/main/java/edu/uoc/lti/jwt/client/JWSClientCredentialsTokenBuilder.java b/src/main/java/edu/uoc/lti/jwt/client/JWSClientCredentialsTokenBuilder.java index ee27159..1dc5204 100644 --- a/src/main/java/edu/uoc/lti/jwt/client/JWSClientCredentialsTokenBuilder.java +++ b/src/main/java/edu/uoc/lti/jwt/client/JWSClientCredentialsTokenBuilder.java @@ -1,14 +1,13 @@ package edu.uoc.lti.jwt.client; -import io.jsonwebtoken.Jwts; -import lombok.RequiredArgsConstructor; - import java.util.Date; import java.util.UUID; import edu.uoc.lti.clientcredentials.ClientCredentialsRequest; import edu.uoc.lti.clientcredentials.ClientCredentialsTokenBuilder; import edu.uoc.lti.jwt.AlgorithmFactory; +import io.jsonwebtoken.Jwts; +import lombok.RequiredArgsConstructor; /** * @author xaracil@uoc.edu diff --git a/src/main/java/edu/uoc/lti/jwt/deeplink/JWSTokenBuilder.java b/src/main/java/edu/uoc/lti/jwt/deeplink/JWSTokenBuilder.java index b315528..bea8e35 100644 --- a/src/main/java/edu/uoc/lti/jwt/deeplink/JWSTokenBuilder.java +++ b/src/main/java/edu/uoc/lti/jwt/deeplink/JWSTokenBuilder.java @@ -1,16 +1,16 @@ package edu.uoc.lti.jwt.deeplink; +import java.util.Date; + +import edu.uoc.lti.ResponseMessageTypeEnum; import edu.uoc.lti.claims.ClaimsEnum; import edu.uoc.lti.deeplink.DeepLinkingResponse; import edu.uoc.lti.deeplink.DeepLinkingTokenBuilder; import edu.uoc.lti.jwt.AlgorithmFactory; -import edu.uoc.lti.ResponseMessageTypeEnum; import io.jsonwebtoken.JwtBuilder; import io.jsonwebtoken.Jwts; import lombok.RequiredArgsConstructor; -import java.util.Date; - /** * @author xaracil@uoc.edu */