-
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.
Showing
25 changed files
with
716 additions
and
25 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
6 changes: 4 additions & 2 deletions
6
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/controller/auth/AuthReq.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 |
---|---|---|
@@ -1,14 +1,16 @@ | ||
package org.haedal.zzansuni.controller.auth; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.NotNull; | ||
import org.haedal.zzansuni.domain.auth.OAuth2Provider; | ||
|
||
public class AuthReq { | ||
public record OAuth2LoginRequest( | ||
@NotBlank(message = "provider는 필수입니다.") | ||
@NotNull(message = "provider는 필수입니다.") | ||
OAuth2Provider provider, | ||
@NotBlank(message = "code는 필수입니다.") | ||
String code | ||
String code, | ||
String state | ||
) { | ||
} | ||
} |
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
59 changes: 58 additions & 1 deletion
59
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/auth/AuthService.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 |
---|---|---|
@@ -1,5 +1,62 @@ | ||
package org.haedal.zzansuni.domain.auth; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.haedal.zzansuni.domain.user.*; | ||
import org.haedal.zzansuni.global.jwt.JwtToken; | ||
import org.haedal.zzansuni.global.jwt.JwtUser; | ||
import org.haedal.zzansuni.global.jwt.JwtUtils; | ||
import org.springframework.data.util.Pair; | ||
import org.springframework.lang.NonNull; | ||
import org.springframework.lang.Nullable; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class AuthService { | ||
private final List<OAuth2Client> oAuth2Clients; | ||
private final JwtUtils jwtUtils; | ||
private final UserReader userReader; | ||
private final UserStore userStore; | ||
|
||
/** | ||
* OAuth2 로그인 또는 회원가입 <br> | ||
* [state]는 nullable한 입력 값이다.<br> | ||
* 1. OAuth2Client를 이용해 해당 provider로부터 유저정보를 가져옴 | ||
* 2. authToken으로 유저를 찾거나 없으면 회원가입 | ||
* 3. 토큰 발급, 유저정보 반환 | ||
*/ | ||
public Pair<JwtToken, UserModel> oAuth2LoginOrSignup(OAuth2Provider provider, @NonNull String code, @Nullable String state) { | ||
OAuth2Client oAuth2Client = oAuth2Clients.stream() | ||
.filter(client -> client.canHandle(provider)) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("지원하지 않는 OAuth2Provider 입니다.")); | ||
|
||
// OAuth2Client를 이용해 해당 provider로부터 유저정보를 가져옴 | ||
OAuthUserInfoModel oAuthUserInfoModel = oAuth2Client.getAuthToken(code, state); | ||
|
||
// authToken으로 유저를 찾아서 없으면 [OAuthUserInfoModel]를 통해서 회원가입 진행 | ||
User user = userReader | ||
.findByAuthToken(oAuthUserInfoModel.authToken()) | ||
.orElseGet(() -> signup(oAuthUserInfoModel, provider)); | ||
|
||
// 토큰 발급, 유저정보 반환 | ||
JwtToken jwtToken = createToken(user); | ||
UserModel userModel = UserModel.from(user); | ||
return Pair.of(jwtToken, userModel); | ||
} | ||
|
||
|
||
private User signup(OAuthUserInfoModel oAuthUserInfoModel, OAuth2Provider provider) { | ||
UserCommand.CreateOAuth2 command = oAuthUserInfoModel.toCreateCommand(provider); | ||
User user = User.create(command); | ||
return userStore.store(user); | ||
} | ||
|
||
private JwtToken createToken(User user) { | ||
JwtUser jwtUser = JwtUser.of(user.getId(), user.getRole()); | ||
return jwtUtils.createToken(jwtUser); | ||
} | ||
|
||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/auth/OAuth2Client.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,14 @@ | ||
package org.haedal.zzansuni.domain.auth; | ||
|
||
import org.springframework.lang.NonNull; | ||
import org.springframework.lang.Nullable; | ||
|
||
public interface OAuth2Client { | ||
boolean canHandle(OAuth2Provider provider); | ||
|
||
/** | ||
* 인증코드를 이용하여 사용자 정보를 가져온다. | ||
* [state]가 필요한 Client의 경우 해당 파라미터를 사용한다. | ||
*/ | ||
OAuthUserInfoModel getAuthToken(@NonNull String code, @Nullable String state); | ||
} |
3 changes: 2 additions & 1 deletion
3
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/auth/OAuth2Provider.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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package org.haedal.zzansuni.domain.auth; | ||
|
||
public enum OAuth2Provider { | ||
KAKAO; | ||
KAKAO, | ||
NAVER, | ||
} |
19 changes: 19 additions & 0 deletions
19
...suni-api-server/app/src/main/java/org/haedal/zzansuni/domain/auth/OAuthUserInfoModel.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,19 @@ | ||
package org.haedal.zzansuni.domain.auth; | ||
|
||
import lombok.Builder; | ||
import org.haedal.zzansuni.domain.user.UserCommand; | ||
|
||
@Builder | ||
public record OAuthUserInfoModel( | ||
String authToken, | ||
String nickname | ||
) { | ||
public UserCommand.CreateOAuth2 toCreateCommand(OAuth2Provider provider){ | ||
return UserCommand.CreateOAuth2 | ||
.builder() | ||
.nickname(nickname) | ||
.authToken(authToken) | ||
.provider(provider) | ||
.build(); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/TierSystem.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.domain.user; | ||
|
||
import lombok.Getter; | ||
import lombok.RequiredArgsConstructor; | ||
|
||
import java.util.Arrays; | ||
|
||
/** | ||
* 티어 정보를 enum으로 정적으로 관리 | ||
* 향후 데이터베이스에 저장할수도 있음 | ||
*/ | ||
@Getter | ||
@RequiredArgsConstructor | ||
public enum TierSystem { | ||
NOBI_4("노비 IV", 0, 5), | ||
NOBI_3("노비 III", 5, 10), | ||
NOBI_2("노비 II", 10, 20), | ||
NOBI_1("노비 I", 20, 40), | ||
SANGMIN_4("상민 IV", 40, 60), | ||
SANGMIN_3("상민 III", 60, 80), | ||
SANGMIN_2("상민 II", 80, 100), | ||
SANGMIN_1("상민 I", 100, 120), | ||
PYEONGMIN_4("평민 IV", 120, 140), | ||
PYEONGMIN_3("평민 III", 140, 160), | ||
PYEONGMIN_2("평민 II", 160, 180), | ||
PYEONGMIN_1("평민 I", 180, 200), | ||
YANGBAN_4("양반 IV", 200, 230), | ||
YANGBAN_3("양반 III", 230, 260), | ||
YANGBAN_2("양반 II", 260, 290), | ||
YANGBAN_1("양반 I", 290, 320), | ||
JINGOL_4("진골 IV", 320, 360), | ||
JINGOL_3("진골 III", 360, 400), | ||
JINGOL_2("진골 II", 400, 440), | ||
JINGOL_1("진골 I", 440, 480), | ||
SEONGOL_4("성골 IV", 480, 530), | ||
SEONGOL_3("성골 III", 530, 580), | ||
SEONGOL_2("성골 II", 580, 630), | ||
SEONGOL_1("성골 I", 630, 680), | ||
ECHO_4("에코 IV", 680, 740), | ||
ECHO_3("에코 III", 740, 800), | ||
ECHO_2("에코 II", 800, 860), | ||
ECHO_1("에코 I", 860, Integer.MAX_VALUE); | ||
|
||
|
||
private final String korean; | ||
private final int startExp; | ||
private final int endExp; | ||
|
||
public static TierSystem getTier(int exp) { | ||
return Arrays.stream(TierSystem.values()) | ||
.filter(tier -> tier.startExp <= exp && exp < tier.endExp) | ||
.findFirst() | ||
.orElseThrow(() -> new IllegalArgumentException("티어를 찾을 수 없습니다.")); | ||
|
||
} | ||
} |
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
24 changes: 23 additions & 1 deletion
24
zzansuni-api-server/app/src/main/java/org/haedal/zzansuni/domain/user/UserCommand.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
Oops, something went wrong.