Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#820] add checkstyle to streampipes-security-jwt #868

Merged
merged 1 commit into from
Dec 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion streampipes-security-jwt/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -46,4 +47,12 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,7 +51,8 @@ public static String makeJwtToken(String subject,
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);

Expand All @@ -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);
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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));
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down