Skip to content

Commit

Permalink
[Feat] 피그마 파일 추가 시 파일 이름까지 저장하도록 변경 (#55)
Browse files Browse the repository at this point in the history
* fix : 로그인 시 피그마 리프레시 토큰까지 저장

* fix : 동일 피그마 계정이 중복 저장되지 않도록 수정

* feat : 파일 id 헤더 사용 시 파일 저장 로직 변경
  • Loading branch information
iiqcov authored and cowboysj committed Nov 16, 2024
1 parent 573c554 commit daf06f9
Show file tree
Hide file tree
Showing 17 changed files with 288 additions and 150 deletions.
23 changes: 13 additions & 10 deletions src/main/java/gigedi/dev/domain/auth/application/AuthService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
import gigedi.dev.domain.auth.dto.AccessTokenDto;
import gigedi.dev.domain.auth.dto.RefreshTokenDto;
import gigedi.dev.domain.auth.dto.request.TokenRefreshRequest;
import gigedi.dev.domain.auth.dto.response.FigmaAccountResponse;
import gigedi.dev.domain.auth.dto.response.GoogleLoginResponse;
import gigedi.dev.domain.auth.dto.response.TokenPairResponse;
import gigedi.dev.domain.auth.dto.response.UserInfoResponse;
import gigedi.dev.domain.auth.dto.response.*;
import gigedi.dev.domain.figma.application.FigmaService;
import gigedi.dev.domain.member.dao.MemberRepository;
import gigedi.dev.domain.member.domain.Member;
import gigedi.dev.domain.member.domain.OauthInfo;
Expand All @@ -30,11 +28,12 @@
@RequiredArgsConstructor
public class AuthService {
private final GoogleService googleService;
private final FigmaService figmaService;
private final FigmaApiService figmaApiService;
private final IdTokenVerifier idTokenVerifier;
private final MemberRepository memberRepository;
private final FigmaRepository figmaRepository;
private final JwtTokenService jwtTokenService;
private final FigmaService figmaService;
private final MemberUtil memberUtil;

public TokenPairResponse googleSocialLogin(String code) {
Expand All @@ -49,20 +48,24 @@ public TokenPairResponse googleSocialLogin(String code) {

public UserInfoResponse figmaSocialLogin(String code) {
final Member currentMember = memberUtil.getCurrentMember();
String accessToken = figmaService.getAccessToken(code);
UserInfoResponse userInfo = figmaService.getUserInfo(accessToken);
saveFigmaAccountInfo(currentMember, userInfo);
FigmaTokenResponse tokenResponse =
figmaApiService.getAccessToken(URLDecoder.decode(code, StandardCharsets.UTF_8));
figmaService.validateFigmaAccountAlreadyExists(tokenResponse.userId().toString());
UserInfoResponse userInfo = figmaApiService.getUserInfo(tokenResponse.accessToken());
saveFigmaAccountInfo(currentMember, userInfo, tokenResponse);
return userInfo;
}

private void saveFigmaAccountInfo(Member member, UserInfoResponse userInfo) {
private void saveFigmaAccountInfo(
Member member, UserInfoResponse userInfo, FigmaTokenResponse tokenResponse) {
Figma figma =
Figma.createFigma(
userInfo.userName(),
userInfo.email(),
userInfo.ImgUrl(),
userInfo.userId(),
member);
member,
tokenResponse.refreshToken());
figmaRepository.save(figma);
}

Expand Down
167 changes: 167 additions & 0 deletions src/main/java/gigedi/dev/domain/auth/application/FigmaApiService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
package gigedi.dev.domain.auth.application;

import static gigedi.dev.global.common.constants.SecurityConstants.*;

import java.util.Base64;

import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClient;

import com.fasterxml.jackson.databind.ObjectMapper;

import gigedi.dev.domain.auth.dto.response.FigmaTokenResponse;
import gigedi.dev.domain.auth.dto.response.FigmaUserResponse;
import gigedi.dev.domain.auth.dto.response.UserInfoResponse;
import gigedi.dev.domain.file.dto.response.FigmaTokenReissueResponse;
import gigedi.dev.domain.file.dto.response.GetFileInfoResponse;
import gigedi.dev.global.error.exception.CustomException;
import gigedi.dev.global.error.exception.ErrorCode;
import gigedi.dev.infra.config.oauth.FigmaProperties;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
@Service
public class FigmaApiService {

private final FigmaProperties figmaProperties;
private final RestClient restClient;

public FigmaTokenResponse getAccessToken(String code) {
try {
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add(CLIENT_ID_KEY, figmaProperties.id());
formData.add(CLIENT_SECRET_KEY, figmaProperties.secret());
formData.add(REDIRECT_URI_KEY, figmaProperties.redirectUri());
formData.add(CODE_KEY, code);
formData.add(GRANT_TYPE_KEY, LOGIN_GRANT_TYPE_VALUE);

String responseBody =
restClient
.post()
.uri(FIGMA_GET_ID_TOKEN_URL)
.header(
HttpHeaders.CONTENT_TYPE,
MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.body(formData)
.retrieve()
.onStatus(
status -> !status.is2xxSuccessful(),
(request, responseEntity) -> {
log.error(
"Figma ID Token 요청 실패: {}",
responseEntity.getStatusCode());
throw new CustomException(ErrorCode.FIGMA_LOGIN_FAILED);
})
.body(String.class);

ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.readValue(responseBody, FigmaTokenResponse.class);
} catch (Exception e) {
log.error("Figma 로그인 중 예외 발생 : {}", e.getMessage(), e);
throw new CustomException(ErrorCode.FIGMA_LOGIN_FAILED);
}
}

public UserInfoResponse getUserInfo(String accessToken) {
try {
FigmaUserResponse figmaUserResponse =
restClient
.get()
.uri(FIGMA_GET_USER_INFO_URL)
.header(HttpHeaders.AUTHORIZATION, TOKEN_PREFIX + accessToken)
.retrieve()
.onStatus(
status -> !status.is2xxSuccessful(),
(request, response) -> {
log.error(
"Figma 유저 정보 요청 실패: {}", response.getStatusCode());
throw new CustomException(ErrorCode.FIGMA_USER_INFO_FAILED);
})
.body(FigmaUserResponse.class);

if (figmaUserResponse == null) {
throw new CustomException(ErrorCode.FIGMA_USER_INFO_NOT_FOUND);
}

return new UserInfoResponse(
figmaUserResponse.handle(),
figmaUserResponse.email(),
figmaUserResponse.img_url(),
figmaUserResponse.id());

} catch (Exception e) {
log.error("Figma 유저 정보 조회 중 예외 발생 : " + e.getMessage(), e);
throw new CustomException(ErrorCode.FIGMA_USER_INFO_FAILED);
}
}

public FigmaTokenReissueResponse reissueAccessToken(String refreshToken) {
try {
String figmaInfo = figmaProperties.id() + ":" + figmaProperties.secret();

MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add(REFRESH_TOKEN, refreshToken);

return restClient
.post()
.uri(
uriBuilder ->
uriBuilder
.scheme(HTTPS_SCHEME)
.host(FIGMA_HOST)
.path(FIGMA_TOKEN_REISSUE_URL)
.build())
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.header(
HttpHeaders.AUTHORIZATION,
BASIC_TOKEN_PREFIX
+ Base64.getEncoder().encodeToString(figmaInfo.getBytes()))
.body(formData)
.retrieve()
.onStatus(
status -> !status.is2xxSuccessful(),
(request, response) -> {
log.error("Figma 토큰 재발급 요청 실패: {}", response.getStatusCode());
throw new CustomException(ErrorCode.FIGMA_TOKEN_REISSUE_FAILED);
})
.body(FigmaTokenReissueResponse.class);
} catch (Exception e) {
log.error("Figma 토큰 재발급 중 예외 발생 : {}", e.getMessage(), e);
throw new CustomException(ErrorCode.FIGMA_TOKEN_REISSUE_FAILED);
}
}

public GetFileInfoResponse getFileInfo(String fileId, String accessToken) {
try {
return restClient
.get()
.uri(
uriBuilder ->
uriBuilder
.scheme(HTTPS_SCHEME)
.host(FIGMA_HOST)
.path(FIGMA_FILE_INFO_URL)
.queryParam("depth", "1")
.build(fileId))
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.AUTHORIZATION, TOKEN_PREFIX + accessToken)
.retrieve()
.onStatus(
status -> !status.is2xxSuccessful(),
(request, response) -> {
log.error("Figma 토큰 재발급 요청 실패: {}", response.getStatusCode());
throw new CustomException(ErrorCode.GETTING_FIGMA_FILE_INFO_FAILED);
})
.body(GetFileInfoResponse.class);
} catch (Exception e) {
log.error("Figma 토큰 재발급 중 예외 발생 : {}", e.getMessage(), e);
throw new CustomException(ErrorCode.GETTING_FIGMA_FILE_INFO_FAILED);
}
}
}
108 changes: 0 additions & 108 deletions src/main/java/gigedi/dev/domain/auth/application/FigmaService.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public GoogleLoginResponse getIdTokenByGoogleLogin(String code) {
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
formData.add(CODE_KEY, code);
formData.add(CLIENT_ID_KEY, googleProperties.getId());
formData.add(CLIENT_ID_SECRET, googleProperties.getSecret());
formData.add(CLIENT_SECRET_KEY, googleProperties.getSecret());
formData.add(REDIRECT_URI_KEY, googleProperties.getRedirectUri());
formData.add(GRANT_TYPE_KEY, googleProperties.getGrantType());

Expand Down Expand Up @@ -77,7 +77,7 @@ private GoogleReissueResponse getAccessTokenWithRefreshToken(String refreshToken
formData.add(GRANT_TYPE_KEY, REISSUE_GRANT_TYPE_VALUE);
formData.add(REFRESH_TOKEN, refreshToken);
formData.add(CLIENT_ID_KEY, googleProperties.getId());
formData.add(CLIENT_ID_SECRET, googleProperties.getSecret());
formData.add(CLIENT_SECRET_KEY, googleProperties.getSecret());

return restClient
.post()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ public interface FigmaRepository extends JpaRepository<Figma, Long> {

Optional<Figma> findByFigmaIdAndDeletedAtIsNull(Long figmaId);

Optional<Figma> findByFigmaUserId(String figmaUserId);
Optional<Figma> findByFigmaUserIdAndDeletedAtIsNull(String figmaUserId);
}
8 changes: 4 additions & 4 deletions src/main/java/gigedi/dev/domain/auth/domain/Figma.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class Figma extends BaseTimeEntity {

@Column private LocalDateTime deletedAt;

private String accessToken;
@Column(nullable = false)
private String refreshToken;

@ManyToOne(fetch = FetchType.LAZY)
Expand All @@ -57,14 +57,12 @@ private Figma(
String email,
String figmaProfile,
String figmaUserId,
String accessToken,
String refreshToken,
Member member) {
this.figmaName = figmaName;
this.email = email;
this.figmaProfile = figmaProfile;
this.figmaUserId = figmaUserId;
this.accessToken = accessToken;
this.refreshToken = refreshToken;
this.member = member;
}
Expand All @@ -74,13 +72,15 @@ public static Figma createFigma(
String email,
String figmaProfile,
String figmaUserId,
Member member) {
Member member,
String refreshToken) {
return Figma.builder()
.figmaName(figmaName)
.email(email)
.figmaProfile(figmaProfile)
.figmaUserId(figmaUserId)
.member(member)
.refreshToken(refreshToken)
.build();
}
}
Loading

0 comments on commit daf06f9

Please sign in to comment.