From 1c7f65a7e72c5f505702e2967708d54684f006c9 Mon Sep 17 00:00:00 2001 From: ACANDO14 Date: Tue, 28 May 2024 15:03:14 +0200 Subject: [PATCH] feat(refactor): use sonarlint and splotless to lint files --- .../fakeoauth2/FakeOAuth2Application.java | 3 ++- .../fakeoauth2/controller/OAuthController.java | 9 +++++---- .../fakeoauth2/controller/UserInfoController.java | 11 ++++++++--- .../fakeoauth2/controller/UtilController.java | 7 +++++-- .../fakeoauth2/exception/IORuntimeException.java | 8 ++++++++ .../NotSupportedConfigurationRuntimeException.java | 8 ++++++++ .../fakeoauth2/service/IKeyService.java | 6 +++--- .../fakeoauth2/service/JwtService.java | 10 +++++++--- .../fakeoauth2/service/RSAKeyService.java | 9 +++++---- .../alessandrocandon/fakeoauth2/util/FileUtil.java | 6 +++++- 10 files changed, 56 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/alessandrocandon/fakeoauth2/exception/IORuntimeException.java create mode 100644 src/main/java/com/alessandrocandon/fakeoauth2/exception/NotSupportedConfigurationRuntimeException.java diff --git a/src/main/java/com/alessandrocandon/fakeoauth2/FakeOAuth2Application.java b/src/main/java/com/alessandrocandon/fakeoauth2/FakeOAuth2Application.java index ccfabef..29156b1 100644 --- a/src/main/java/com/alessandrocandon/fakeoauth2/FakeOAuth2Application.java +++ b/src/main/java/com/alessandrocandon/fakeoauth2/FakeOAuth2Application.java @@ -2,6 +2,7 @@ package com.alessandrocandon.fakeoauth2; import com.alessandrocandon.fakeoauth2.dictionary.AllowedAlgorithm; +import com.alessandrocandon.fakeoauth2.exception.NotSupportedConfigurationRuntimeException; import com.alessandrocandon.fakeoauth2.service.IKeyService; import com.alessandrocandon.fakeoauth2.service.RSAKeyService; import com.alessandrocandon.fakeoauth2.service.UserService; @@ -48,7 +49,7 @@ public IKeyService istantiateIKeyService() throws NoSuchAlgorithmException { case AllowedAlgorithm.RSA: return new RSAKeyService(appProperties); default: - throw new RuntimeException("This algorithm is not supported"); + throw new NotSupportedConfigurationRuntimeException("This algorithm is not supported"); } } } diff --git a/src/main/java/com/alessandrocandon/fakeoauth2/controller/OAuthController.java b/src/main/java/com/alessandrocandon/fakeoauth2/controller/OAuthController.java index 4b3c067..67ac169 100644 --- a/src/main/java/com/alessandrocandon/fakeoauth2/controller/OAuthController.java +++ b/src/main/java/com/alessandrocandon/fakeoauth2/controller/OAuthController.java @@ -5,17 +5,18 @@ import com.alessandrocandon.fakeoauth2.service.JwtService; import com.alessandrocandon.fakeoauth2.util.FileUtil; import java.util.Map; -import org.slf4j.Logger; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.servlet.view.RedirectView; import org.springframework.web.util.UriComponentsBuilder; @RestController public class OAuthController { - private static final Logger log = org.slf4j.LoggerFactory.getLogger(OAuthController.class); - @Autowired private JwtService jwtService; + private final JwtService jwtService; + + public OAuthController(JwtService jwtService) { + this.jwtService = jwtService; + } @PostMapping(path = "/as/token.oauth2") public JwtToken token() { diff --git a/src/main/java/com/alessandrocandon/fakeoauth2/controller/UserInfoController.java b/src/main/java/com/alessandrocandon/fakeoauth2/controller/UserInfoController.java index 7b09c89..112aa7a 100644 --- a/src/main/java/com/alessandrocandon/fakeoauth2/controller/UserInfoController.java +++ b/src/main/java/com/alessandrocandon/fakeoauth2/controller/UserInfoController.java @@ -5,15 +5,20 @@ import com.alessandrocandon.fakeoauth2.service.UserService; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.web.bind.annotation.*; @RestController public class UserInfoController { - @Autowired private UserService userService; - @Autowired private JwtService jwtService; + private final JwtService jwtService; + + private final UserService userService; + + public UserInfoController(UserService userService, JwtService jwtService) { + this.jwtService = jwtService; + this.userService = userService; + } @PostMapping("/idp/userinfo.openid") public JsonNode get( diff --git a/src/main/java/com/alessandrocandon/fakeoauth2/controller/UtilController.java b/src/main/java/com/alessandrocandon/fakeoauth2/controller/UtilController.java index f7ad084..15c280b 100644 --- a/src/main/java/com/alessandrocandon/fakeoauth2/controller/UtilController.java +++ b/src/main/java/com/alessandrocandon/fakeoauth2/controller/UtilController.java @@ -4,13 +4,16 @@ import com.alessandrocandon.fakeoauth2.service.UserService; import com.fasterxml.jackson.databind.JsonNode; import java.util.Map; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @RestController public class UtilController { - @Autowired private UserService userService; + private UserService userService; + + public UtilController(UserService userService) { + this.userService = userService; + } @PostMapping("/userinfo") public void post(@RequestBody JsonNode rawUser) { diff --git a/src/main/java/com/alessandrocandon/fakeoauth2/exception/IORuntimeException.java b/src/main/java/com/alessandrocandon/fakeoauth2/exception/IORuntimeException.java new file mode 100644 index 0000000..2865faf --- /dev/null +++ b/src/main/java/com/alessandrocandon/fakeoauth2/exception/IORuntimeException.java @@ -0,0 +1,8 @@ +/* Decathlon Italy - Tacos Team(C) 2024 */ +package com.alessandrocandon.fakeoauth2.exception; + +public class IORuntimeException extends RuntimeException { + public IORuntimeException(String message) { + super(message); + } +} diff --git a/src/main/java/com/alessandrocandon/fakeoauth2/exception/NotSupportedConfigurationRuntimeException.java b/src/main/java/com/alessandrocandon/fakeoauth2/exception/NotSupportedConfigurationRuntimeException.java new file mode 100644 index 0000000..72040b5 --- /dev/null +++ b/src/main/java/com/alessandrocandon/fakeoauth2/exception/NotSupportedConfigurationRuntimeException.java @@ -0,0 +1,8 @@ +/* Decathlon Italy - Tacos Team(C) 2024 */ +package com.alessandrocandon.fakeoauth2.exception; + +public class NotSupportedConfigurationRuntimeException extends RuntimeException { + public NotSupportedConfigurationRuntimeException(String message) { + super(message); + } +} diff --git a/src/main/java/com/alessandrocandon/fakeoauth2/service/IKeyService.java b/src/main/java/com/alessandrocandon/fakeoauth2/service/IKeyService.java index df68db8..db9c570 100644 --- a/src/main/java/com/alessandrocandon/fakeoauth2/service/IKeyService.java +++ b/src/main/java/com/alessandrocandon/fakeoauth2/service/IKeyService.java @@ -6,9 +6,9 @@ import java.security.PublicKey; public interface IKeyService { - public PrivateKey getPrivate(); + PrivateKey getPrivate(); - public PublicKey getPublic(); + PublicKey getPublic(); - public Algorithm getAlgorithm(); + Algorithm getAlgorithm(); } diff --git a/src/main/java/com/alessandrocandon/fakeoauth2/service/JwtService.java b/src/main/java/com/alessandrocandon/fakeoauth2/service/JwtService.java index fc4bf77..0a155c1 100644 --- a/src/main/java/com/alessandrocandon/fakeoauth2/service/JwtService.java +++ b/src/main/java/com/alessandrocandon/fakeoauth2/service/JwtService.java @@ -6,15 +6,19 @@ import java.time.Instant; import java.util.Base64; import java.util.Map; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class JwtService { - @Autowired IKeyService iKeyService; + private final IKeyService iKeyService; - @Autowired UserService userService; + private final UserService userService; + + public JwtService(UserService userService, IKeyService iKeyService) { + this.userService = userService; + this.iKeyService = iKeyService; + } public JwtToken getToken(Map headers) { diff --git a/src/main/java/com/alessandrocandon/fakeoauth2/service/RSAKeyService.java b/src/main/java/com/alessandrocandon/fakeoauth2/service/RSAKeyService.java index 15320fb..0b0e9c2 100644 --- a/src/main/java/com/alessandrocandon/fakeoauth2/service/RSAKeyService.java +++ b/src/main/java/com/alessandrocandon/fakeoauth2/service/RSAKeyService.java @@ -4,6 +4,7 @@ import com.alessandrocandon.fakeoauth2.AppProperties; import com.alessandrocandon.fakeoauth2.dictionary.AllowedAlgorithm; import com.alessandrocandon.fakeoauth2.dictionary.AllowedHash; +import com.alessandrocandon.fakeoauth2.exception.NotSupportedConfigurationRuntimeException; import com.alessandrocandon.fakeoauth2.util.FileUtil; import com.auth0.jwt.algorithms.Algorithm; import java.security.KeyFactory; @@ -37,7 +38,7 @@ public Algorithm getAlgorithm() { case AllowedHash.RSA256: return Algorithm.RSA256(this.getPublic(), this.getPrivate()); default: - throw new RuntimeException("Not HASH algorithm allowed"); + throw new NotSupportedConfigurationRuntimeException("Not HASH algorithm allowed"); } } @@ -47,7 +48,7 @@ public RSAPrivateKey getPrivate() { try { return (RSAPrivateKey) kf.generatePrivate(keySpecPKCS8); } catch (InvalidKeySpecException e) { - throw new RuntimeException(e); + throw new NotSupportedConfigurationRuntimeException("No valid private key found"); } } @@ -57,13 +58,13 @@ public RSAPublicKey getPublic() { try { return (RSAPublicKey) kf.generatePublic(keySpecX509); } catch (InvalidKeySpecException e) { - throw new RuntimeException(e); + throw new NotSupportedConfigurationRuntimeException("No valid public key found"); } } private String cleaner(String rawKey) { return rawKey - .replaceAll("\\n", "") + .replace("\n", "") .replace("-----BEGIN PRIVATE KEY-----", "") .replace("-----END PRIVATE KEY-----", "") .replace("-----BEGIN PUBLIC KEY-----", "") diff --git a/src/main/java/com/alessandrocandon/fakeoauth2/util/FileUtil.java b/src/main/java/com/alessandrocandon/fakeoauth2/util/FileUtil.java index 0d0acae..c5992b6 100644 --- a/src/main/java/com/alessandrocandon/fakeoauth2/util/FileUtil.java +++ b/src/main/java/com/alessandrocandon/fakeoauth2/util/FileUtil.java @@ -1,19 +1,23 @@ /* Decathlon Italy - Tacos Team(C) 2024 */ package com.alessandrocandon.fakeoauth2.util; +import com.alessandrocandon.fakeoauth2.exception.IORuntimeException; import java.io.*; import java.nio.charset.StandardCharsets; import org.springframework.core.io.ClassPathResource; import org.springframework.util.FileCopyUtils; public class FileUtil { + + private FileUtil() {} + public static String getResourceFileAsString(String fileName) { try { ClassPathResource resource = new ClassPathResource(fileName); byte[] binaryData = FileCopyUtils.copyToByteArray(resource.getInputStream()); return new String(binaryData, StandardCharsets.UTF_8); } catch (IOException e) { - throw new RuntimeException(e); + throw new IORuntimeException("Cannot Read Resource File: " + fileName); } } }