-
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.
Browse files
Browse the repository at this point in the history
Issue/#78
- Loading branch information
Showing
16 changed files
with
468 additions
and
54 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
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
56 changes: 56 additions & 0 deletions
56
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/auth/domain/CreateJwtUseCase.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,56 @@ | ||
package org.haedal.zzansuni.auth.domain; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.haedal.zzansuni.common.domain.UuidHolder; | ||
import org.haedal.zzansuni.global.jwt.JwtToken; | ||
import org.haedal.zzansuni.global.jwt.JwtUser; | ||
import org.haedal.zzansuni.global.jwt.JwtUtils; | ||
import org.haedal.zzansuni.user.domain.User; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class CreateJwtUseCase { | ||
private final JwtUtils jwtUtils; | ||
private final RefreshTokenReader refreshTokenReader; | ||
private final RefreshTokenStore refreshTokenStore; | ||
private final UuidHolder uuidHolder; | ||
/** | ||
* JWT 발급 | ||
* 1. 리프래시토큰의 uuid 생성 | ||
* 2. JWT 토큰 생성 | ||
* 3. DB에 리프래시토큰 정보를 저장 | ||
*/ | ||
@Transactional | ||
public JwtToken invoke(User user) { | ||
JwtUser jwtUser = JwtUser.of(user.getId(), user.getRole()); | ||
String uuid = uuidHolder.random(); | ||
JwtToken jwtToken = jwtUtils.generateToken(jwtUser, uuid); | ||
RefreshToken refreshToken = RefreshToken.create(uuid, user, jwtToken.getRefreshTokenExpireAt()); | ||
refreshTokenStore.flushSave(refreshToken); | ||
return jwtToken; | ||
} | ||
|
||
@Transactional | ||
public JwtToken removeRefreshTokenAndCreateJwt(JwtUtils.UserIdAndUuid userIdAndUuid) { | ||
RefreshToken refreshToken = refreshTokenReader.findById(userIdAndUuid.uuid()) | ||
.orElseThrow(() -> new IllegalArgumentException("존재하지 않는 토큰입니다.")); | ||
|
||
// jwtUtils에서 이미 검증하였으나, 방어적으로 다시 한번 검증 | ||
if (!refreshToken.getUser().getId().equals(userIdAndUuid.userId())) { | ||
throw new IllegalArgumentException("토큰의 유저정보가 일치하지 않습니다."); | ||
} else if (refreshToken.getExpiredAt().isBefore(LocalDateTime.now())) { | ||
throw new IllegalArgumentException("만료된 토큰입니다."); | ||
} | ||
|
||
refreshTokenStore.delete(refreshToken.getId()); | ||
User user = refreshToken.getUser(); | ||
return invoke(user); | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/auth/domain/RefreshToken.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,39 @@ | ||
package org.haedal.zzansuni.auth.domain; | ||
|
||
import jakarta.persistence.*; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.haedal.zzansuni.user.domain.User; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Entity | ||
@Builder | ||
@AllArgsConstructor | ||
@Getter | ||
@NoArgsConstructor | ||
public class RefreshToken { | ||
@Id @Column(columnDefinition = "CHAR(36)") | ||
private String id; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY, optional = false) | ||
@JoinColumn(name = "user_id", nullable = false) | ||
private User user; | ||
|
||
@Column(columnDefinition ="TIMESTAMP(0)", nullable = false) | ||
private LocalDateTime expiredAt; | ||
|
||
public static RefreshToken create(String id,User user,LocalDateTime refreshTokenExpireAt) { | ||
return RefreshToken.builder() | ||
.id(id) | ||
.user(user) | ||
.expiredAt(refreshTokenExpireAt) | ||
.build(); | ||
} | ||
|
||
public Long getUserId() { | ||
return user.getId(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...suni-api-server/app/src/main/java/org/haedal/zzansuni/auth/domain/RefreshTokenReader.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,8 @@ | ||
package org.haedal.zzansuni.auth.domain; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
public interface RefreshTokenReader { | ||
Optional<RefreshToken> findById(String id); | ||
} |
8 changes: 8 additions & 0 deletions
8
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/auth/domain/RefreshTokenStore.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,8 @@ | ||
package org.haedal.zzansuni.auth.domain; | ||
|
||
|
||
public interface RefreshTokenStore { | ||
void flushSave(RefreshToken refreshToken); | ||
|
||
void delete(String id); | ||
} |
35 changes: 35 additions & 0 deletions
35
...pp/src/main/java/org/haedal/zzansuni/auth/infrastructure/RefreshTokenReaderStoreImpl.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,35 @@ | ||
package org.haedal.zzansuni.auth.infrastructure; | ||
|
||
import jakarta.persistence.EntityManager; | ||
import lombok.RequiredArgsConstructor; | ||
import org.haedal.zzansuni.auth.domain.RefreshToken; | ||
import org.haedal.zzansuni.auth.domain.RefreshTokenReader; | ||
import org.haedal.zzansuni.auth.domain.RefreshTokenStore; | ||
import org.springframework.stereotype.Repository; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
@RequiredArgsConstructor | ||
public class RefreshTokenReaderStoreImpl implements RefreshTokenReader, RefreshTokenStore { | ||
private final RefreshTokenRepository refreshTokenRepository; | ||
private final EntityManager entityManager; | ||
|
||
@Override | ||
@Transactional | ||
public void flushSave(RefreshToken refreshToken) { | ||
entityManager.persist(refreshToken); | ||
entityManager.flush(); | ||
} | ||
|
||
@Override | ||
public void delete(String id) { | ||
refreshTokenRepository.deleteById(id); | ||
} | ||
|
||
@Override | ||
public Optional<RefreshToken> findById(String id) { | ||
return refreshTokenRepository.findById(id); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...ver/app/src/main/java/org/haedal/zzansuni/auth/infrastructure/RefreshTokenRepository.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,9 @@ | ||
package org.haedal.zzansuni.auth.infrastructure; | ||
|
||
import org.haedal.zzansuni.auth.domain.RefreshToken; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.util.List; | ||
|
||
public interface RefreshTokenRepository extends JpaRepository<RefreshToken, String> { | ||
} |
5 changes: 5 additions & 0 deletions
5
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/common/domain/UuidHolder.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,5 @@ | ||
package org.haedal.zzansuni.common.domain; | ||
|
||
public interface UuidHolder { | ||
String random(); | ||
} |
12 changes: 12 additions & 0 deletions
12
...-server/app/src/main/java/org/haedal/zzansuni/common/infrastructure/SystemUuidHolder.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,12 @@ | ||
package org.haedal.zzansuni.common.infrastructure; | ||
|
||
import org.haedal.zzansuni.common.domain.UuidHolder; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class SystemUuidHolder implements UuidHolder { | ||
@Override | ||
public String random() { | ||
return java.util.UUID.randomUUID().toString(); | ||
} | ||
} |
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
Oops, something went wrong.