Skip to content

Commit

Permalink
refactor: Service에서 payload 생성하는 부분 메서드로 추출 (#35)
Browse files Browse the repository at this point in the history
- accessToken, freshToken용을 따로 생성
  • Loading branch information
mybloom committed Jun 11, 2022
1 parent b79f37b commit 659215a
Showing 1 changed file with 22 additions and 10 deletions.
32 changes: 22 additions & 10 deletions BE/src/main/java/org/team4/airbnb/auth/OAuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import lombok.RequiredArgsConstructor;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.MediaType;
Expand Down Expand Up @@ -33,7 +34,10 @@ public class OAuthService {
private final JwtTokenProvider jwtTokenProvider;

private final String TOKEN_TYPE = "Bearer";
private final long VALID_TIME = Duration.ofMillis(30).toMillis();
private final long VALID_TIME_ACCESS_TOKEN = Duration.ofMillis(30).toMillis();
private final long VALID_TIME_REFRESH_TOKEN = Duration.ofMillis(60).toMillis();

private final Random random = new Random();

@Transactional
public LoginResponse processLogin(String provider, String authCode) {
Expand All @@ -53,20 +57,28 @@ public LoginResponse processLogin(String provider, String authCode) {
* @return 로그인 응답 DTO
*/
private LoginResponse createJwtTokenAndMakeResponse(String userId) {
JwtPayload payload = makePayload(userId);
String accessToken = jwtTokenProvider.createAccessToken(userId);
String refreshToken = jwtTokenProvider.createRefreshToken();
Date issuedAt = new Date(System.currentTimeMillis());
Map<String, Object> privateClaim = new HashMap<>();
privateClaim.put("userid", userId);
privateClaim.put("role", "customer");

String accessToken = jwtTokenProvider.createToken(makePayloadForAccessToken(privateClaim, issuedAt));
String refreshToken = jwtTokenProvider.createToken(makePayloadForRefreshToken(privateClaim, issuedAt));

return LoginResponse.of(accessToken, refreshToken, TOKEN_TYPE);
}

private JwtPayload makePayload(String userId) {
Date issuedAt = new Date(System.currentTimeMillis());
Date expiration = new Date(System.currentTimeMillis() + VALID_TIME);
Map<String, String> privateClaim = new HashMap<>();
private JwtPayload makePayloadForAccessToken(Map<String, Object> privateClaim, Date issuedAt) {
Date expiration = new Date(System.currentTimeMillis() + VALID_TIME_ACCESS_TOKEN);
return JwtPayload.of(issuedAt, expiration, privateClaim);
}

privateClaim.put("userid", userId);
privateClaim.put("role","customer");
private JwtPayload makePayloadForRefreshToken(Map<String, Object> privateClaim, Date issuedAt) {
Date expiration = new Date(System.currentTimeMillis() + VALID_TIME_REFRESH_TOKEN);

byte[] bytes = new byte[7];
random.nextBytes(bytes);
privateClaim.put("refresh", new String(bytes, StandardCharsets.UTF_8));

return JwtPayload.of(issuedAt, expiration, privateClaim);
}
Expand Down

0 comments on commit 659215a

Please sign in to comment.