Skip to content

Commit

Permalink
feat: 토큰 만료에 대한 재발급 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
JaeHongDev committed Jul 6, 2024
1 parent d6d1acb commit b9da5e0
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package in.backend.core.auth.application;


import in.backend.core.auth.application.payload.IssuedToken;
import in.backend.core.auth.domain.Visitor;
import in.backend.core.auth.infrastrcutrue.RefreshTokenWriter;
import in.backend.core.auth.infrastrcutrue.RefreshTokenReader;
import in.backend.global.provider.JwtProvider;
import java.time.Instant;
import lombok.RequiredArgsConstructor;
Expand All @@ -12,21 +10,16 @@
@Service
@RequiredArgsConstructor
public class TokenReissue {

private final JwtProvider jwtProvider;
private final RefreshTokenWriter refreshTokenWriter;

public IssuedToken publish(Visitor visitor) {
var now = Instant.now();
private final RefreshTokenReader refreshTokenReader;

var accessToken = jwtProvider.createAccessToken(visitor.memberId(), now);
var refreshToken = jwtProvider.createRefreshToken(visitor.memberId(), now);
public String publish(String refreshToken) {
jwtProvider.validRefreshToken(refreshToken);

refreshTokenWriter.write(visitor.memberId(), refreshToken);

return IssuedToken.builder()
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
return jwtProvider.createAccessToken(
refreshTokenReader.read(refreshToken).getId(),
Instant.now()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.Getter;
import lombok.NoArgsConstructor;


@Getter
@Entity
@NoArgsConstructor
@Table(name = "REFRESH_TOKENS")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package in.backend.core.auth.infrastrcutrue;


import in.backend.core.auth.entity.RefreshTokenEntity;
import in.backend.global.exception.GlobalExceptionCode;
import in.backend.global.exception.RefreshTokenException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -14,4 +17,9 @@ public class RefreshTokenReader {
public boolean existsBy(Long memberId) {
return refreshTokenRepository.existsById(memberId);
}

public RefreshTokenEntity read(String refreshToken) {
return refreshTokenRepository.findByToken(refreshToken)
.orElseThrow(() -> new RefreshTokenException(GlobalExceptionCode.NOT_FOUND_REFRESH_TOKEN));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package in.backend.core.auth.infrastrcutrue;

import in.backend.core.auth.entity.RefreshTokenEntity;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;

public interface RefreshTokenRepository extends JpaRepository<RefreshTokenEntity, Long> {

Optional<RefreshTokenEntity> findByToken(String refreshToken);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
Expand Down Expand Up @@ -69,17 +70,14 @@ public OAuthProfileResponse getProfile(@ModelAttribute OAuthProfileRequest profi
return socialLoginProcessor.findProfile(profile);
}

@MemberOnly
@PostMapping("/token/reissue")
public ResponseEntity<AccessTokenResponse> reIssue(
@Auth Visitor visitor,
HttpServletResponse response
@CookieValue("refreshToken") final String refreshToken
) {
var issuedToken = tokenReissue.publish(visitor);
response.addHeader(SET_COOKIE, cookieProvider.createCookie(issuedToken.refreshToken()).toString());
var accessToken = tokenReissue.publish(refreshToken);

return ResponseEntity.status(CREATED)
.body(new AccessTokenResponse(issuedToken.accessToken()));
.body(new AccessTokenResponse(accessToken));
}


Expand Down
20 changes: 10 additions & 10 deletions backend/src/main/java/in/backend/global/provider/JwtProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,6 @@ private Map<String, Object> decode(final String token) {
return jwtDecoder.decode(token).getClaims();
}


public Long extractToValueFrom(final String token) {
return Long.parseLong(this.decode(token)
.get("id")
.toString()
);
}

public Long decodeAccessToken(final String accessToken) {
try {
return Long.parseLong(String.valueOf(this.decode(accessToken).get("id")));
Expand All @@ -91,10 +83,18 @@ public Long decodeRefreshToken(final String refreshToken) {
public String extractToken(final HttpServletRequest request) {
final String token = request.getHeader(HEADER_AUTHORIZATION);

if (!Objects.isNull(token) && token.startsWith(TOKEN_PREFIX)) {
return token.substring(TOKEN_PREFIX.length());
return extractToken(token);

}

public String extractToken(final String headerValue) {
if (!Objects.isNull(headerValue) && headerValue.startsWith(TOKEN_PREFIX)) {
return headerValue.substring(TOKEN_PREFIX.length());
}
return null;
}

public void validRefreshToken(String refreshToken) {
decodeRefreshToken(refreshToken);
}
}

0 comments on commit b9da5e0

Please sign in to comment.