Skip to content

Commit

Permalink
๐Ÿ”€ Merge main
Browse files Browse the repository at this point in the history
MemberService ์ถฉ๋Œ ํ•ด๊ฒฐ
  • Loading branch information
min-0 committed Sep 5, 2024
2 parents 0cc829b + 5c364fd commit 081e261
Show file tree
Hide file tree
Showing 42 changed files with 818 additions and 108 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.dnd.dndtravel.auth.controller.request.AppleWithdrawRequest;
import com.dnd.dndtravel.auth.controller.request.ReIssueTokenRequest;
import com.dnd.dndtravel.auth.controller.swagger.AuthControllerSwagger;
import com.dnd.dndtravel.auth.service.dto.response.AppleIdTokenPayload;
import com.dnd.dndtravel.auth.service.AppleOAuthService;
import com.dnd.dndtravel.auth.service.JwtTokenService;
Expand All @@ -20,19 +21,18 @@

@RequiredArgsConstructor
@RestController
public class AuthController {
public class AuthController implements AuthControllerSwagger {
private final AppleOAuthService appleOAuthService;
private final JwtTokenService jwtTokenService;
private final MemberService memberService;

//todo ํด๋ผ์ด์–ธํŠธ์—์„œ ์‹ค์ œ ์ธ์ฆ์ฝ”๋“œ ๋ณด๋‚ด์ฃผ๋ฉด ํ…Œ์ŠคํŠธ ์ง„ํ–‰ ํ•„์š”
@PostMapping("/login/oauth2/apple")
public ResponseEntity<TokenResponse> appleOAuthLogin(@RequestBody AppleLoginRequest appleLoginRequest) {
// ํด๋ผ์ด์–ธํŠธ์—์„œ ์ค€ code ๊ฐ’์œผ๋กœ apple์˜ IdToken Payload๋ฅผ ์–ป์–ด์˜จ๋‹ค
AppleIdTokenPayload tokenPayload = appleOAuthService.get(appleLoginRequest.appleToken());

// apple์—์„œ ๊ฐ€์ ธ์˜จ ์œ ์ €์ •๋ณด๋ฅผ DB์— ์ €์žฅ
Member member = memberService.saveMember(tokenPayload.name(), tokenPayload.email(), appleLoginRequest.selectedColor());
Member member = memberService.saveMember(tokenPayload.email(), appleLoginRequest.selectedColor());

// ํด๋ผ์ด์–ธํŠธ์™€ ์ฃผ๊ณ ๋ฐ›์„ user token(access , refresh) ์ƒ์„ฑ
TokenResponse tokenResponse = jwtTokenService.generateTokens(member.getId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package com.dnd.dndtravel.auth.controller.request;

//todo ํ•„๋“œ๊ฐ’๋“ค ์œ ํšจ์„ฑ ์ฒดํฌ
// todo ์ž…๋ ฅ ์ƒ‰์ƒ ์˜ˆ์‹œ๋Š” ํด๋ผ์—์„œ String ํƒ€์ž…๋“ค. RED, ORANGE, YELLOW, MELON, BLUE, PURPLE
import static io.swagger.v3.oas.annotations.media.Schema.RequiredMode.REQUIRED;

import com.dnd.dndtravel.auth.controller.request.validation.ColorValidation;
import com.dnd.dndtravel.member.domain.SelectedColor;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record AppleLoginRequest(
@Schema(description = "authorization code", requiredMode = REQUIRED)
@NotBlank(message = "authorization code๋Š” ํ•„์ˆ˜ ์ž…๋‹ˆ๋‹ค.")
@Size(max = 300, message = "authorization code ํ˜•์‹์ด ์•„๋‹™๋‹ˆ๋‹ค")
String appleToken,

@Schema(description = "์œ ์ €๊ฐ€ ์„ ํƒํ•œ ์ƒ‰์ƒ", requiredMode = REQUIRED)
@ColorValidation(enumClass = SelectedColor.class)
String selectedColor
){
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.dnd.dndtravel.auth.controller.request.validation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import jakarta.validation.Constraint;

@Constraint(validatedBy = {ColorValidator.class})
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ColorValidation {
String message() default "invalid region";
Class<? extends java.lang.Enum<?>> enumClass();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.dnd.dndtravel.auth.controller.request.validation;

import java.util.Arrays;

import com.dnd.dndtravel.map.controller.request.validation.RegionCondition;
import com.dnd.dndtravel.map.controller.request.validation.RegionEnum;
import com.dnd.dndtravel.member.domain.SelectedColor;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

public class ColorValidator implements ConstraintValidator<ColorValidation, String> {

private ColorValidation annotation;


@Override
public void initialize(ColorValidation constraintAnnotation) {
this.annotation = constraintAnnotation;
}

@Override
public boolean isValid(String color, ConstraintValidatorContext context) {
// ์ƒ‰์ƒ ์—†์œผ๋ฉด ์˜ˆ์™ธ
if (color == null || color.isBlank()) {
return false;
}

Object[] enumValues = this.annotation.enumClass().getEnumConstants();
if (enumValues == null) {
return false;
}

// ์ƒ‰์ƒ Enum์ค‘ ํ•˜๋‚˜๋ผ๋„ ํ•ด๋‹น๋˜๋ฉด true
return Arrays.stream(enumValues).anyMatch(enumValue -> SelectedColor.isMatch(color));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package com.dnd.dndtravel.auth.controller.swagger;

import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import com.dnd.dndtravel.auth.controller.request.AppleLoginRequest;
import com.dnd.dndtravel.auth.controller.request.ReIssueTokenRequest;
import com.dnd.dndtravel.auth.service.dto.response.ReissueTokenResponse;
import com.dnd.dndtravel.auth.service.dto.response.TokenResponse;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "auth", description = "์ธ์ฆ API")
public interface AuthControllerSwagger {

String STATUS_CODE_400_BODY_MESSAGE = "{\"message\":\"์ž˜๋ชป๋œ ์š”์ฒญ์ž…๋‹ˆ๋‹ค\"}";

@Operation(
summary = "์• ํ”Œ OAuth login API"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "์ •์ƒ ๋กœ๊ทธ์ธ",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE)),
@ApiResponse(responseCode = "204", description = "refresh token ์žฌ๋ฐœ๊ธ‰ ํ•„์š”์‹œ",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE)),
@ApiResponse(
responseCode = "500", description = "์„œ๋ฒ„ ์˜ค๋ฅ˜",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(example = "{\"message\":\"Internal Server Error\"}")
)
)
})
ResponseEntity<TokenResponse> appleOAuthLogin(
@Parameter(description = "๋กœ๊ทธ์ธ ์š”์ฒญ ์ •๋ณด(authorization code, color)", required = true)
AppleLoginRequest appleLoginRequest
);

@Operation(
summary = "Refresh Token ์žฌ๋ฐœ๊ธ‰ API"
)
@ApiResponses(value = {
@ApiResponse(responseCode = "200", description = "์ •์ƒ ๋ฐœ๊ธ‰",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(implementation = ReissueTokenResponse.class))),
@ApiResponse(responseCode = "400", description = "์œ ํšจํ•˜์ง€ ์•Š์€ RefreshToken ์š”์ฒญ์‹œ",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(example = STATUS_CODE_400_BODY_MESSAGE)
)
),
@ApiResponse(
responseCode = "500", description = "์„œ๋ฒ„ ์˜ค๋ฅ˜",
content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE,
schema = @Schema(example = "{\"message\":\"Internal Server Error\"}")
)
)
})
ReissueTokenResponse reissueToken(
@Parameter(description = "refreshToken", required = true)
ReIssueTokenRequest reissueTokenRequest
);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dnd.dndtravel.auth.exception;

public class AppleTokenDecodingException extends RuntimeException {
private static final String MESSAGE = "Apple ํ† ํฐ payload ํ•ด๋…์‹คํŒจ";

public AppleTokenDecodingException(Exception e) {
super(MESSAGE, e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.dnd.dndtravel.auth.exception;

import io.jsonwebtoken.JwtException;

public class JwtTokenDecodingException extends RuntimeException {
private static final String MESSAGE = "Jwt ํ† ํฐ ํ•ด๋… ์‹คํŒจ";

public JwtTokenDecodingException(JwtException e) {
super(MESSAGE, e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.dnd.dndtravel.auth.exception;

import io.jsonwebtoken.ExpiredJwtException;

public class JwtTokenExpiredException extends RuntimeException {
private static final String MESSAGE = "Jwt ํ† ํฐ์ด ๋งŒ๋ฃŒ๋˜์—ˆ์Œ";

public JwtTokenExpiredException(ExpiredJwtException e) {
super(MESSAGE, e);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dnd.dndtravel.auth.exception;

public class RefreshTokenInvalidException extends RuntimeException {
private static final String MESSAGE = "์œ ํšจํ•˜์ง€ ์•Š์€ RefreshToken ํ† ํฐ [refreshToken=%s]";

public RefreshTokenInvalidException(String refreshToken) {
super(String.format(MESSAGE, refreshToken));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@

@RequiredArgsConstructor
@Component
@Slf4j
public class AppleOAuthService {
private final AppleClient appleClient;
private final AppleProperties appleProperties;
Expand Down
9 changes: 6 additions & 3 deletions src/main/java/com/dnd/dndtravel/auth/service/JwtProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@

import javax.crypto.SecretKey;

import com.dnd.dndtravel.auth.exception.JwtTokenExpiredException;
import com.dnd.dndtravel.auth.exception.JwtTokenDecodingException;

@Component
public class JwtProvider {
private static final String CLAIM_CONTENT = "memberId";
Expand Down Expand Up @@ -49,14 +52,14 @@ public Claims parseClaims(String splitHeader) {
Jws<Claims> claims;
try {
claims = Jwts.parser()
.verifyWith(Keys.hmacShaKeyFor(Base64.getDecoder().decode(String.valueOf(this.secretKey))))
.verifyWith(secretKey)
.build()
.parseSignedClaims(splitHeader);

} catch (ExpiredJwtException e) {
throw new RuntimeException("message", e); // ์œ ํšจํ•˜์ง€ ์•Š์€ ํ† ํฐ
throw new JwtTokenExpiredException(e);
} catch (JwtException e) {
throw new RuntimeException("message", e); // ํ† ํฐ ํ•ด๋… ์‹คํŒจ
throw new JwtTokenDecodingException(e);
}

return claims.getPayload();
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/dnd/dndtravel/auth/service/JwtTokenService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.springframework.transaction.annotation.Transactional;

import com.dnd.dndtravel.auth.domain.RefreshToken;
import com.dnd.dndtravel.auth.exception.RefreshTokenInvalidException;
import com.dnd.dndtravel.auth.repository.RefreshTokenRepository;
import com.dnd.dndtravel.auth.service.dto.response.TokenResponse;
import com.dnd.dndtravel.auth.service.dto.response.ReissueTokenResponse;
Expand All @@ -16,26 +17,33 @@ public class JwtTokenService {
private final JwtProvider jwtProvider;
private final RefreshTokenRepository refreshTokenRepository;

// todo ํ˜„์žฌ refreshtokenํ•˜๋‚˜๋กœ ๊ณ„์†ํ•ด์„œ ๋ฐœ๊ธ‰ํ•ด์ฃผ๋Š” ๋ชจ๋ธ์ธ๋ฐ, ์ด๋Š” ๋ฆฌํ”„๋ ˆ์‰ฌ ํƒˆ์ทจ์‹œ ๋ณด์•ˆ์œ„ํ˜‘์žˆ์Œ. ์ถ”ํ›„ ๊ฐœ์„  ํ•„์š”
@Transactional
public TokenResponse generateTokens(Long memberId) {
RefreshToken refreshToken = refreshTokenRepository.findByMemberId(memberId);

// ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ์ด ์—†๋Š”๊ฒฝ์šฐ
if (refreshToken == null) {
String newRefreshToken = jwtProvider.refreshToken();
refreshTokenRepository.save(RefreshToken.of(memberId, newRefreshToken)); // refreshToken์€ DB์— ์ €์žฅ
return new TokenResponse(jwtProvider.accessToken(memberId), newRefreshToken);
} else if (refreshToken.isExpire()) {
}

// ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ์ด ๋งŒ๋ฃŒ๋์œผ๋ฉด ์žฌ๋ฐœ๊ธ‰ ๋ฐ›์œผ๋ผ๊ณ  ๋ฉ˜ํŠธ์คŒ
if (refreshToken.isExpire()) {
return null;
}

return new TokenResponse(jwtProvider.accessToken(memberId), null);

// ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ์ด DB์— ์กด์žฌํ•˜๊ณ  ์œ ํšจํ•œ๊ฒฝ์šฐ
refreshTokenRepository.delete(refreshToken);
String newRefreshToken = jwtProvider.refreshToken();
refreshTokenRepository.save(RefreshToken.of(refreshToken.getMemberId(), newRefreshToken));
return new TokenResponse(jwtProvider.accessToken(memberId), newRefreshToken);
}

@Transactional
public ReissueTokenResponse reIssue(String token) {
//validation
RefreshToken refreshToken = refreshTokenRepository.findByRefreshToken(token).orElseThrow(() -> new RuntimeException("์œ ํšจํ•˜์ง€ ์•Š์€ ํ† ํฐ"));
RefreshToken refreshToken = refreshTokenRepository.findByRefreshToken(token).orElseThrow(() -> new RefreshTokenInvalidException(token));

//RTR
refreshTokenRepository.delete(refreshToken);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.dnd.dndtravel.auth.service;

import java.util.Random;

import org.springframework.stereotype.Component;

@Component
public class MemberNameGenerator {
private static final String[] ADJECTIVES = {
"๊ฐ€๋ƒ˜ํ”ˆ", "๊ฐ€์—พ์€", "๊ฑฐ์„ผ", "๊ฑฐ์นœ", "๊ฑด์กฐํ•œ",
"๊ฒŒ์œผ๋ฅธ", "๊ณ ๋‹ฌํ”ˆ", "๊ท€์—ฌ์šด", "๊ทธ๋ฆฌ์šด", "๊นจ๋—ํ•œ",
"๋ˆ„๋Ÿฐ", "๋Š๋ฆฐ", "๋”๋Ÿฌ์šด", "๋œ๋œ", "๋™๊ทธ๋ž€", "๋›ฐ์–ด๋‚œ",
"๋ฌด์„œ์šด", "๋ฏธ์นœ", "๋ณด๋žŒ์ฐฌ", "๋ฝ€์–€", "๋น„์‹ผ", "์„œํˆฐ","์„ฃ๋ถ€๋ฅธ",
"์„ฑ๊ฐ€์‹ ","์ˆ˜์ค์€","์œ์‚ด๊ฐ™์€","๋ชป๋‚œ", "๋ชป์ƒ๊ธด", "๋ฌด๊ฑฐ์šด", "๋น ๋ฅธ",
"์šฉ๊ฐํ•œ", "ํ–‰๋ณตํ•œ", "์Šฌํ”ˆ", "ํƒ์Šค๋Ÿฌ์šด", "ํ•œ๊ฒฐ๊ฐ™์€","ํฌ๋ง์ฐฌ"
};

private static final String[] ANIMALS = {
"์‚ฌ์ž", "ํ˜ธ๋ž‘์ด", "๊ณฐ", "์—ฌ์šฐ", "ํ† ๋ผ",
"๊ฑฐ๋ถ์ด", "๊ณ ์–‘์ด", "๋Š‘๋Œ€", "๊ฐ•์•„์ง€", "ํŒฌ๋”",
"๋ณ‘์•„๋ฆฌ", "๋ง์•„์ง€", "์ฝ”๋ผ๋ฆฌ", "์˜ค์ง•์–ด", "๋ฐ”ํ€ด๋ฒŒ๋ ˆ",
"๊ฑฐ๋จธ๋ฆฌ", "๊ฐœ๋ฏธํ•ฅ๊ธฐ", "๋Œ๊ณ ๋ž˜", "์ˆœ๋ก", "์˜ฌ๋นผ๋ฏธ", "๋ฐ•์ฅ",
"ํŽญ๊ท„", "๋‚˜๋ฌด๋Š˜๋ณด", "์˜ค์†Œ๋ฆฌ", "ํ•˜์ด์—๋‚˜", "ํ–„์Šคํ„ฐ"
};

private final Random random = new Random();

public String generateRandomName() {
String adjective = ADJECTIVES[this.random.nextInt(ADJECTIVES.length)];
String animal = ANIMALS[this.random.nextInt(ANIMALS.length)];
return adjective + animal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Base64;

import com.dnd.dndtravel.auth.exception.AppleTokenDecodingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

Expand All @@ -25,7 +26,7 @@ public static <T> T decodePayload(String token, Class<T> targetClass) {
try {
return objectMapper.readValue(payload, targetClass);
} catch (Exception e) {
throw new RuntimeException("Error decoding token payload", e);
throw new AppleTokenDecodingException(e);
}
}
}
Loading

0 comments on commit 081e261

Please sign in to comment.