Skip to content

Commit

Permalink
Fix java code style
Browse files Browse the repository at this point in the history
Signed-off-by: Taras Drozdovskyi <[email protected]>
  • Loading branch information
tdrozdovsky committed Oct 26, 2023
1 parent 748219c commit 0e99e1e
Show file tree
Hide file tree
Showing 91 changed files with 3,336 additions and 1,692 deletions.
10 changes: 4 additions & 6 deletions src/main/java/com/lpvs/LicensePreValidationSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/

package com.lpvs;

import org.springframework.beans.factory.annotation.Value;
Expand All @@ -20,8 +19,7 @@

import com.lpvs.util.LPVSExitHandler;


@SpringBootApplication(scanBasePackages = { "com.lpvs" })
@SpringBootApplication(scanBasePackages = {"com.lpvs"})
@EnableAutoConfiguration
@EnableAsync
public class LicensePreValidationSystem {
Expand All @@ -36,7 +34,8 @@ public LicensePreValidationSystem(@Value("${lpvs.cores:8}") int corePoolSize) {

public static void main(String[] args) {
try {
ApplicationContext applicationContext = SpringApplication.run(LicensePreValidationSystem.class, args);
ApplicationContext applicationContext =
SpringApplication.run(LicensePreValidationSystem.class, args);
exitHandler = applicationContext.getBean(LPVSExitHandler.class);
} catch (IllegalArgumentException e) {
System.err.println("An IllegalArgumentException occurred: " + e.getMessage());
Expand All @@ -45,7 +44,7 @@ public static void main(String[] args) {
}

@Bean("threadPoolTaskExecutor")
public TaskExecutor getAsyncExecutor(){
public TaskExecutor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(corePoolSize);
executor.setThreadNamePrefix("LPVS-ASYNC::");
Expand All @@ -60,4 +59,3 @@ public static void exit(int exitCode) {
}
}
}

50 changes: 30 additions & 20 deletions src/main/java/com/lpvs/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/

package com.lpvs.config;

import com.lpvs.service.OAuthService;
Expand Down Expand Up @@ -47,11 +46,13 @@ public class SecurityConfig {

@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
http
.cors()
http.cors()
.and()
.csrf().disable()
.headers().frameOptions().disable()
.csrf()
.disable()

Check failure

Code scanning / CodeQL

Disabled Spring CSRF protection High

CSRF vulnerability due to protection being disabled.
.headers()
.frameOptions()
.disable()
.and()
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/oauth/logout"))
Expand All @@ -60,26 +61,35 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.clearAuthentication(true)
.and()
.authorizeRequests()
.anyRequest().permitAll()
.anyRequest()
.permitAll()
.and()
.oauth2Login()
.successHandler(new AuthenticationSuccessHandler() {
@Value("${frontend.main-page.url:}")
private String frontendMainPageUrl;
.successHandler(
new AuthenticationSuccessHandler() {
@Value("${frontend.main-page.url:}")
private String frontendMainPageUrl;

private String REDIRECT_URI = frontendMainPageUrl+"/login/callback";
private String REDIRECT_URI = frontendMainPageUrl + "/login/callback";

@Override
public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal();
System.out.println("oAuth2User = " + oAuth2User);
@Override
public void onAuthenticationSuccess(
HttpServletRequest request,
HttpServletResponse response,
Authentication authentication)
throws IOException, ServletException {
OAuth2User oAuth2User = (OAuth2User) authentication.getPrincipal();
System.out.println("oAuth2User = " + oAuth2User);

response.sendRedirect(UriComponentsBuilder.fromUriString(REDIRECT_URI)
.queryParam("accessToken", "accessToken")
.queryParam("refreshToken", "refreshToken")
.build().encode(StandardCharsets.UTF_8).toUriString());
}
})
response.sendRedirect(
UriComponentsBuilder.fromUriString(REDIRECT_URI)
.queryParam("accessToken", "accessToken")
.queryParam("refreshToken", "refreshToken")
.build()
.encode(StandardCharsets.UTF_8)
.toUriString());
}
})
.defaultSuccessUrl(frontendMainPageUrl, true)
.userInfoEndpoint()
.userService(oAuthService);
Expand Down
31 changes: 20 additions & 11 deletions src/main/java/com/lpvs/controller/GitHubWebhooksController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Use of this source code is governed by a MIT license that can be
* found in the LICENSE file.
*/

package com.lpvs.controller;

import com.lpvs.entity.LPVSQueue;
Expand Down Expand Up @@ -34,7 +33,8 @@
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

@RestController @Slf4j
@RestController
@Slf4j
public class GitHubWebhooksController {

private String GITHUB_SECRET;
Expand All @@ -47,19 +47,21 @@ public class GitHubWebhooksController {
*/
@PostConstruct
public void setProps() {
this.GITHUB_SECRET = Optional.ofNullable(this.GITHUB_SECRET).filter(s -> !s.isEmpty())
.orElse(Optional.ofNullable(System.getenv("LPVS_GITHUB_SECRET")).orElse(""));
this.GITHUB_SECRET =
Optional.ofNullable(this.GITHUB_SECRET)
.filter(s -> !s.isEmpty())
.orElse(
Optional.ofNullable(System.getenv("LPVS_GITHUB_SECRET"))
.orElse(""));
if (this.GITHUB_SECRET.isEmpty()) {
log.error("LPVS_GITHUB_SECRET(github.secret) is not set.");
exitHandler.exit(-1);
}
}

@Autowired
private LPVSQueueService queueService;
@Autowired private LPVSQueueService queueService;

@Autowired
private LPVSQueueRepository queueRepository;
@Autowired private LPVSQueueRepository queueRepository;

private LPVSGitHubService gitHubService;

Expand All @@ -70,7 +72,12 @@ public void setProps() {
private static final String ERROR = "Error";
private static final String ALGORITHM = "HmacSHA256";

public GitHubWebhooksController(LPVSQueueService queueService, LPVSGitHubService gitHubService, LPVSQueueRepository queueRepository, @Value("${github.secret:}") String GITHUB_SECRET, LPVSExitHandler exitHandler) {
public GitHubWebhooksController(
LPVSQueueService queueService,
LPVSGitHubService gitHubService,
LPVSQueueRepository queueRepository,
@Value("${github.secret:}") String GITHUB_SECRET,
LPVSExitHandler exitHandler) {
this.queueService = queueService;
this.gitHubService = gitHubService;
this.queueRepository = queueRepository;
Expand All @@ -87,7 +94,9 @@ public GitHubWebhooksController(LPVSQueueService queueService, LPVSGitHubService
* @throws Exception if an error occurs during processing.
*/
@RequestMapping(value = "/webhooks", method = RequestMethod.POST)
public ResponseEntity<LPVSResponseWrapper> gitHubWebhooks(@RequestHeader(SIGNATURE) String signature, @RequestBody String payload) throws Exception {
public ResponseEntity<LPVSResponseWrapper> gitHubWebhooks(
@RequestHeader(SIGNATURE) String signature, @RequestBody String payload)
throws Exception {
log.debug("New GitHub webhook request received");

// if signature is empty return 401
Expand Down Expand Up @@ -127,7 +136,7 @@ public ResponseEntity<LPVSResponseWrapper> gitHubWebhooks(@RequestHeader(SIGNATU
* @throws Exception if an error occurs during signature verification.
*/
public boolean wrongSecret(String signature, String payload) throws Exception {
String lpvsSecret = signature.split("=",2)[1];
String lpvsSecret = signature.split("=", 2)[1];

SecretKeySpec key = new SecretKeySpec(GITHUB_SECRET.getBytes("utf-8"), ALGORITHM);
Mac mac = Mac.getInstance(ALGORITHM);
Expand Down
Loading

0 comments on commit 0e99e1e

Please sign in to comment.