You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@streampipes.apache.org by ze...@apache.org on 2022/12/14 20:30:03 UTC

[streampipes] 01/01: [#820] add checkstyle to streampipes-security-jwt

This is an automated email from the ASF dual-hosted git repository.

zehnder pushed a commit to branch activate-checkstyle
in repository https://gitbox.apache.org/repos/asf/streampipes.git

commit 24980676aec520c5e9840c8570c434ce7d895d06
Author: Philipp Zehnder <te...@users.noreply.github.com>
AuthorDate: Wed Dec 14 21:29:06 2022 +0100

    [#820] add checkstyle to streampipes-security-jwt
---
 streampipes-security-jwt/pom.xml                        | 11 ++++++++++-
 .../streampipes/security/jwt/JwtTokenGenerator.java     | 17 ++++++++++-------
 .../apache/streampipes/security/jwt/JwtTokenUtils.java  |  8 ++++----
 .../streampipes/security/jwt/JwtTokenValidator.java     |  6 +++++-
 .../apache/streampipes/security/jwt/KeyGenerator.java   |  9 ++++++---
 .../org/apache/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 47bceb934..15b813718 100644
--- a/streampipes-security-jwt/pom.xml
+++ b/streampipes-security-jwt/pom.xml
@@ -16,7 +16,8 @@
   ~ limitations under the License.
   ~
   -->
-<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <parent>
         <artifactId>streampipes-parent</artifactId>
         <groupId>org.apache.streampipes</groupId>
@@ -46,4 +47,12 @@
         </dependency>
     </dependencies>
 
+    <build>
+        <plugins>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-checkstyle-plugin</artifactId>
+            </plugin>
+        </plugins>
+    </build>
 </project>
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 0424cd02c..45f1109bb 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.Jwts;
 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 class JwtTokenGenerator {
   public static String makeJwtToken(String subject,
                                     Path keyFilePath,
                                     Map<String, Object> claims,
-                                    Date expirationDate) throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
+                                    Date expirationDate)
+      throws NoSuchAlgorithmException, IOException, InvalidKeySpecException {
 
     JwtBuilder builder = prepareJwtToken(subject, makeRsaKey(keyFilePath), expirationDate);
 
@@ -71,7 +73,8 @@ public class JwtTokenGenerator {
     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 @@ public class JwtTokenGenerator {
                                             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 1ea45ce8f..ad48bd204 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 class JwtTokenUtils {
 
   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 ccd6df077..de74ba8b5 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 bbd3f64ad..ceda5a079 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 @@ import java.security.spec.X509EncodedKeySpec;
 
 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 class KeyGenerator {
       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 2cf49b107..e3b7b7099 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) {