Skip to content

Commit

Permalink
feat: 카카오 로그인 기본 기능
Browse files Browse the repository at this point in the history
- 추후 검증 추가 예정

Related to: #16
  • Loading branch information
hajungIm committed Mar 22, 2024
1 parent a11ce97 commit e65de9a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.umc5th.muffler.domain.member.service;

import com.umc5th.muffler.global.security.jwt.JwtOICDProvider;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Header;
import io.jsonwebtoken.Jwt;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class KakaoService {

private final String ISS = "https://kauth.kakao.com";
@Value("${jwt.aud}")
private String AUD;

private final JwtOICDProvider jwtOICDProvider;

public String login(String idToken) {
Jwt<Header, Claims> jwt = jwtOICDProvider.getUnsignedTokenClaims(idToken, ISS, AUD);
return jwt.getBody().getSubject();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class MemberService {

private final MemberRepository memberRepository;
private final AppleService appleService;
private final KakaoService kakaoService;
private final JwtTokenUtils jwtTokenUtils;
private final BatchUpdateCategoryRepository batchUpdateCategoryRepository;
private final EntityManager entityManager;
Expand Down Expand Up @@ -79,6 +80,9 @@ private String socialLogin(LoginRequest request) {
if (request.getSocialType() == SocialType.APPLE) {
return appleService.login(request);
}
if (request.getSocialType() == SocialType.KAKAO) {
return kakaoService.login(request.getIdToken());
}
throw new CommonException(BAD_REQUEST, "지원하지 않는 소셜 로그인 입니다.");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.umc5th.muffler.global.security.jwt;

import static com.umc5th.muffler.global.response.code.ErrorCode.INVALID_TOKEN;

import com.umc5th.muffler.global.response.exception.CommonException;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Header;
import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class JwtOICDProvider {
private String getUnsignedToken(String token) {
String[] splitToken = token.split("\\.");
if (splitToken.length != 3) {
throw new CommonException(INVALID_TOKEN, "Invalid KakaoIdToken");
}
return splitToken[0] + "." + splitToken[1] + ".";
}

public Jwt<Header, Claims> getUnsignedTokenClaims(String token, String iss, String aud) {
try {
return Jwts.parserBuilder()
.requireAudience(aud)
.requireIssuer(iss)
.build()
.parseClaimsJwt(getUnsignedToken(token));
} catch (ExpiredJwtException e) {
log.error("Expired JWT Token", e);
throw new CommonException(INVALID_TOKEN, "Expired JWT Token");
} catch (Exception e) {
log.error(e.toString());
throw new CommonException(INVALID_TOKEN, "Invalid KakaoIdToken");
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ spring:
enable: true
jwt:
secret: ${JWT_SECRET}
aud: ${JWT_AUD}

server:
servlet:
encoding:
Expand Down

0 comments on commit e65de9a

Please sign in to comment.