Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spring.backend.auth.presentation;

import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
Expand All @@ -11,12 +12,25 @@
import spring.backend.core.configuration.argumentresolver.ClientIp;
import spring.backend.core.presentation.RestResponse;

import java.time.Duration;

@RestController
@RequestMapping("/v1/oauth/login")
@RequiredArgsConstructor
public class HandleOAuthLoginController {

private final HandleOAuthLoginService handleOAuthLoginService;
private final long ACCESS_EXPIRATION;
private final long REFRESH_EXPIRATION;

public HandleOAuthLoginController(HandleOAuthLoginService handleOAuthLoginService,
@Value("${jwt.access-token-expiry}") long accessTokenExpiry,
@Value("${jwt.refresh-token-expiry}") long refreshTokenExpiry
) {
this.handleOAuthLoginService = handleOAuthLoginService;
this.ACCESS_EXPIRATION = accessTokenExpiry;
this.REFRESH_EXPIRATION = refreshTokenExpiry;
}


@GetMapping("/{providerName}")
public ResponseEntity<RestResponse<LoginUserInfoResponse>> handleOAuthLogin(@RequestParam(value = "code", required = false) String code,
Expand All @@ -26,12 +40,14 @@ public ResponseEntity<RestResponse<LoginUserInfoResponse>> handleOAuthLogin(@Req
.httpOnly(true)
.secure(true)
.sameSite("None")
.maxAge(Duration.ofSeconds(ACCESS_EXPIRATION))
.path("/")
.build();
ResponseCookie refreshTokenCookie = ResponseCookie.from("refresh_token", loginResponse.refreshToken())
.httpOnly(true)
.secure(true)
.sameSite("None")
.maxAge(Duration.ofDays(REFRESH_EXPIRATION))
.path("/")
.build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.maxmind.geoip2.exception.GeoIp2Exception;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
Expand All @@ -17,15 +18,26 @@
import spring.backend.core.presentation.RestResponse;

import java.io.IOException;
import java.time.Duration;

import static org.springframework.http.ResponseCookie.from;

@RestController
@RequestMapping("/v1/token/rotate")
@RequiredArgsConstructor
@Log4j2
public class RotateAccessTokenController implements RotateTokenSwagger {
private final RotateAccessTokenService rotateTokenService;
private final long ACCESS_EXPIRATION;
private final long REFRESH_EXPIRATION;

public RotateAccessTokenController(RotateAccessTokenService rotateTokenService,
@Value("${jwt.access-token-expiry}") long accessTokenExpiry,
@Value("${jwt.refresh-token-expiry}") long refreshTokenExpiry
) {
this.rotateTokenService = rotateTokenService;
this.ACCESS_EXPIRATION = accessTokenExpiry;
this.REFRESH_EXPIRATION = refreshTokenExpiry;
}

@PostMapping
public ResponseEntity<RestResponse<RotateTokenResponse>> rotateToken(
Expand All @@ -37,13 +49,15 @@ public ResponseEntity<RestResponse<RotateTokenResponse>> rotateToken(
.httpOnly(true)
.secure(true)
.sameSite("None")
.maxAge(Duration.ofSeconds(ACCESS_EXPIRATION))
.path("/")
.build();

ResponseCookie newRefreshToken = from("refresh_token", rotateTokenResponse.refreshToken())
.httpOnly(true)
.secure(true)
.sameSite("None")
.maxAge(Duration.ofDays(REFRESH_EXPIRATION))
.path("/")
.build();

Expand Down