-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: email 형식 제약조건 변경하기 * refactor: member의 nickname을 부여하는 방식 변경 * feat: 카카오 로그인 기능 추가 * refactor: OAuth 추상화하기 세부사항 - 소셜 로그인 과정 중에 accessToken을 가져오는 것과 사용자 정보를 조회하는 부분을 추상화 - authService는 각 infoProvider를 의존하는것이 아닌 OAuthInfoProvider를 의존하도록 수정 * refactor: QA를 위한 수정 * refactor: CI 실패 수정 * refactor: 에러 상세화를 위한 수정 * refactor: 카카오 로그인 확인을 위한 로그 추가 * refactor: 서버 엑세스 코드 확인 위한 로그 추가 * refactor: 서버 엑세스 코드 확인 위한 로그 추가 * refactor: QA를 종료후 기존의 코드 원복 세부사항 - 예외 핸들링 원복 - 불필요한 로그 제거 * refactor : 코드리뷰 반영 세부사항 - 명확한 클래스 명칭으로 수정 - requireNonNull을 이용할 시 nullPointException 처리 - 상수 분리 * refactor: CI 실패하는 오류 수정 * refactor: 서브모듈 최신화
- Loading branch information
1 parent
63c03aa
commit d78c279
Showing
25 changed files
with
418 additions
and
94 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
88 changes: 88 additions & 0 deletions
88
backend/src/main/java/shook/shook/auth/application/KakaoInfoProvider.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,88 @@ | ||
package shook.shook.auth.application; | ||
|
||
import java.util.Objects; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpEntity; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.HttpClientErrorException; | ||
import org.springframework.web.client.HttpServerErrorException; | ||
import org.springframework.web.client.RestTemplate; | ||
import shook.shook.auth.application.dto.KakaoAccessTokenResponse; | ||
import shook.shook.auth.application.dto.KakaoMemberInfoResponse; | ||
import shook.shook.auth.exception.OAuthException; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class KakaoInfoProvider implements OAuthInfoProvider { | ||
|
||
private static final String APPLICATION_X_WWW_FORM_URLENCODED = "application/x-www-form-urlencoded;charset=utf-8"; | ||
private static final String TOKEN_PREFIX = "Bearer "; | ||
private static final String GRANT_TYPE = "authorization_code"; | ||
|
||
|
||
@Value("${oauth2.kakao.access-token-url}") | ||
private String KAKAO_ACCESS_TOKEN_URL; | ||
|
||
@Value("${oauth2.kakao.member-info-url}") | ||
private String KAKAO_MEMBER_INFO_URL; | ||
|
||
@Value("${oauth2.kakao.client-id}") | ||
private String KAKAO_CLIENT_ID; | ||
|
||
@Value("${oauth2.kakao.redirect-url}") | ||
private String LOGIN_REDIRECT_URL; | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
public String getMemberInfo(final String accessToken) { | ||
try { | ||
final HttpHeaders headers = new HttpHeaders(); | ||
headers.set(HttpHeaders.AUTHORIZATION, TOKEN_PREFIX + accessToken); | ||
final HttpEntity<Object> request = new HttpEntity<>(headers); | ||
final ResponseEntity<KakaoMemberInfoResponse> response = restTemplate.exchange( | ||
KAKAO_MEMBER_INFO_URL, | ||
HttpMethod.GET, | ||
request, | ||
KakaoMemberInfoResponse.class | ||
); | ||
|
||
return String.valueOf(Objects.requireNonNull(response.getBody()).getId()); | ||
} catch (HttpClientErrorException e) { | ||
throw new OAuthException.InvalidAccessTokenException(); | ||
} catch (HttpServerErrorException | NullPointerException e) { | ||
throw new OAuthException.KakaoServerException(); | ||
} | ||
} | ||
|
||
public String getAccessToken(final String authorizationCode) { | ||
try { | ||
final MultiValueMap<String, String> params = new LinkedMultiValueMap<>(); | ||
params.add("grant_type", GRANT_TYPE); | ||
params.add("client_id", KAKAO_CLIENT_ID); | ||
params.add("redirect_uri", LOGIN_REDIRECT_URL); | ||
params.add("code", authorizationCode); | ||
|
||
HttpHeaders headers = new HttpHeaders(); | ||
headers.set(HttpHeaders.CONTENT_TYPE, APPLICATION_X_WWW_FORM_URLENCODED); | ||
|
||
final HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, headers); | ||
|
||
final ResponseEntity<KakaoAccessTokenResponse> response = restTemplate.postForEntity( | ||
KAKAO_ACCESS_TOKEN_URL, | ||
request, | ||
KakaoAccessTokenResponse.class); | ||
|
||
return Objects.requireNonNull(response.getBody()).getAccessToken(); | ||
} catch (HttpClientErrorException e) { | ||
throw new OAuthException.InvalidAuthorizationCodeException(); | ||
} catch (HttpServerErrorException | NullPointerException e) { | ||
throw new OAuthException.KakaoServerException(); | ||
} | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
backend/src/main/java/shook/shook/auth/application/OAuthInfoProvider.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 shook.shook.auth.application; | ||
|
||
public interface OAuthInfoProvider { | ||
|
||
String getMemberInfo(final String accessToken); | ||
|
||
String getAccessToken(final String authorizationCode); | ||
} |
27 changes: 27 additions & 0 deletions
27
backend/src/main/java/shook/shook/auth/application/OAuthProviderFinder.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,27 @@ | ||
package shook.shook.auth.application; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.stereotype.Component; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class OAuthProviderFinder { | ||
|
||
private final Map<OAuthType, OAuthInfoProvider> oauthExecution = new HashMap<>(); | ||
private final KakaoInfoProvider kakaoInfoProvider; | ||
private final GoogleInfoProvider googleInfoProvider; | ||
|
||
@PostConstruct | ||
public void init() { | ||
oauthExecution.put(OAuthType.GOOGLE, googleInfoProvider); | ||
oauthExecution.put(OAuthType.KAKAO, kakaoInfoProvider); | ||
} | ||
|
||
public OAuthInfoProvider getOAuthInfoProvider(final String oauthType) { | ||
final OAuthType key = OAuthType.find(oauthType); | ||
return oauthExecution.get(key); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/shook/shook/auth/application/OAuthType.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,16 @@ | ||
package shook.shook.auth.application; | ||
|
||
import java.util.Arrays; | ||
import shook.shook.auth.exception.OAuthException; | ||
|
||
public enum OAuthType { | ||
|
||
GOOGLE, KAKAO; | ||
|
||
public static OAuthType find(final String oauthType) { | ||
return Arrays.stream(OAuthType.values()) | ||
.filter(type -> type.name().equals(oauthType.toUpperCase())) | ||
.findFirst() | ||
.orElseThrow(OAuthException.OauthTypeNotFoundException::new); | ||
} | ||
} |
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
16 changes: 16 additions & 0 deletions
16
backend/src/main/java/shook/shook/auth/application/dto/KakaoAccessTokenResponse.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,16 @@ | ||
package shook.shook.auth.application.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Getter | ||
public class KakaoAccessTokenResponse { | ||
|
||
@JsonProperty("access_token") | ||
private String accessToken; | ||
} |
15 changes: 15 additions & 0 deletions
15
backend/src/main/java/shook/shook/auth/application/dto/KakaoMemberInfoResponse.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,15 @@ | ||
package shook.shook.auth.application.dto; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AccessLevel; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
@AllArgsConstructor | ||
@NoArgsConstructor(access = AccessLevel.PRIVATE) | ||
@Getter | ||
public class KakaoMemberInfoResponse { | ||
|
||
private Long id; | ||
} |
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
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.