-
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.
- Loading branch information
Showing
89 changed files
with
2,366 additions
and
883 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
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); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
backend/src/main/java/shook/shook/auth/application/TokenPairScheduler.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,28 @@ | ||
package shook.shook.auth.application; | ||
|
||
import java.util.Set; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
import shook.shook.auth.exception.TokenException; | ||
import shook.shook.auth.repository.InMemoryTokenPairRepository; | ||
|
||
@RequiredArgsConstructor | ||
@Component | ||
public class TokenPairScheduler { | ||
|
||
private final TokenProvider tokenProvider; | ||
private final InMemoryTokenPairRepository inMemoryTokenPairRepository; | ||
|
||
@Scheduled(cron = "${schedules.in-memory-token.cron}") | ||
public void removeExpiredTokenPair() { | ||
final Set<String> refreshTokens = inMemoryTokenPairRepository.getTokenPairs().keySet(); | ||
for (String refreshToken : refreshTokens) { | ||
try { | ||
tokenProvider.parseClaims(refreshToken); | ||
} catch (TokenException.ExpiredTokenException e) { | ||
inMemoryTokenPairRepository.delete(refreshToken); | ||
} | ||
} | ||
} | ||
} |
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
Oops, something went wrong.