-
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.
Merge pull request #17 from IvanoskiHarmonia/qus-2-user-authenticatio…
…n-via-google Google oauth
- Loading branch information
Showing
13 changed files
with
223 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,3 +35,6 @@ out/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
# oauth keys | ||
src/main/resources/oauth2.txt |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/quizapp/service/QuizServiceConstants.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,10 @@ | ||
package com.quizapp.service; | ||
|
||
public final class QuizServiceConstants { | ||
|
||
private QuizServiceConstants() { | ||
throw new IllegalStateException("Utility class"); | ||
} | ||
|
||
public static final String SECRET_KEY = System.getenv("SECRET_KEY_QUIZ_SERVICE"); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/quizapp/service/data/configuration/AppConfig.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,24 @@ | ||
package com.quizapp.service.data.configuration; | ||
|
||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.cors.CorsConfiguration; | ||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource; | ||
import org.springframework.web.filter.CorsFilter; | ||
|
||
@Configuration | ||
public class AppConfig { | ||
|
||
@Bean | ||
public CorsFilter corsFilter() { | ||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); | ||
CorsConfiguration config = new CorsConfiguration(); | ||
config.setAllowCredentials(true); | ||
config.addAllowedOrigin("http://localhost:3000"); | ||
config.addAllowedHeader("*"); | ||
config.addAllowedMethod("*"); | ||
|
||
source.registerCorsConfiguration("/**", config); | ||
return new CorsFilter(source); | ||
} | ||
} |
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
70 changes: 70 additions & 0 deletions
70
src/main/java/com/quizapp/service/data/controller/SessionController.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,70 @@ | ||
package com.quizapp.service.data.controller; | ||
|
||
import com.quizapp.service.QuizServiceConstants; | ||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwts; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.util.Date; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@CrossOrigin(origins = "http://localhost:3000", allowCredentials = "true") | ||
@RestController | ||
@RequestMapping("/api/session") | ||
public class SessionController { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(SessionController.class); | ||
|
||
@GetMapping("/validate") | ||
public ResponseEntity<String> validateSession(HttpServletRequest request) { | ||
boolean isValidSession = false; | ||
String token = null; | ||
|
||
Cookie[] cookies = request.getCookies(); | ||
if (cookies != null) { | ||
for (Cookie cookie : cookies) { | ||
if ("authToken".equals(cookie.getName())) { | ||
token = cookie.getValue(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
if (token != null && !token.isEmpty()) { | ||
try { | ||
Claims claims = | ||
Jwts.parser() | ||
.setSigningKey(QuizServiceConstants.SECRET_KEY) | ||
.parseClaimsJws(token) | ||
.getBody(); | ||
|
||
if (claims.getExpiration().after(new Date())) { | ||
isValidSession = true; | ||
} | ||
} catch (Exception e) { | ||
logger.error("Error validating session: {}", e.getMessage()); | ||
} | ||
} | ||
|
||
return ResponseEntity.ok().body("{\"isValidSession\":" + isValidSession + "}"); | ||
} | ||
|
||
@PostMapping("/logout") | ||
public ResponseEntity<String> logout(HttpServletResponse response) { | ||
Cookie cookie = new Cookie("authToken", null); | ||
cookie.setPath("/"); | ||
cookie.setHttpOnly(true); | ||
cookie.setMaxAge(0); | ||
response.addCookie(cookie); | ||
|
||
return ResponseEntity.ok().body("{\"message\":\"Successfully logged out.\"}"); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
src/main/java/com/quizapp/service/data/controller/UserController.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,48 @@ | ||
package com.quizapp.service.data.controller; | ||
|
||
import com.quizapp.service.data.dto.UserDataDTO; | ||
import com.quizapp.service.data.entity.User; | ||
import com.quizapp.service.data.service.UserService; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import jakarta.transaction.Transactional; | ||
import jakarta.validation.constraints.NotNull; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.CrossOrigin; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@CrossOrigin(origins = "http://localhost:3000", allowCredentials = "true") | ||
@RestController | ||
@RequestMapping("/api/users") | ||
public class UserController { | ||
|
||
private final UserService userService; | ||
|
||
@Autowired | ||
public UserController(UserService userService) { | ||
this.userService = userService; | ||
} | ||
|
||
@Transactional | ||
@PostMapping("/login") | ||
public ResponseEntity<User> createUser( | ||
@NotNull @RequestBody UserDataDTO userData, HttpServletResponse response) { | ||
|
||
User user = | ||
userService.createOrUpdateUser( | ||
userData.getEmail(), userData.getToken(), userData.getExpiresAt()); | ||
|
||
Cookie cookie = new Cookie("token", userData.getToken()); | ||
cookie.setHttpOnly(true); | ||
cookie.setMaxAge(60 * 60 * 24 * 365); // 1 year | ||
cookie.setSecure(true); | ||
cookie.setPath("/"); | ||
response.addCookie(cookie); | ||
|
||
return ResponseEntity.ok(user); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/quizapp/service/data/dto/UserDataDTO.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,15 @@ | ||
package com.quizapp.service.data.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public class UserDataDTO { | ||
|
||
private String token; | ||
private Long expiresAt; | ||
private String email; | ||
} |
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
10 changes: 10 additions & 0 deletions
10
src/main/java/com/quizapp/service/data/repository/UserRepository.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,10 @@ | ||
package com.quizapp.service.data.repository; | ||
|
||
import com.quizapp.service.data.entity.User; | ||
import java.util.Optional; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface UserRepository extends JpaRepository<User, Long> { | ||
|
||
Optional<User> findByEmail(String email); | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/quizapp/service/data/service/UserService.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,33 @@ | ||
package com.quizapp.service.data.service; | ||
|
||
import com.quizapp.service.data.entity.User; | ||
import com.quizapp.service.data.repository.UserRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UserService { | ||
private final UserRepository userRepository; | ||
|
||
@Autowired | ||
public UserService(UserRepository userRepository) { | ||
this.userRepository = userRepository; | ||
} | ||
|
||
public User createOrUpdateUser(String email, String token, Long expiresAt) { | ||
User existingUser = userRepository.findByEmail(email).orElse(null); | ||
|
||
if (existingUser != null) { | ||
existingUser.setToken(token); | ||
existingUser.setExpiresAt(expiresAt); | ||
return userRepository.save(existingUser); | ||
} else { | ||
User user = new User(); | ||
user.setEmail(email); | ||
user.setToken(token); | ||
user.setExpiresAt(expiresAt); | ||
|
||
return userRepository.save(user); | ||
} | ||
} | ||
} |