From 24980676aec520c5e9840c8570c434ce7d895d06 Mon Sep 17 00:00:00 2001 From: Philipp Zehnder Date: Wed, 14 Dec 2022 21:29:06 +0100 Subject: [PATCH] [#820] add checkstyle to streampipes-security-jwt --- streampipes-security-jwt/pom.xml | 11 ++++++++++- .../security/jwt/JwtTokenGenerator.java | 17 ++++++++++------- .../streampipes/security/jwt/JwtTokenUtils.java | 8 ++++---- .../security/jwt/JwtTokenValidator.java | 6 +++++- .../streampipes/security/jwt/KeyGenerator.java | 9 ++++++--- .../streampipes/security/jwt/KeyUtils.java | 12 ++++++------ 6 files changed, 41 insertions(+), 22 deletions(-) diff --git a/streampipes-security-jwt/pom.xml b/streampipes-security-jwt/pom.xml index 47bceb934a..15b813718f 100644 --- a/streampipes-security-jwt/pom.xml +++ b/streampipes-security-jwt/pom.xml @@ -16,7 +16,8 @@ ~ limitations under the License. ~ --> - + streampipes-parent org.apache.streampipes @@ -46,4 +47,12 @@ + + + + org.apache.maven.plugins + maven-checkstyle-plugin + + + diff --git a/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenGenerator.java b/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenGenerator.java index 0424cd02c1..45f1109bb9 100644 --- a/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenGenerator.java +++ b/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenGenerator.java @@ -23,6 +23,7 @@ import io.jsonwebtoken.security.Keys; import javax.crypto.SecretKey; + import java.io.IOException; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; @@ -50,7 +51,8 @@ public static String makeJwtToken(String subject, public static String makeJwtToken(String subject, Path keyFilePath, Map claims, - Date expirationDate) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException { + Date expirationDate) + throws NoSuchAlgorithmException, IOException, InvalidKeySpecException { JwtBuilder builder = prepareJwtToken(subject, makeRsaKey(keyFilePath), expirationDate); @@ -71,7 +73,8 @@ private static SecretKey makeHmacKey(String tokenSecret) { return Keys.hmacShaKeyFor(tokenSecret.getBytes(StandardCharsets.UTF_8)); } - private static RSAPrivateKey makeRsaKey(Path keyFilePath) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException { + private static RSAPrivateKey makeRsaKey(Path keyFilePath) + throws NoSuchAlgorithmException, IOException, InvalidKeySpecException { String key = Files.readString(keyFilePath, Charset.defaultCharset()); byte[] decoded = KeyUtils.extractPrivate(key); @@ -85,10 +88,10 @@ private static JwtBuilder prepareJwtToken(String subject, Key key, Date expirationDate) { return Jwts - .builder() - .setSubject(subject) - .setIssuedAt(new Date()) - .setExpiration(expirationDate) - .signWith(key); + .builder() + .setSubject(subject) + .setIssuedAt(new Date()) + .setExpiration(expirationDate) + .signWith(key); } } diff --git a/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenUtils.java b/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenUtils.java index 1ea45ce8f6..ad48bd2047 100644 --- a/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenUtils.java +++ b/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenUtils.java @@ -44,14 +44,14 @@ public static Claims getClaimsFromToken(String token, public static JwtParser jwtParser(String tokenSecret) { return Jwts.parserBuilder() - .setSigningKey(tokenSecret.getBytes(StandardCharsets.UTF_8)) - .build(); + .setSigningKey(tokenSecret.getBytes(StandardCharsets.UTF_8)) + .build(); } public static JwtParser jwtParser(SigningKeyResolver resolver) { return Jwts.parserBuilder() - .setSigningKeyResolver(resolver) - .build(); + .setSigningKeyResolver(resolver) + .build(); } } diff --git a/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenValidator.java b/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenValidator.java index ccd6df0772..de74ba8b5b 100644 --- a/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenValidator.java +++ b/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/JwtTokenValidator.java @@ -18,7 +18,11 @@ package org.apache.streampipes.security.jwt; -import io.jsonwebtoken.*; +import io.jsonwebtoken.ExpiredJwtException; +import io.jsonwebtoken.JwtParser; +import io.jsonwebtoken.MalformedJwtException; +import io.jsonwebtoken.SigningKeyResolver; +import io.jsonwebtoken.UnsupportedJwtException; import io.jsonwebtoken.security.WeakKeyException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; diff --git a/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/KeyGenerator.java b/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/KeyGenerator.java index bbd3f64ad8..ceda5a0797 100644 --- a/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/KeyGenerator.java +++ b/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/KeyGenerator.java @@ -18,8 +18,9 @@ package org.apache.streampipes.security.jwt; -import io.jsonwebtoken.security.Keys; import org.apache.streampipes.commons.constants.Envs; + +import io.jsonwebtoken.security.Keys; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -36,7 +37,7 @@ public class KeyGenerator { - private static Logger LOG = LoggerFactory.getLogger(KeyGenerator.class); + private static final Logger LOG = LoggerFactory.getLogger(KeyGenerator.class); public Key makeKeyForSecret(String tokenSecret) { return Keys.hmacShaKeyFor(tokenSecret.getBytes(StandardCharsets.UTF_8)); @@ -54,7 +55,9 @@ public Key makeKeyForSecret(String alg, try { return makeKeyForRsa(pkContent); } catch (IOException | InvalidKeySpecException | NoSuchAlgorithmException e) { - LOG.error("Could not properly create the provided key, defaulting to an HMAC token, which will almost certainly lead to problems"); + LOG.error( + "Could not properly create the provided key, defaulting to an HMAC token, " + + "which will almost certainly lead to problems"); return makeKeyForSecret(tokenSecret); } } else { diff --git a/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/KeyUtils.java b/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/KeyUtils.java index 2cf49b107b..e3b7b70996 100644 --- a/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/KeyUtils.java +++ b/streampipes-security-jwt/src/main/java/org/apache/streampipes/security/jwt/KeyUtils.java @@ -24,16 +24,16 @@ public class KeyUtils { public static byte[] extractPrivate(String key) { return decode(key - .replace("-----BEGIN PRIVATE KEY-----", "") - .replaceAll(System.lineSeparator(), "") - .replace("-----END PRIVATE KEY-----", "")); + .replace("-----BEGIN PRIVATE KEY-----", "") + .replaceAll(System.lineSeparator(), "") + .replace("-----END PRIVATE KEY-----", "")); } public static byte[] extractPublic(String key) { return decode(key - .replace("-----BEGIN PUBLIC KEY-----", "") - .replaceAll(System.lineSeparator(), "") - .replace("-----END PUBLIC KEY-----", "")); + .replace("-----BEGIN PUBLIC KEY-----", "") + .replaceAll(System.lineSeparator(), "") + .replace("-----END PUBLIC KEY-----", "")); } private static byte[] decode(String key) {