-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3786bd4
commit b7a806f
Showing
13 changed files
with
130 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
...in/java/com/kurttekin/can/job_track/application/service/TurnstileVerificationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.kurttekin.can.job_track.application.service; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
@Service | ||
public class TurnstileVerificationService { | ||
@Value("${turnstile.secret-key}") | ||
private String turnstileSecretKey; | ||
private static final String VERIFY_URL = "https://challenges.cloudflare.com/turnstile/v0/siteverify"; | ||
public boolean verifyToken(String turnstileToken) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
Map<String, String> requestBody = new HashMap<>(); | ||
requestBody.put("secret", turnstileSecretKey); | ||
requestBody.put("response", turnstileToken); | ||
Map response = restTemplate.postForObject(VERIFY_URL, requestBody, Map.class); | ||
return response != null && Boolean.TRUE.equals(response.get("success")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
import com.kurttekin.can.job_track.application.dto.LoginRequest; | ||
import com.kurttekin.can.job_track.application.dto.UserRegistrationRequest; | ||
import com.kurttekin.can.job_track.application.service.EmailService; | ||
import com.kurttekin.can.job_track.application.service.TurnstileVerificationService; | ||
import com.kurttekin.can.job_track.domain.service.UserService; | ||
import com.kurttekin.can.job_track.infrastructure.security.jwt.JwtProvider; | ||
import org.junit.jupiter.api.BeforeEach; | ||
|
@@ -48,10 +49,13 @@ class AuthControllerTest { | |
@Mock | ||
private EmailService emailService; | ||
|
||
@Mock | ||
private TurnstileVerificationService turnstileVerificationService; | ||
|
||
private LoginRequest loginRequest; | ||
private UserRegistrationRequest userRegistrationRequest; | ||
private String token; | ||
|
||
private String turnstileToken; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
|
@@ -60,14 +64,19 @@ public void setUp() { | |
|
||
userRegistrationRequest = new UserRegistrationRequest("testuser", "[email protected]", "testpassword"); | ||
token = "test.jwt.token"; | ||
turnstileToken= "test.jwt.turnstile"; | ||
} | ||
|
||
@Test | ||
public void testLogin_InvalidCredentials() { | ||
// Mock Turnstile verification logic | ||
//when(turnstileVerificationService.verifyToken(anyString())).thenReturn(true); | ||
when(turnstileVerificationService.verifyToken(turnstileToken)).thenReturn(true); | ||
|
||
when(authenticationManager.authenticate(any(UsernamePasswordAuthenticationToken.class))) | ||
.thenThrow(new BadCredentialsException("Invalid credentials")); | ||
|
||
ResponseEntity<?> response = authController.login(loginRequest); | ||
ResponseEntity<?> response = authController.login(loginRequest, turnstileToken); | ||
|
||
assertEquals(HttpStatus.UNAUTHORIZED, response.getStatusCode()); | ||
assertEquals("Invalid credentials", response.getBody()); | ||
|
@@ -82,8 +91,10 @@ public void testRegisterUser_Success() { | |
doNothing().when(userService).registerUser(any(UserRegistrationRequest.class)); | ||
doNothing().when(emailService).sendVerificationEmail(anyString(),anyString(), anyString()); // Mock email sending | ||
|
||
when(turnstileVerificationService.verifyToken(turnstileToken)).thenReturn(true); | ||
|
||
// Call the registerUser method in the controller | ||
ResponseEntity<String> response = authController.registerUser(userRegistrationRequest); | ||
ResponseEntity<String> response = authController.registerUser(userRegistrationRequest, turnstileToken); | ||
|
||
// Check the status and response body | ||
assertEquals(HttpStatus.OK, response.getStatusCode()); | ||
|
@@ -94,7 +105,9 @@ public void testRegisterUser_Success() { | |
public void testRegisterUser_Failure() { | ||
doThrow(new RuntimeException("Registration failed")).when(userService).registerUser(any(UserRegistrationRequest.class)); | ||
|
||
ResponseEntity<String> response = authController.registerUser(userRegistrationRequest); | ||
when(turnstileVerificationService.verifyToken(turnstileToken)).thenReturn(true); | ||
|
||
ResponseEntity<String> response = authController.registerUser(userRegistrationRequest, turnstileToken); | ||
|
||
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); | ||
assertEquals("Registration failed", response.getBody()); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import React, { useState } from 'react'; | ||
import Turnstile from 'react-turnstile'; | ||
import {REACT_APP_TURNSTILE_SITE_KEY} from "../config"; | ||
|
||
const TurnstileWidget = ({ onChange }) => { | ||
const [token, setToken] = useState(null); | ||
|
||
const handleTurnstileChange = (value) => { | ||
setToken(value); | ||
if (onChange) { | ||
onChange(value); // Pass token back to parent component | ||
} | ||
}; | ||
|
||
return ( | ||
<div> | ||
<Turnstile | ||
sitekey={REACT_APP_TURNSTILE_SITE_KEY} | ||
onChange={handleTurnstileChange} | ||
/> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TurnstileWidget; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export const REACT_APP_BACKEND_URL = process.env.REACT_APP_BACKEND_URL+'/api'; | ||
export const REACT_APP_BACKEND_URL = process.env.REACT_APP_BACKEND_URL+'/api'; | ||
export const REACT_APP_TURNSTILE_SITE_KEY = process.env.REACT_APP_TURNSTILE_SITE_KEY; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters