Skip to content

Commit

Permalink
fix: #11 oauth 전화번호 인증 redis 조회 key 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
psychology50 committed Dec 27, 2023
1 parent 067826d commit ec351b4
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 19 deletions.
17 changes: 8 additions & 9 deletions src/main/java/com/kcy/fitapet/domain/oauth/api/OauthApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.kcy.fitapet.global.common.response.SuccessResponse;
import com.kcy.fitapet.global.common.security.jwt.dto.Jwt;
import com.kcy.fitapet.global.common.util.cookie.CookieUtil;
import com.kcy.fitapet.global.common.util.sms.dto.SmsReq;
import com.kcy.fitapet.global.common.util.sms.dto.SmsRes;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -23,13 +22,13 @@
import org.springframework.http.ResponseCookie;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.Optional;

import static com.kcy.fitapet.global.common.security.jwt.AuthConstants.*;
import static com.kcy.fitapet.global.common.security.jwt.AuthConstants.ACCESS_TOKEN;
import static com.kcy.fitapet.global.common.security.jwt.AuthConstants.REFRESH_TOKEN;

@Tag(name = "OAuth API")
@RestController
Expand All @@ -52,16 +51,16 @@ public ResponseEntity<?> signIn(
@RequestParam("provider") ProviderType provider,
@RequestBody @Valid OauthSignInReq req
) {
Jwt jwt = null;
Optional<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 == null)
? ResponseEntity.ok(SuccessResponse.from(Map.of("id", req.id())))
: getJwtResponseEntity(jwt);
return jwt.isPresent()
? getJwtResponseEntity(jwt.get())
: ResponseEntity.ok(SuccessResponse.from(Map.of("id", req.id())));
}

@Operation(summary = "OAuth 회원가입", description = "/{id}/sms로 전화번호 인증 후, accessToken 발급이 선행되어야 한다.")
Expand All @@ -79,7 +78,7 @@ public ResponseEntity<?> signUp(
@RequestHeader("Authorization") String accessToken,
@RequestBody @Valid OauthSignUpReq req
) {
Jwt jwt = null;
Jwt jwt;
if (ProviderType.NAVER.equals(provider)) {
return null; // TODO: 2023-12-24 네이버 로그인 구현
} else {
Expand All @@ -104,7 +103,7 @@ public ResponseEntity<?> signUpSmsAuthorization(
@RequestBody @Valid OauthSmsReq req
) {
if (code == null) {
SmsRes smsRes = oAuthService.sendCode(req, id, provider);
SmsRes smsRes = oAuthService.sendCode(req, provider);
return ResponseEntity.ok(SuccessResponse.from(smsRes));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Optional;

@Service
@RequiredArgsConstructor
Expand All @@ -59,16 +60,17 @@ public class OauthService {
private final SmsRedisHelper smsRedisHelper;

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

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

Expand Down Expand Up @@ -99,7 +101,7 @@ public Jwt signUpByOIDC(Long id, ProviderType provider, String requestAccessToke
}

@Transactional
public SmsRes sendCode(OauthSmsReq dto, Long id, ProviderType provider) {
public SmsRes sendCode(OauthSmsReq dto, ProviderType provider) {
SensInfo smsInfo = smsProvider.sendCodeByPhoneNumber(dto.toSmsReq());
String key = makeTopic(dto.to(), provider);

Expand Down Expand Up @@ -140,7 +142,7 @@ private OIDCDecodePayload getPayload(ProviderType provider, String idToken, Stri

return oauthOIDCHelper.getPayloadFromIdToken(
idToken, oauthApplicationConfig.getAuthorizationUri(),
oauthApplicationConfig.getClientId(), nonce, oidcPublicKeyResponse);
oauthApplicationConfig.getClientSecret(), nonce, oidcPublicKeyResponse);
}

/**
Expand All @@ -153,7 +155,7 @@ private void isValidRequestId(Long id, Long sub) {
}

private String makeTopic(String phoneNumber, ProviderType provider) {
return provider.name() + "@" + phoneNumber;
return provider.name() + "_" + phoneNumber;
}

private void validateToken(String accessToken, String value, ProviderType provider) {
Expand All @@ -166,11 +168,11 @@ private void validateToken(String accessToken, String value, ProviderType provid
}

private ProviderType getProviderByTopic(String topic) {
return ProviderType.valueOf(topic.split("@")[0].toUpperCase());
return ProviderType.valueOf(topic.split("_")[0].toUpperCase());
}

private String getPhoneByTopic(String topic) {
return topic.split("@")[1];
return topic.split("_")[1];
}

private Jwt generateToken(JwtUserInfo jwtUserInfo) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import com.kcy.fitapet.global.common.security.oauth.dto.OIDCPublicKey;
import com.kcy.fitapet.global.common.security.oauth.dto.OIDCPublicKeyResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class OauthOIDCHelper {
private final OauthOIDCProvider oauthOIDCProvider;

Expand All @@ -28,7 +30,6 @@ public OIDCDecodePayload getPayloadFromIdToken(String token, String iss, String
.filter(k -> k.kid().equals(kid))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("No matching key found"));

return oauthOIDCProvider.getOIDCTokenBody(token, key.n(), key.e());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public String getKidFromUnsignedTokenHeader(String token, String iss, String aud
@Override
public OIDCDecodePayload getOIDCTokenBody(String token, String modulus, String exponent) {
Claims body = getOIDCTokenJws(token, modulus, exponent).getBody();
log.info("body : {}", body);

return new OIDCDecodePayload(
body.getIssuer(),
Expand All @@ -47,7 +48,7 @@ private Jwt<Header, Claims> getUnsignedTokenClaims(String token, String iss, Str
return Jwts.parserBuilder()
.requireAudience(aud)
.requireIssuer(iss)
.require("nonce", nonce)
// .require("nonce", nonce)
.build()
.parseClaimsJwt(getUnsignedToken(token));
} catch (JwtException e) {
Expand All @@ -72,6 +73,7 @@ private String getUnsignedToken(String token){
*/
private Jws<Claims> getOIDCTokenJws(String token, String modulus, String exponent) {
try {
log.info("token : {}", token);
return Jwts.parserBuilder()
.setSigningKey(getRSAPublicKey(modulus, exponent))
.build()
Expand Down

0 comments on commit ec351b4

Please sign in to comment.