Skip to content

Commit

Permalink
Merge pull request #22 from alessandro-candon/feature/refactor2
Browse files Browse the repository at this point in the history
feat(refactor): use sonarlint and splotless to lint files
  • Loading branch information
alessandro-candon authored May 28, 2024
2 parents 0f712c5 + 1c7f65a commit 2956d05
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> headers) {

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

Expand All @@ -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");
}
}

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

0 comments on commit 2956d05

Please sign in to comment.