8
8
import com .flint .flint .member .repository .MemberRepository ;
9
9
import com .flint .flint .member .repository .PolicyRepository ;
10
10
import com .flint .flint .redis .RedisUtil ;
11
- import com .flint .flint .security .auth .dto .AuthenticationResponse ;
11
+ import com .flint .flint .security .auth .dto .response . AuthenticationResponse ;
12
12
import com .flint .flint .security .auth .dto .ClaimsDTO ;
13
- import com .flint .flint .security .auth .dto .RegisterRequest ;
14
- import com .flint .flint .security .auth . jwt .JwtService ;
15
- import com .flint .flint .security .oauth .dto . AuthorizionRequestHeader ;
16
- import com .flint .flint .security .oauth .dto . OAuth2UserAttribute ;
17
- import com . flint . flint . security . oauth . dto . OAuth2UserAttributeFactory ;
13
+ import com .flint .flint .security .auth .dto .request . RegisterRequest ;
14
+ import com .flint .flint .security .jwt .JwtService ;
15
+ import com .flint .flint .security .oauth .OAuth2UserAttribute ;
16
+ import com .flint .flint .security .oauth .OAuth2UserAttributeFactory ;
17
+ import jakarta . servlet . http . HttpServletRequest ;
18
18
import jakarta .transaction .Transactional ;
19
19
import lombok .RequiredArgsConstructor ;
20
20
import org .springframework .beans .factory .annotation .Value ;
@@ -43,38 +43,48 @@ public class AuthenticationService {
43
43
* member 저장, 수신동의 저장, 엑세스,리프레쉬토큰 생성, redis에 리프레쉬 토큰 저장
44
44
*/
45
45
@ Transactional
46
- public AuthenticationResponse register (RegisterRequest registerRequest , AuthorizionRequestHeader authorizionRequestHeader ) {
46
+ public AuthenticationResponse register (RegisterRequest registerRequest , HttpServletRequest oauth2TokenWithBearer ) {
47
47
//카카오인지 네이버인지 선택
48
48
OAuth2UserAttribute oAuth2UserAttribute = OAuth2UserAttributeFactory .of (registerRequest .getProviderName ());
49
- String oauth2AccessToekn = authorizionRequestHeader . getAccessToken (). replace ( "Bearer " , "" );
49
+ String oauth2AccessToken = jwtService . parseTokenFrom ( oauth2TokenWithBearer );
50
50
//정보 추출
51
- oAuth2UserAttribute .setUserAttributesByOauthToken (oauth2AccessToekn );
51
+ oAuth2UserAttribute .setUserAttributesByOauthToken (oauth2AccessToken );
52
52
checkRegistration (oAuth2UserAttribute .getProviderId ());
53
53
Member member = saveInformation (registerRequest , oAuth2UserAttribute );
54
54
return generateToken (member );
55
55
}
56
56
57
57
/**
58
- * 유저 리프레쉬 토큰의 만료기간까지 다 지났을 때 로그인
59
- * (리프레쉬 토큰이 살아있을 때 로그인은 newTokenByRefreshToken()를 호출한다)
60
- * 엑세스,리프레쉬토큰 생성, redis에 리프레쉬 토큰 저장
58
+ * Oauth2Provider 토큰으로 로그인
59
+ * 첫 로그인 혹은 엑세스,리프레쉬 토큰 없을 때
61
60
*/
62
61
@ Transactional
63
- public AuthenticationResponse login (String providerName , AuthorizionRequestHeader authorizionRequestHeader ) {
62
+ public AuthenticationResponse loginByOauth2Provider (String providerName , HttpServletRequest oauth2TokenWithBearer ) {
64
63
OAuth2UserAttribute oAuth2UserAttribute = OAuth2UserAttributeFactory .of (providerName );
65
- String oauth2AccessToekn = authorizionRequestHeader . getAccessToken (). replace ( "Bearer " , "" );
64
+ String oauth2AccessToken = jwtService . parseTokenFrom ( oauth2TokenWithBearer );
66
65
//정보 추출
67
- oAuth2UserAttribute .setUserAttributesByOauthToken (oauth2AccessToekn );
66
+ oAuth2UserAttribute .setUserAttributesByOauthToken (oauth2AccessToken );
68
67
String providerId = oAuth2UserAttribute .getProviderId ();
69
68
Member member = memberRepository .findByProviderId (providerId ).orElseThrow (() -> new FlintCustomException (HttpStatus .NOT_FOUND , ResultCode .USER_NOT_JOINED ));
70
69
return generateToken (member );
71
70
}
72
71
73
72
/**
74
- * 리프레쉬 토큰 재발급
73
+ * 엑세스 토큰으로 로그인
75
74
*/
76
75
@ Transactional
77
- public AuthenticationResponse newTokenByRefreshToken (String refreshToken ) {
76
+ public void loginByAccessToken (HttpServletRequest accessTokenWithBearer ) {
77
+ String accessToken = jwtService .parseTokenFrom (accessTokenWithBearer );
78
+ jwtService .isTokenValid (accessToken );
79
+ }
80
+
81
+
82
+ /**
83
+ * 리프레쉬 토큰으로 로그인
84
+ */
85
+ @ Transactional
86
+ public AuthenticationResponse newTokenByRefreshToken (HttpServletRequest refreshTokenWithBearer ) {
87
+ String refreshToken = jwtService .parseTokenFrom (refreshTokenWithBearer );
78
88
String providerId = jwtService .parseProviderId (refreshToken );
79
89
Member member = memberRepository .findByProviderId (providerId ).orElseThrow (() -> new FlintCustomException (HttpStatus .NOT_FOUND , ResultCode .USER_NOT_FOUND ));
80
90
jwtService .isTokenValid (refreshToken );
@@ -84,6 +94,7 @@ public AuthenticationResponse newTokenByRefreshToken(String refreshToken) {
84
94
throw new FlintCustomException (HttpStatus .BAD_REQUEST , ResultCode .REFRESHTOKEN_OUTDATED );
85
95
}
86
96
97
+
87
98
/**
88
99
* 엑세스 토큰 리프레쉬 토큰 생성, 레디쉬에 리프레쉬 토큰 저장
89
100
*/
@@ -118,4 +129,4 @@ private Member saveInformation(RegisterRequest registerRequest, OAuth2UserAttrib
118
129
policyRepository .save (policy );
119
130
return member ;
120
131
}
121
- }
132
+ }
0 commit comments