Skip to content

Commit

Permalink
Merge pull request #80 from KCY-Fit-a-Pet/feat/79
Browse files Browse the repository at this point in the history
โœจ ๋ชจ๋“  ๋กœ๊ทธ์ธ & ํšŒ์›๊ฐ€์ž… ๋กœ์ง์—์„œ ์„ฑ๊ณต ์‹œ user pk ๋ฐ˜ํ™˜
  • Loading branch information
heejinnn authored Jan 16, 2024
2 parents fc4a3a9 + c37dacd commit 055cbf7
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 33 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@
- [ ] ์‹ค์ œ ์„œ๋น„์Šค๋ฅผ ๊ณต๊ฐœ์ ์œผ๋กœ ๋ฐฐํฌํ•˜๊ณ  ์šด์˜ํ•˜๋Š” ๊ฒฝํ—˜์„ ํ•ด๋ณด์•˜๋‹ค.
- [ ] ์œ ์ €์˜ ํ”ผ๋“œ๋ฐฑ์— ๋”ฐ๋ผ ์„ฑ๋Šฅ/์‚ฌ์šฉ์„ฑ์„ ๊ฐœ์„ ํ•˜๊ณ  ์‹ ๊ทœ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•ด๋ณด์•˜๋‹ค.
- [ ] ๋ฐœ๊ฒฌ๋˜๋Š” ๋ฒ„๊ทธ์™€ ๊ฐœ์„ ์‚ฌํ•ญ๋“ค์„ ์ •๋ฆฌํ•˜๊ณ  ์Œ“์ธ ์ด์Šˆ๋“ค์„ ์ฒด๊ณ„์ ์œผ๋กœ ๊ด€๋ฆฌํ•ด๋ณด์•˜๋‹ค.
- [ ] ์ฝ”๋“œ๋ฅผ ์ง€์†์ ์œผ๋กœ ๋ฆฌํŒฉํ† ๋งํ•˜๊ณ  ๋””์ž์ธ ํŒจํ„ด์„ ์ ์šฉํ•ด๋ณด์•˜๋‹ค.
- [ ] ์œ„์˜ ์‹œ๋„์—์„œ ๋” ์ข‹์€ ์„ค๊ณ„์™€ ๋” ๋น ๋ฅธ ๊ฐœ๋ฐœ ์‚ฌ์ด์˜ ํŠธ๋ ˆ์ด๋“œ ์˜คํ”„๋ฅผ ๊ณ ๋ฏผํ•ด๋ณธ ์ ์ด ์žˆ๋‹ค.
- [X] ์ฝ”๋“œ๋ฅผ ์ง€์†์ ์œผ๋กœ ๋ฆฌํŒฉํ† ๋งํ•˜๊ณ  ๋””์ž์ธ ํŒจํ„ด์„ ์ ์šฉํ•ด๋ณด์•˜๋‹ค.
- [X] ์œ„์˜ ์‹œ๋„์—์„œ ๋” ์ข‹์€ ์„ค๊ณ„์™€ ๋” ๋น ๋ฅธ ๊ฐœ๋ฐœ ์‚ฌ์ด์˜ ํŠธ๋ ˆ์ด๋“œ ์˜คํ”„๋ฅผ ๊ณ ๋ฏผํ•ด๋ณธ ์ ์ด ์žˆ๋‹ค.
- [ ] ๋ฐ˜๋ณต๋˜๋Š” ์ˆ˜์ •๊ณผ ๋ฐฐํฌ์— ์ˆ˜๋ฐ˜๋˜๋Š” ์ž‘์—…๋“ค์„ ์ž๋™ํ™” ํ•ด๋ณด์•˜๋‹ค.
- [ ] ์–ธ์–ด๋‚˜ ํ”„๋ ˆ์ž„์›Œํฌ๋งŒ์œผ๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์—†๋Š” ๊ฒƒ๋“ค์„ ์ง์ ‘ ๊ตฌํ˜„ํ•ด๋ณด์•˜๋‹ค.
- [ ] ๋‚ด๊ฐ€ ์‚ฌ์šฉํ•œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋‚˜ ํ”„๋ ˆ์ž„ ์›Œํฌ์˜ ํ•œ๊ณ„๋ฅผ ๋Š๋ผ๊ณ  ๊ฐœ์„ ํ•ด๋ณด์•˜๋‹ค.
Expand All @@ -86,7 +86,7 @@
- WAS Server ๋‚ด๋ถ€์— Nginx๋ฅผ ํ†ตํ•ด Reverse Proxy๋ฅผ ๊ตฌํ˜„ํ–ˆ์Šต๋‹ˆ๋‹ค.

## ERD
<div align="center"><img src="https://github.com/KCY-Fit-a-Pet/fit-a-pet-client/assets/96044622/25596514-4b67-4ccb-9186-4197f0facb3d"></img></div>
<div align="center"><img src="https://github.com/KCY-Fit-a-Pet/fit-a-pet-server/assets/96044622/9b75726d-1695-4459-8c3f-72f664d6d036"></img></div>

- ํ˜„์žฌ ๋งŽ์€ ๋ถ€๋ถ„์ด ์ˆ˜์ •๋˜์—ˆ๊ณ , ์•ž์œผ๋กœ๋„ ๊ณ„์† ์ˆ˜์ •๋  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.

Expand Down
23 changes: 13 additions & 10 deletions src/main/java/com/kcy/fitapet/domain/member/api/AuthApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public class AuthApi {
@PostMapping("/register")
@PreAuthorize("isAnonymous()")
public ResponseEntity<?> signUp(@RequestHeader("Authorization") @NotBlank String accessToken, @RequestBody @Valid SignUpReq dto) {
Jwt tokens = memberAuthService.register(accessToken, dto);
return getResponseEntity(tokens);
Pair<Long, Jwt> result = memberAuthService.register(accessToken, dto);
return getResponseEntity(result.getKey(), result.getValue());
}

@Operation(summary = "ํšŒ์›๊ฐ€์ž… ์ „ํ™”๋ฒˆํ˜ธ ์ธ์ฆ")
Expand Down Expand Up @@ -138,11 +138,8 @@ public ResponseEntity<?> searchSmsAuthorization(
@PreAuthorize("isAnonymous()")
public ResponseEntity<?> signIn(@RequestBody @Valid SignInReq dto) {
Pair<Long, Jwt> result = memberAuthService.login(dto);
ResponseCookie cookie = cookieUtil.createCookie(REFRESH_TOKEN.getValue(), result.getValue().refreshToken(), 60 * 60 * 24 * 7);
return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, cookie.toString())
.header(ACCESS_TOKEN.getValue(), result.getValue().accessToken())
.body(SuccessResponse.from(Map.of("userId", result.getKey())));

return getResponseEntity(result.getKey(), result.getValue());
}

@Operation(summary = "๋กœ๊ทธ์•„์›ƒ", description = "์•ก์„ธ์Šค ํ† ํฐ๊ณผ ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ์„ ๋งŒ๋ฃŒ์‹œํ‚ต๋‹ˆ๋‹ค.")
Expand Down Expand Up @@ -188,7 +185,13 @@ public ResponseEntity<?> signOut(
@PreAuthorize("isAnonymous()")
public ResponseEntity<?> refresh(@CookieValue("refreshToken") @Valid String refreshToken) {
Jwt tokens = memberAuthService.refresh(refreshToken);
return getResponseEntity(tokens);

ResponseCookie cookie = cookieUtil.createCookie(REFRESH_TOKEN.getValue(), tokens.refreshToken(), 60 * 60 * 24 * 7);

return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, cookie.toString())
.header(ACCESS_TOKEN.getValue(), tokens.accessToken())
.body(SuccessResponse.noContent());
}

@Operation(summary = "ํ† ํฐ ๊ฒ€์ฆ", description = "์•ก์„ธ์Šค ํ† ํฐ์˜ ์œ ํšจ์„ฑ์„ ๊ฒ€์‚ฌํ•ฉ๋‹ˆ๋‹ค.")
Expand All @@ -204,12 +207,12 @@ public ResponseEntity<?> verify(@AccessTokenInfo AccessToken accessToken) {
* @param tokens : ์•ก์„ธ์Šค ํ† ํฐ๊ณผ ๋ฆฌํ”„๋ ˆ์‹œ ํ† ํฐ
* @return ResponseEntity<?>
*/
private ResponseEntity<?> getResponseEntity(Jwt tokens) {
private ResponseEntity<?> getResponseEntity(Long userId, Jwt tokens) {
ResponseCookie cookie = cookieUtil.createCookie(REFRESH_TOKEN.getValue(), tokens.refreshToken(), 60 * 60 * 24 * 7);

return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, cookie.toString())
.header(ACCESS_TOKEN.getValue(), tokens.accessToken())
.body(SuccessResponse.noContent());
.body(SuccessResponse.from(Map.of("userId", userId)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class MemberAuthService {
private final PasswordEncoder bCryptPasswordEncoder;

@Transactional
public Jwt register(String requestSmsAccessToken, SignUpReq dto) {
public Pair<Long, Jwt> register(String requestSmsAccessToken, SignUpReq dto) {
String accessToken = jwtMapper.getProvider(SMS_AUTH_TOKEN).resolveToken(requestSmsAccessToken);

if (forbiddenTokenService.isForbidden(accessToken))
Expand All @@ -76,7 +76,7 @@ public Jwt register(String requestSmsAccessToken, SignUpReq dto) {
jwtMapper.getProvider(SMS_AUTH_TOKEN).getExpiryDate(accessToken), false)
);

return generateToken(JwtUserInfo.from(registeredMember));
return Pair.of(registeredMember.getId(), generateToken(JwtUserInfo.from(registeredMember)));
}

@Transactional
Expand Down
23 changes: 12 additions & 11 deletions src/main/java/com/kcy/fitapet/domain/oauth/api/OauthApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.hc.core5.http.HttpStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseCookie;
Expand Down Expand Up @@ -50,15 +51,15 @@ public ResponseEntity<?> signIn(
@RequestParam("provider") ProviderType provider,
@RequestBody @Valid OauthSignInReq req
) {
Optional<Jwt> jwt;
Optional<Pair<Long, Jwt>> jwt;
if (ProviderType.NAVER.equals(provider)) {
return null; // TODO: 2023-12-24 ๋„ค์ด๋ฒ„ ๋กœ๊ทธ์ธ ๊ตฌํ˜„
} else {
jwt = oAuthService.signInByOIDC(req.id(), req.idToken(), provider, req.nonce());
}

return jwt.isPresent()
? getJwtResponseEntity(jwt.get())
? getJwtResponseEntity(jwt.get().getKey(), jwt.get().getValue())
: ResponseEntity.ok(SuccessResponse.from(Map.of("id", req.id())));
}

Expand All @@ -76,14 +77,14 @@ public ResponseEntity<?> signUp(
@RequestHeader("Authorization") String accessToken,
@RequestBody @Valid OauthSignUpReq req
) {
Jwt jwt;
Pair<Long, Jwt> jwt;
if (ProviderType.NAVER.equals(provider)) {
return null; // TODO: 2023-12-24 ๋„ค์ด๋ฒ„ ๋กœ๊ทธ์ธ ๊ตฌํ˜„
} else {
jwt = oAuthService.signUpByOIDC(id, provider, accessToken, req);
}

return getJwtResponseEntity(jwt);
return getJwtResponseEntity(jwt.getKey(), jwt.getValue());
}

@Operation(summary = "OAuth ํšŒ์›๊ฐ€์ž… ์ „ํ™”๋ฒˆํ˜ธ ์ธ์ฆ")
Expand All @@ -105,23 +106,23 @@ public ResponseEntity<?> signUpSmsAuthorization(
return ResponseEntity.ok(SuccessResponse.from(smsRes));
}

Jwt token = oAuthService.checkCertificationNumber(req, id, code, provider);
if (token == null)
Pair<Long, Jwt> token = oAuthService.checkCertificationNumber(req, id, code, provider);
if (token.getValue() == null)
return ResponseEntity.status(HttpStatus.SC_UNAUTHORIZED).build();
else if (token.refreshToken() == null)
else if (token.getValue().refreshToken() == null)
return ResponseEntity.ok()
.header(ACCESS_TOKEN.getValue(), token.accessToken())
.header(ACCESS_TOKEN.getValue(), token.getValue().accessToken())
.body(SuccessResponse.from(Map.of("member", "์‹ ๊ทœ ํšŒ์›")));

return getJwtResponseEntity(token);
return getJwtResponseEntity(token.getKey(), token.getValue());
}

private ResponseEntity<?> getJwtResponseEntity(Jwt jwt) {
private ResponseEntity<?> getJwtResponseEntity(Long userId, Jwt jwt) {
ResponseCookie cookie = cookieUtil.createCookie(REFRESH_TOKEN.getValue(), jwt.refreshToken(), 60 * 60 * 24 * 7);

return ResponseEntity.ok()
.header(HttpHeaders.SET_COOKIE, cookie.toString())
.header(ACCESS_TOKEN.getValue(), jwt.accessToken())
.body(SuccessResponse.noContent());
.body(SuccessResponse.from(Map.of("userId", userId)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.kcy.fitapet.global.common.util.sms.dto.SmsRes;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand Down Expand Up @@ -59,22 +60,22 @@ public class OauthService {
private final SmsRedisHelper smsRedisHelper;

@Transactional
public Optional<Jwt> signInByOIDC(String id, String idToken, ProviderType provider, String nonce) {
public Optional<Pair<Long, Jwt>> signInByOIDC(String id, String idToken, ProviderType provider, String nonce) {
OIDCDecodePayload payload = getPayload(provider, idToken, nonce);
log.info("payload : {}", payload);
isValidRequestId(id, payload.sub());

if (oauthSearchService.isExistMember(new BigInteger(id), provider)) {
Member member = oauthSearchService.findMemberByOauthIdAndProvider(new BigInteger(id), provider);
return Optional.of(generateToken(JwtUserInfo.from(member)));
return Optional.of(Pair.of(member.getId(), generateToken(JwtUserInfo.from(member))));
} else {
oidcTokenService.saveOIDCToken(idToken, provider, id);
return Optional.empty();
}
}

@Transactional
public Jwt signUpByOIDC(String id, ProviderType provider, String requestOauthAccessToken, OauthSignUpReq req) {
public Pair<Long, Jwt> signUpByOIDC(String id, ProviderType provider, String requestOauthAccessToken, OauthSignUpReq req) {
String accessToken = jwtMapper.getProvider(AuthConstants.SMS_OAUTH_TOKEN).resolveToken(requestOauthAccessToken);
JwtSubInfo subs = jwtMapper.getProvider(AuthConstants.SMS_OAUTH_TOKEN).getSubInfoFromToken(accessToken);
String phone = getPhoneByTopic(subs.phoneNumber());
Expand All @@ -98,7 +99,7 @@ public Jwt signUpByOIDC(String id, ProviderType provider, String requestOauthAcc

log.info("success oauth signup member id : {} - oauth id : {} [provider: {}]",
member.getId(), oauthAccount.getOauthId(), oauthAccount.getProvider());
return generateToken(JwtUserInfo.from(member));
return Pair.of(member.getId(), generateToken(JwtUserInfo.from(member)));
}

@Transactional
Expand All @@ -113,7 +114,7 @@ public SmsRes sendCode(OauthSmsReq dto, ProviderType provider) {
}

@Transactional
public Jwt checkCertificationNumber(OauthSmsReq req, String id, String code, ProviderType provider) {
public Pair<Long, Jwt> checkCertificationNumber(OauthSmsReq req, String id, String code, ProviderType provider) {
String key = makeTopic(req.to(), provider);
log.info("key: {}", key);
if (!smsRedisHelper.isCorrectCode(key, code, SmsPrefix.OAUTH)) {
Expand All @@ -130,10 +131,10 @@ public Jwt checkCertificationNumber(OauthSmsReq req, String id, String code, Pro
oauthAccount.updateMember(member);
oidcTokenService.deleteOIDCToken(req.idToken());

return generateToken(JwtUserInfo.from(member));
return Pair.of(member.getId(), generateToken(JwtUserInfo.from(member)));
}

return Jwt.of(jwtMapper.getProvider(SMS_OAUTH_TOKEN).generateToken(SmsOauthInfo.of(id, key)), null);
return Pair.of(0L, Jwt.of(jwtMapper.getProvider(SMS_OAUTH_TOKEN).generateToken(SmsOauthInfo.of(id, key)), null));
}

/**
Expand Down

0 comments on commit 055cbf7

Please sign in to comment.