|
| 1 | +package io.github.two_rk_dev.pointeurback.controller; |
| 2 | + |
| 3 | +import io.github.two_rk_dev.pointeurback.dto.LoggedInDTO; |
| 4 | +import io.github.two_rk_dev.pointeurback.dto.LoginRequestDTO; |
| 5 | +import io.github.two_rk_dev.pointeurback.dto.LoginResponseDTO; |
| 6 | +import io.github.two_rk_dev.pointeurback.dto.UserDTO; |
| 7 | +import io.github.two_rk_dev.pointeurback.service.AuthService; |
| 8 | +import jakarta.validation.Valid; |
| 9 | +import lombok.RequiredArgsConstructor; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | +import org.springframework.http.HttpHeaders; |
| 12 | +import org.springframework.http.MediaType; |
| 13 | +import org.springframework.http.ResponseCookie; |
| 14 | +import org.springframework.http.ResponseEntity; |
| 15 | +import org.springframework.security.authentication.BadCredentialsException; |
| 16 | +import org.springframework.security.core.GrantedAuthority; |
| 17 | +import org.springframework.security.core.annotation.AuthenticationPrincipal; |
| 18 | +import org.springframework.security.core.userdetails.UserDetails; |
| 19 | +import org.springframework.web.bind.annotation.*; |
| 20 | + |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +@RestController |
| 24 | +@RequestMapping("/auth") |
| 25 | +@RequiredArgsConstructor |
| 26 | +class AuthController { |
| 27 | + private final AuthService authService; |
| 28 | + |
| 29 | + private static @NotNull String buildDeviceIdCookie(@NotNull LoggedInDTO loggedInDTO) { |
| 30 | + return ResponseCookie.from("device_id", loggedInDTO.refreshToken().deviceId()) |
| 31 | + .sameSite("Strict") |
| 32 | + .path("/") |
| 33 | + .maxAge(Integer.MAX_VALUE) |
| 34 | + .httpOnly(true) |
| 35 | + .secure(loggedInDTO.refreshToken().secure()) |
| 36 | + .build().toString(); |
| 37 | + } |
| 38 | + |
| 39 | + private static @NotNull String buildRefreshTokenCookie(@NotNull LoggedInDTO loggedInDTO) { |
| 40 | + return ResponseCookie.from("refresh_token", loggedInDTO.refreshToken().token()) |
| 41 | + .sameSite("Strict") |
| 42 | + .path("/") |
| 43 | + .maxAge(loggedInDTO.refreshToken().maxAge()) |
| 44 | + .httpOnly(true) |
| 45 | + .secure(loggedInDTO.refreshToken().secure()) |
| 46 | + .build().toString(); |
| 47 | + } |
| 48 | + |
| 49 | + @PostMapping("/login") |
| 50 | + public ResponseEntity<LoginResponseDTO> login( |
| 51 | + @Valid @RequestBody LoginRequestDTO dto, |
| 52 | + @CookieValue(name = "device_id", required = false) String deviceId) { |
| 53 | + |
| 54 | + LoggedInDTO loggedInDTO = authService.login(dto, deviceId); |
| 55 | + List<String> cookies = List.of(buildRefreshTokenCookie(loggedInDTO), buildDeviceIdCookie(loggedInDTO)); |
| 56 | + return ResponseEntity.ok() |
| 57 | + .contentType(MediaType.APPLICATION_JSON) |
| 58 | + .headers(headers -> headers.addAll(HttpHeaders.SET_COOKIE, cookies)) |
| 59 | + .body(loggedInDTO.responseDTO()); |
| 60 | + } |
| 61 | + |
| 62 | + @GetMapping("/me") |
| 63 | + public ResponseEntity<UserDTO> me(@AuthenticationPrincipal UserDetails userDetails) { |
| 64 | + String role = userDetails.getAuthorities().stream() |
| 65 | + .map(GrantedAuthority::getAuthority) |
| 66 | + .filter(authority -> authority.startsWith("ROLE_")) |
| 67 | + .map(authority -> authority.substring(5).toLowerCase()) |
| 68 | + .findFirst().orElse(null); |
| 69 | + return ResponseEntity.ok(new UserDTO(userDetails.getUsername(), role)); |
| 70 | + } |
| 71 | + |
| 72 | + @PostMapping("/refresh") |
| 73 | + public ResponseEntity<LoginResponseDTO> refreshToken( |
| 74 | + @CookieValue(value = "device_id", required = false) String deviceId, |
| 75 | + @CookieValue(value = "refresh_token", required = false) String refreshToken) { |
| 76 | + |
| 77 | + if (deviceId == null || refreshToken == null) throw new BadCredentialsException("Insufficient credentials"); |
| 78 | + LoggedInDTO refreshedSession = authService.refreshSession(deviceId, refreshToken); |
| 79 | + return ResponseEntity.ok() |
| 80 | + .contentType(MediaType.APPLICATION_JSON) |
| 81 | + .headers(headers -> headers.set("Set-Cookie", buildRefreshTokenCookie(refreshedSession))) |
| 82 | + .body(refreshedSession.responseDTO()); |
| 83 | + } |
| 84 | + |
| 85 | + @PostMapping("/logout") |
| 86 | + public ResponseEntity<Void> logout( |
| 87 | + @CookieValue(value = "device_id", required = false) String deviceId, |
| 88 | + @CookieValue(value = "refresh_token", required = false) String refreshToken) { |
| 89 | + |
| 90 | + if (deviceId == null || refreshToken == null) return ResponseEntity.noContent().build(); |
| 91 | + authService.logout(deviceId, refreshToken); |
| 92 | + ResponseCookie cookie = ResponseCookie.from("refresh_token", "") |
| 93 | + .sameSite("Strict") |
| 94 | + .path("/") |
| 95 | + .maxAge(0) |
| 96 | + .httpOnly(true) |
| 97 | + .build(); |
| 98 | + return ResponseEntity |
| 99 | + .noContent() |
| 100 | + .headers(headers -> headers.set("Set-Cookie", cookie.toString())) |
| 101 | + .build(); |
| 102 | + } |
| 103 | +} |
0 commit comments