diff --git a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java index 91d662e..b4a9b87 100644 --- a/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java +++ b/src/main/java/com/kumofactory/cloud/blueprint/dto/aws/AwsBluePrintDto.java @@ -13,23 +13,24 @@ @Data public class AwsBluePrintDto { - private String name; - private List components; - private List links; - - public static List awsComponentDtosMapper(List awsComponentDtos) { - List awsComponentDtoList = new ArrayList<>(); - for (AwsComponent awsComponent : awsComponentDtos) { - awsComponentDtoList.add(AwsComponentDto.mapper(awsComponent)); - } - return awsComponentDtoList; - } - - public static List componentLinkDtoListMapper(List componentLinks) { - List componentLinkDtoList = new ArrayList<>(); - for (ComponentLine pointLink : componentLinks) { - componentLinkDtoList.add(ComponentLineDto.mapper(pointLink)); - } - return componentLinkDtoList; - } + private Long id; + private String name; + private List components; + private List links; + + public static List awsComponentDtosMapper(List awsComponentDtos) { + List awsComponentDtoList = new ArrayList<>(); + for (AwsComponent awsComponent : awsComponentDtos) { + awsComponentDtoList.add(AwsComponentDto.mapper(awsComponent)); + } + return awsComponentDtoList; + } + + public static List componentLinkDtoListMapper(List componentLinks) { + List componentLinkDtoList = new ArrayList<>(); + for (ComponentLine pointLink : componentLinks) { + componentLinkDtoList.add(ComponentLineDto.mapper(pointLink)); + } + return componentLinkDtoList; + } } diff --git a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java index 6030539..dc45521 100644 --- a/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java +++ b/src/main/java/com/kumofactory/cloud/global/middleware/auth/AuthorizationFromTokenAspect.java @@ -32,14 +32,17 @@ public Object applyAuthorizationFromToken(ProceedingJoinPoint joinPoint, Authori ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = Objects.requireNonNull(attributes).getRequest(); String userId = getUserFromAccessToken(request.getHeader("Authorization")); - + if (userId == null) { + throw new RuntimeException("유효하지 않은 토큰입니다."); + } // 추출된 사용자 정보를 매개변수로 전달 Object[] args = joinPoint.getArgs(); args[0] = userId; return joinPoint.proceed(args); } - + + // user oauth id 추출 private String getUserFromAccessToken(String token) { String accessToken = token.split(" ")[1]; boolean isValidate = jwtTokenProvider.validateAccessToken(accessToken); diff --git a/src/main/java/com/kumofactory/cloud/member/MemberRepository.java b/src/main/java/com/kumofactory/cloud/member/MemberRepository.java index 594b50e..9e0024f 100644 --- a/src/main/java/com/kumofactory/cloud/member/MemberRepository.java +++ b/src/main/java/com/kumofactory/cloud/member/MemberRepository.java @@ -6,5 +6,7 @@ @Repository public interface MemberRepository extends JpaRepository { - Member findMemberById(Long id); + Member findMemberById(Long id); + + Member findMemberByOauthId(String oauth_id); } diff --git a/src/main/java/com/kumofactory/cloud/member/domain/Member.java b/src/main/java/com/kumofactory/cloud/member/domain/Member.java index ab53028..0055d94 100644 --- a/src/main/java/com/kumofactory/cloud/member/domain/Member.java +++ b/src/main/java/com/kumofactory/cloud/member/domain/Member.java @@ -20,22 +20,22 @@ @Setter public class Member { - @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; - private String oauth_id; + private String oauthId; - private String provider; + private String provider; - @OneToMany(mappedBy = "member") - private List bluePrints; + @OneToMany(mappedBy = "member") + private List bluePrints; - // =========== 생성함수 =========== // - public static Member createMember(UserInfoDto userInfoDto) { - Member member = new Member(); - member.setOauth_id(userInfoDto.id()); - member.setProvider(userInfoDto.provider()); - return member; - } + // =========== 생성함수 =========== // + public static Member createMember(UserInfoDto userInfoDto) { + Member member = new Member(); + member.setOauthId(userInfoDto.id()); + member.setProvider(userInfoDto.provider()); + return member; + } } diff --git a/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java b/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java index ac3a01e..19dcd74 100644 --- a/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java +++ b/src/main/java/com/kumofactory/cloud/oauth/service/OAuthService.java @@ -4,9 +4,12 @@ import com.kumofactory.cloud.config.OAuthProvider; import com.kumofactory.cloud.jwt.dto.TokenDto; import com.kumofactory.cloud.jwt.provider.JwtTokenProvider; +import com.kumofactory.cloud.member.MemberRepository; +import com.kumofactory.cloud.member.domain.Member; import com.kumofactory.cloud.oauth.dto.UserInfoDto; import com.kumofactory.cloud.oauth.service.github.GitHubService; import com.kumofactory.cloud.oauth.service.google.GoogleService; + import static com.kumofactory.cloud.oauth.dto.OAuthDto.*; import lombok.extern.slf4j.Slf4j; @@ -25,79 +28,88 @@ @Slf4j @RequiredArgsConstructor public class OAuthService { - private final GoogleService googleService; - private final GitHubService githubService; - - private final JwtTokenProvider jwtTokenProvider; - - private TokenDto token; - - public ResponseEntity request(OAuthProvider provider, String code) - throws JsonProcessingException { - - HttpHeaders responseHeaders = new HttpHeaders(); - - switch (provider) { - case GOOGLE:{ - token = getAccessTokenFromGoogle(code); - break; - } - case GITHUB:{ - token = getAccessTokenFromGitHub(code); - break; - } - default:{ - throw new IllegalArgumentException("Unknown type of social login."); - } - } - - responseHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json"); - - ResponseCookie refreshTokenCookie = ResponseCookie - .from("refreshToken", token.getRefreshToken()) - .httpOnly(true) - .path("/") - .maxAge(Duration.ofDays(365)) - .build(); - - responseHeaders.add(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString()); - - Map map = new HashMap<>(); - map.put("accessToken", token.getAccessToken()); - - return ResponseEntity.ok() - .headers(responseHeaders) - .body(new ObjectMapper().writeValueAsString(map)); - } - - // TODO : 중복된 코드 간소화 (getAccessTokenFromXXX) - private TokenDto getAccessTokenFromGoogle(String code) - throws JsonProcessingException{ - - GoogleToken accessTokenFromGoogle = googleService.requestAccessToken(code); - if(accessTokenFromGoogle != null) { - - UserInfoDto userInfo = googleService.requestUserInfo(accessTokenFromGoogle.accessToken()); - TokenDto token = jwtTokenProvider.create(userInfo.id()); - - return token; - } - assert accessTokenFromGoogle != null; - return null; - } - - private TokenDto getAccessTokenFromGitHub(String code) - throws JsonProcessingException{ - - GitHubToken accessTokenFromGithub = githubService.requestAccessToken(code); - if(accessTokenFromGithub != null) { - - UserInfoDto userInfo = githubService.requestUserInfo(accessTokenFromGithub.accessToken()); - TokenDto token = jwtTokenProvider.create(userInfo.id()); - - return token; - } - assert accessTokenFromGithub != null; - return null; - } + private final GoogleService googleService; + private final GitHubService githubService; + + private final JwtTokenProvider jwtTokenProvider; + private final MemberRepository memberRepository; + + private TokenDto token; + + public ResponseEntity request(OAuthProvider provider, String code) + throws JsonProcessingException { + + HttpHeaders responseHeaders = new HttpHeaders(); + + switch (provider) { + case GOOGLE: { + token = getAccessTokenFromGoogle(code); + break; + } + case GITHUB: { + token = getAccessTokenFromGitHub(code); + break; + } + default: { + throw new IllegalArgumentException("Unknown type of social login."); + } + } + + responseHeaders.add(HttpHeaders.CONTENT_TYPE, "application/json"); + + ResponseCookie refreshTokenCookie = ResponseCookie + .from("refreshToken", token.getRefreshToken()) + .httpOnly(true) + .path("/") + .maxAge(Duration.ofDays(365)) + .build(); + + responseHeaders.add(HttpHeaders.SET_COOKIE, refreshTokenCookie.toString()); + + Map map = new HashMap<>(); + map.put("accessToken", token.getAccessToken()); + + return ResponseEntity.ok() + .headers(responseHeaders) + .body(new ObjectMapper().writeValueAsString(map)); + } + + // TODO : 중복된 코드 간소화 (getAccessTokenFromXXX) + private TokenDto getAccessTokenFromGoogle(String code) + throws JsonProcessingException { + + GoogleToken accessTokenFromGoogle = googleService.requestAccessToken(code); + if (accessTokenFromGoogle != null) { + + UserInfoDto userInfo = googleService.requestUserInfo(accessTokenFromGoogle.accessToken()); + saveMember(userInfo); + + return jwtTokenProvider.create(userInfo.id()); + } + return null; + } + + private TokenDto getAccessTokenFromGitHub(String code) + throws JsonProcessingException { + + GitHubToken accessTokenFromGithub = githubService.requestAccessToken(code); + if (accessTokenFromGithub != null) { + + UserInfoDto userInfo = githubService.requestUserInfo(accessTokenFromGithub.accessToken()); + saveMember(userInfo); + + + return jwtTokenProvider.create(userInfo.id()); + } + return null; + } + + // 처음 가입한 member 일때만 저장 + private void saveMember(UserInfoDto userInfo) { + Member member = memberRepository.findMemberByOauthId(userInfo.id()); + if (member == null) { + member = Member.createMember(userInfo); + memberRepository.save(member); + } + } }