Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 카카오 oauth token 받아오기 구현 #157

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import sunflower.server.application.SessionService;
import sunflower.server.auth.application.KakaoMemberService;
Expand All @@ -22,4 +23,10 @@ public ResponseEntity<Void> login() {
.header("Location", uri)
.build();
}

@GetMapping("/login/kakao/session")
public ResponseEntity<Void> authorize(@RequestParam("code") String code) {
kakaoMemberService.login(code);
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,37 @@

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import sunflower.server.auth.client.KakaoOAuthClient;

@Service
public class KakaoMemberService {

private String redirectURI;
private String restApiKey;
private String authCodeURI;
private final String redirectURI;
private final String restApiKey;
private final String authCodeURI;
private final String authTokenURI;
private final KakaoOAuthClient kakaoOAuthClient;

public KakaoMemberService(
@Value("${oauth.kakao.rest-api-key}") String restApiKey,
@Value("${oauth.kakao.redirect-uri}") String redirectURI,
@Value("${oauth.kakao.auth-code-uri}") String authCodeURI
@Value("${oauth.kakao.auth-code-uri}") String authCodeURI,
@Value("${oauth.kakao.token-uri}") String authTokenURI,
final KakaoOAuthClient kakaoOAuthClient
) {
this.redirectURI = redirectURI;
this.restApiKey = restApiKey;
this.authCodeURI = authCodeURI;
this.authTokenURI = authTokenURI;
this.kakaoOAuthClient = kakaoOAuthClient;
}

public String loginURI() {
return String.format(authCodeURI, restApiKey, redirectURI);
}

public String login(final String code) {
kakaoOAuthClient.login(authTokenURI, restApiKey, redirectURI, code);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package sunflower.server.auth.client;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
import sunflower.server.auth.client.response.KakaoOAuthResponse;

import java.util.Map;

@Slf4j
@Component
public class KakaoOAuthClient {

private final ObjectMapper objectMapper = new ObjectMapper();
private static final String GRANT_TYPE = "authorization_code";
private final RestTemplate restTemplate;

public KakaoOAuthClient(final RestTemplate restTemplate) {
this.restTemplate = restTemplate;
}

public void login(final String authTokenURI, final String restApiKey, final String redirectURI, final String code) {
final String requestURI = authTokenURI;
HttpHeaders requestHeader = createRequestHeader();
final MultiValueMap<String, Object> requestBody = createRequestBody(restApiKey, redirectURI, code);
final HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(requestBody, requestHeader);

log.info("Request URI: {}", requestURI);
log.info("Request Headers: {}", requestHeader);
log.info("Request Parameters: {}", requestBody);

final KakaoOAuthResponse response = restTemplate.postForObject(requestURI, requestEntity, KakaoOAuthResponse.class);
}

private MultiValueMap<String, Object> createRequestBody(final String restApiKey, final String redirectURI, final String code) {
MultiValueMap<String, Object> requestBody = new LinkedMultiValueMap<>();

requestBody.add("grant_type", GRANT_TYPE);
requestBody.add("client_id", restApiKey);
requestBody.add("redirect_uri", redirectURI);
requestBody.add("code", code);

return requestBody;
}

private String convertToJson(final Map<String, Object> body) {
try {
return objectMapper.writeValueAsString(body);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

private HttpHeaders createRequestHeader() {
HttpHeaders requestHeader = new HttpHeaders();
requestHeader.set("Content-Type", "application/x-www-form-urlencoded");
return requestHeader;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package sunflower.server.auth.client.response;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.ToString;

@ToString
@Getter
@NoArgsConstructor
public class KakaoOAuthResponse {

@JsonProperty("access_token")
private String accessToken;

@JsonProperty("token_type")
private String tokenType;

@JsonProperty("refresh_token")
private String refreshToken;

@JsonProperty("expires_in")
private String expiresIn;

@JsonProperty("refresh_token_expires_in")
private String refreshTokenExpiresIn;

@JsonProperty("scope")
private String scope;
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ApiOcrDownloadClient(
@Value("${ocr.download-uri}") String appURI,
@Value("${ocr.app-id}") String appId,
@Value("${ocr.app-key}") String appKey,
RestTemplate restTemplate
final RestTemplate restTemplate
) {
this.appURI = appURI;
this.appId = appId;
Expand Down
1 change: 1 addition & 0 deletions server/src/test/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ oauth:
redirect-uri: https://www.sunnybraille.com/login/kakao
rest-api-key: rest-api-key
auth-code-uri: auth-code-uri
token-uri: token-uri