Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/depromeet/15th-team2-BE
Browse files Browse the repository at this point in the history
…into feature/94-member-update
  • Loading branch information
ywonchae1 committed Jul 30, 2024
2 parents 1475a5e + a8e5763 commit 449993d
Show file tree
Hide file tree
Showing 158 changed files with 1,811 additions and 1,514 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ generated
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/
**/generated_tests

### IntelliJ IDEA ###
.idea
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.depromeet.auth.port.in.usecase;

import com.depromeet.auth.vo.AccessTokenInfo;
import com.depromeet.auth.vo.JwtToken;
import com.depromeet.member.domain.MemberRole;

public interface CreateTokenUseCase {
JwtToken generateToken(Long memberId, MemberRole memberRole);

AccessTokenInfo generateAccessToken(String refreshToken);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.depromeet.auth.port.in.usecase;

import com.depromeet.auth.vo.kakao.KakaoAccountProfile;
import com.depromeet.dto.auth.AccountProfileResponse;

public interface SocialUseCase {
AccountProfileResponse getGoogleAccountProfile(String code);

KakaoAccountProfile getKakaoAccountProfile(String code);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.depromeet.auth.port.out;

import com.depromeet.dto.auth.AccountProfileResponse;

public interface GooglePort {
AccountProfileResponse getGoogleAccountProfile(String code);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.depromeet.auth.port.out;

import com.depromeet.auth.vo.kakao.KakaoAccountProfile;

public interface KakaoPort {
KakaoAccountProfile getKakaoAccountProfile(final String code);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.depromeet.auth.port.out;

import com.depromeet.auth.vo.AccessTokenInfo;
import com.depromeet.auth.vo.RefreshTokenInfo;
import com.depromeet.member.domain.MemberRole;
import java.util.Optional;

public interface SecurityPort {
AccessTokenInfo generateAccessToken(Long memberId, MemberRole memberRole);

RefreshTokenInfo generateRefreshToken(Long memberId, MemberRole memberRole);

String findTokenType(String token);

Optional<AccessTokenInfo> parseAccessToken(String token);

Optional<RefreshTokenInfo> parseRefreshToken(String token);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.depromeet.auth.service;

import com.depromeet.auth.port.in.usecase.CreateTokenUseCase;
import com.depromeet.auth.port.out.SecurityPort;
import com.depromeet.auth.vo.AccessTokenInfo;
import com.depromeet.auth.vo.JwtToken;
import com.depromeet.auth.vo.RefreshTokenInfo;
import com.depromeet.constant.SecurityConstant;
import com.depromeet.exception.ForbiddenException;
import com.depromeet.exception.NotFoundException;
import com.depromeet.exception.UnauthorizedException;
import com.depromeet.member.domain.Member;
import com.depromeet.member.domain.MemberRole;
import com.depromeet.member.port.out.persistence.MemberPersistencePort;
import com.depromeet.type.auth.AuthErrorType;
import com.depromeet.type.member.MemberErrorType;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class JwtTokenService implements CreateTokenUseCase {
private final SecurityPort securityPort;
private final MemberPersistencePort memberPersistencePort;

public JwtToken generateToken(Long memberId, MemberRole memberRole) {
AccessTokenInfo accessTokenInfo = securityPort.generateAccessToken(memberId, memberRole);
RefreshTokenInfo refreshTokenInfo = securityPort.generateRefreshToken(memberId, memberRole);

memberPersistencePort.updateRefresh(memberId, refreshTokenInfo.refreshToken());

return new JwtToken(
memberId,
SecurityConstant.BEARER_PREFIX.getValue() + accessTokenInfo.accessToken(),
SecurityConstant.BEARER_PREFIX.getValue() + refreshTokenInfo.refreshToken());
}

public String findTokenType(String token) {
return securityPort.findTokenType(token);
}

public Optional<AccessTokenInfo> parseAccessToken(String token) {
return securityPort.parseAccessToken(token);
}

public Optional<RefreshTokenInfo> parseRefreshToken(String token) {
return securityPort.parseRefreshToken(token);
}

public AccessTokenInfo generateAccessToken(String refreshToken) {
return reissueAccessToken(refreshToken);
}

public AccessTokenInfo reissueAccessToken(String refreshToken) {
RefreshTokenInfo refreshTokenInfo =
parseRefreshToken(refreshToken)
.orElseThrow(
() ->
new UnauthorizedException(
AuthErrorType.INVALID_JWT_REFRESH_TOKEN));

Long memberId = refreshTokenInfo.memberId();
Member member =
memberPersistencePort
.findById(memberId)
.orElseThrow(() -> new NotFoundException(MemberErrorType.NOT_FOUND));

if (member.getRefreshToken() != null && member.getRefreshToken().equals(refreshToken)) {
MemberRole memberRole = member.getRole();
return securityPort.generateAccessToken(memberId, memberRole);
} else {
throw new ForbiddenException(AuthErrorType.REFRESH_TOKEN_NOT_MATCH);
}
}

public RefreshTokenInfo retrieveRefreshToken(
RefreshTokenInfo refreshTokenInfoDto, String refreshToken) {
Member member =
memberPersistencePort
.findById(refreshTokenInfoDto.memberId())
.orElseThrow(() -> new NotFoundException(MemberErrorType.NOT_FOUND));
if (member.getRefreshToken() != null && member.getRefreshToken().equals(refreshToken)) {
return refreshTokenInfoDto;
}
throw new ForbiddenException(AuthErrorType.REFRESH_TOKEN_NOT_MATCH);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.depromeet.auth.service;

import com.depromeet.auth.port.in.usecase.SocialUseCase;
import com.depromeet.auth.port.out.GooglePort;
import com.depromeet.auth.port.out.KakaoPort;
import com.depromeet.auth.vo.kakao.KakaoAccountProfile;
import com.depromeet.dto.auth.AccountProfileResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class SocialService implements SocialUseCase {
private final KakaoPort kakaoPort;
private final GooglePort googlePort;

@Override
public AccountProfileResponse getGoogleAccountProfile(String code) {
return googlePort.getGoogleAccountProfile(code);
}

@Override
public KakaoAccountProfile getKakaoAccountProfile(String code) {
return kakaoPort.getKakaoAccountProfile(code);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.depromeet.auth.vo;

import com.depromeet.member.domain.MemberRole;

public record AccessTokenInfo(Long memberId, MemberRole memberRole, String accessToken) {}
11 changes: 11 additions & 0 deletions module-domain/src/main/java/com/depromeet/auth/vo/JwtToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.depromeet.auth.vo;

import java.util.Objects;

public record JwtToken(Long userId, String accessToken, String refreshToken) {
public JwtToken {
Objects.requireNonNull(userId);
Objects.requireNonNull(accessToken);
Objects.requireNonNull(refreshToken);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.depromeet.auth.vo;

import com.depromeet.member.domain.MemberRole;

public record RefreshTokenInfo(Long memberId, MemberRole memberRole, String refreshToken) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.depromeet.auth.vo.kakao;

public record KakaoAccountInfo(String email, KakaoProfileInfo profileInfo) {
public static KakaoAccountInfo of(String email, String nickname) {
return new KakaoAccountInfo(email, KakaoProfileInfo.of(nickname));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.depromeet.auth.vo.kakao;

public record KakaoAccountProfile(String id, KakaoAccountInfo accountInfo) {
public static KakaoAccountProfile of(String id, String email, String nickname) {
return new KakaoAccountProfile(id, KakaoAccountInfo.of(email, nickname));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.depromeet.auth.vo.kakao;

public record KakaoProfileInfo(String nickname) {
public static KakaoProfileInfo of(String nickname) {
return new KakaoProfileInfo(nickname);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.depromeet.image.domain;

import com.depromeet.memory.Memory;
import com.depromeet.memory.domain.Memory;
import java.util.Optional;
import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.depromeet.image.port.in;

import com.depromeet.image.domain.vo.ImagePresignedUrlVo;
import com.depromeet.memory.Memory;
import com.depromeet.memory.domain.Memory;
import java.util.List;

public interface ImageUpdateUseCase {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.depromeet.image.port.in;

import com.depromeet.image.domain.vo.ImagePresignedUrlVo;
import com.depromeet.memory.Memory;
import com.depromeet.memory.domain.Memory;
import java.util.List;

public interface ImageUploadUseCase {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.depromeet.image.port.in.ImageUpdateUseCase;
import com.depromeet.image.port.out.persistence.ImagePersistencePort;
import com.depromeet.image.port.out.s3.S3ManagePort;
import com.depromeet.memory.Memory;
import com.depromeet.memory.domain.Memory;
import com.depromeet.type.image.ImageErrorType;
import com.depromeet.util.ImageNameUtil;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.depromeet.image.port.in.ImageUploadUseCase;
import com.depromeet.image.port.out.persistence.ImagePersistencePort;
import com.depromeet.image.port.out.s3.S3ManagePort;
import com.depromeet.memory.Memory;
import com.depromeet.memory.domain.Memory;
import com.depromeet.type.image.ImageErrorType;
import com.depromeet.util.ImageNameUtil;
import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.depromeet.member;
package com.depromeet.member.domain;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.depromeet.member;
package com.depromeet.member.domain;

import com.depromeet.converter.AbstractCodedEnumConverter;
import com.depromeet.converter.CodedEnum;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.depromeet.member.port.in.command;

public record CreateMemberCommand(String name, String email) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.depromeet.member.port.in.command;

public record SocialMemberCommand(String id, String name, String email) {}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.depromeet.member.port.in.usecase;

import com.depromeet.member.domain.Member;

public interface GoalUpdateUseCase {
Member updateGoal(Long memberId, Integer goal);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.depromeet.member.port.in.usecase;

import com.depromeet.member.domain.Member;
import com.depromeet.member.port.in.command.SocialMemberCommand;

public interface MemberUseCase {
Member findById(Long id);

Member findOrCreateMemberBy(SocialMemberCommand command);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.depromeet.member.port.out.persistence;

import com.depromeet.member.domain.Member;
import java.util.Optional;

public interface MemberPersistencePort {
Optional<Member> findByEmail(String email);

Optional<Member> findById(Long id);

Member save(Member member);

void updateRefresh(Long memberId, String refreshToken);

Optional<Member> updateGoal(Long memberId, Integer goal);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.depromeet.member.service;

import com.depromeet.exception.InternalServerException;
import com.depromeet.exception.NotFoundException;
import com.depromeet.member.domain.Member;
import com.depromeet.member.domain.MemberRole;
import com.depromeet.member.port.in.command.SocialMemberCommand;
import com.depromeet.member.port.in.usecase.GoalUpdateUseCase;
import com.depromeet.member.port.in.usecase.MemberUseCase;
import com.depromeet.member.port.out.persistence.MemberPersistencePort;
import com.depromeet.type.member.MemberErrorType;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
@RequiredArgsConstructor
public class MemberService implements MemberUseCase, GoalUpdateUseCase {
private final MemberPersistencePort memberPersistencePort;

@Override
@Transactional(readOnly = true)
public Member findById(Long id) {
return memberPersistencePort
.findById(id)
.orElseThrow(() -> new NotFoundException(MemberErrorType.NOT_FOUND));
}

@Override
public Member findOrCreateMemberBy(SocialMemberCommand command) {
return memberPersistencePort
.findByEmail(command.email())
.orElseGet(
() -> {
Member member =
Member.builder()
.name(command.name())
.email(command.email())
.role(MemberRole.USER)
.build();
return memberPersistencePort.save(member);
});
}

@Override
public Member updateGoal(Long memberId, Integer goal) {
return memberPersistencePort
.updateGoal(memberId, goal)
.orElseThrow(() -> new InternalServerException(MemberErrorType.UPDATE_GOAL_FAILED));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.depromeet.memory;
package com.depromeet.memory.domain;

import com.depromeet.image.domain.Image;
import com.depromeet.member.Member;
import com.depromeet.member.domain.Member;
import com.depromeet.pool.domain.Pool;
import java.time.LocalDate;
import java.time.LocalTime;
Expand Down
Loading

0 comments on commit 449993d

Please sign in to comment.