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]: 애플 로그인 구현 #61

Merged
merged 2 commits into from
Feb 6, 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
3 changes: 3 additions & 0 deletions .ebextensions_dev/00-makeFiles.config
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ files:
#!/usr/bin/env bash
JAR_PATH=/var/app/current/application.jar

# Copy keyfile from S3
aws s3 cp s3://onnoff-dev-s3/auth/AuthKey_5P49UCUSNW.p8 /var/app/current/src/main/resources/AuthKey_5P49UCUSNW.p8

# run app
killall java
java -Dfile.encoding=UTF-8 -jar $JAR_PATH
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
import com.onnoff.onnoff.domain.user.converter.UserConverter;
import com.onnoff.onnoff.domain.user.dto.UserResponseDTO;
import com.onnoff.onnoff.domain.user.service.UserService;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.InvalidClaimException;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.UnsupportedJwtException;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
Expand Down Expand Up @@ -77,7 +72,8 @@ public ResponseEntity<String> getAccessToken(@RequestParam(name = "code") String
4. 응답 헤더에 Jwt 토큰 추가
*/

@Operation(summary = "소셜 토큰 검증 API",description = "토큰을 검증 하고 이에 대한 결과를 응답합니다. 추가 정보 입력 여부도 같이 응답 합니다.")
@Operation(summary = "카카오 소셜 토큰 검증 API",description = "추가정보와 ID토큰을 받으면 ID토큰을 검증하고 통과 시" +
"서버에서 발급한 토큰을 받습니다. 회원가입을 하지 않은 사용자의 경우 회원가입을 시킵니다.")
@ResponseBody
@PostMapping("/oauth2/kakao/token/validate")
public ApiResponse<UserResponseDTO.LoginDTO> validateKakoToken(@RequestBody LoginRequestDTO.KakaoTokenValidateDTO requestDTO) {
Expand Down Expand Up @@ -106,11 +102,14 @@ public ApiResponse<UserResponseDTO.LoginDTO> validateKakoToken(@RequestBody Logi
return ApiResponse.onSuccess(UserConverter.toLoginDTO(token.getAccessToken(), token.getRefreshToken()));
}

@Operation(summary = "애플 소셜 토큰 검증 API",description = "추가정보와 ID토큰을 받으면 ID토큰을 검증하고 통과 시" +
"액세스/리프레시 토큰을 얻어서 저장시키고. 응답으로 서버에서 발급한 토큰을 받습니다. 회원가입을 하지 않은 사용자의 경우 회원가입을 시킵니다.")
@ResponseBody
@PostMapping("/oauth2/apple/token/validate")
public ApiResponse<UserResponseDTO.LoginDTO> validateAppleToken(@RequestBody LoginRequestDTO.AppleTokenValidateDTO requestDTO) {
// 검증하기
appleLoginService.validate(requestDTO.getIdentityToken());
log.info("애플 ID 토큰 검증 성공");
// 검증 성공 시 리프레시 토큰 발급받아 저장(기한 무제한, 회원탈퇴 시 필요)
TokenResponse tokenResponse = appleLoginService.getAccessTokenByCode(requestDTO.getAuthorizationCode());
// 유저 정보 조회 및 저장
Expand All @@ -124,7 +123,7 @@ public ApiResponse<UserResponseDTO.LoginDTO> validateAppleToken(@RequestBody Log
user.setAppleRefreshToken(tokenResponse.getRefreshToken());
user = userService.create(user);
}
// 응답헤더에 토큰 추가
// 응답본문에 토큰 추가
JwtToken token = jwtUtil.generateToken(String.valueOf(user.getId()));
return ApiResponse.onSuccess(UserConverter.toLoginDTO(token.getAccessToken(), token.getRefreshToken()));
}
Expand Down Expand Up @@ -164,4 +163,4 @@ public ResponseEntity<String> testAfterGetToken(){
log.info("authenticatedUser = {}", UserContext.getUser()); // 테스트 성공
return ResponseEntity.ok("");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@ public class AppleLoginService implements LoginService{
private String iss;
@Value("${apple.team-id}")
private String teamId;
@Value("${apple.redirect-uri}")
private String redirectUri;
@Override
public TokenResponse getAccessTokenByCode(String code) {
// client secret 만들기
Expand All @@ -56,9 +54,8 @@ public TokenResponse getAccessTokenByCode(String code) {
MultiValueMap<String, String> urlEncoded = TokenRequest.builder()
.clientId(clientId)
.clientSecret(clientSecret)
.code("authorization_code_value")
.code(code)
.grantType("authorization_code")
.redirectUri(redirectUri)
.build().toUrlEncoded();
return appleAuthClient.getToken(urlEncoded);
}
Expand Down Expand Up @@ -94,7 +91,6 @@ public String getAccessTokenByRfToken(String code) {
.clientSecret(clientSecret)
.refreshToken(appleRefreshToken)
.grantType("refresh_token")
.redirectUri(redirectUri)
.build().toUrlEncoded();
TokenResponse response = appleAuthClient.getToken(urlEncoded);
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ apple:
team-id: ${APPLE_TEAM_ID} # = ID prefix
key:
id: ${APPLE_KEY_ID}
path: classpath:/apple/AuthKey_${APPLE_KEY_ID}.p8 # 나중에 src/main/resources/apple/에 키 파일 저장, 그냥 문자열로 가져와도 될 것 같기도
path: classpath:/AuthKey_${APPLE_KEY_ID}.p8 # 나중에 src/main/resources/apple/에 키 파일 저장, 그냥 문자열로 가져와도 될 것 같기도
kakao:
redirect-uri: ${KAKAO_REDIRECT_URI}
iss: https://kauth.kakao.com
Expand Down
Loading