Skip to content

Commit

Permalink
Feature | #4 | @lcomment | Jwt Provider κ΅¬ν˜„
Browse files Browse the repository at this point in the history
  • Loading branch information
lcomment committed May 2, 2024
1 parent a86df7b commit b79f1a0
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
100 changes: 100 additions & 0 deletions cakk-api/src/main/java/com/cakk/api/provider/jwt/JwtProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.cakk.api.provider.jwt;

import static com.cakk.common.enums.ReturnCode.*;
import static java.util.Objects.*;

import java.security.Key;
import java.security.PublicKey;
import java.util.Date;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Component;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import lombok.RequiredArgsConstructor;

import com.cakk.api.vo.JsonWebToken;
import com.cakk.api.vo.OAuthUserDetails;
import com.cakk.common.exception.CakkException;
import com.cakk.domain.entity.user.User;

@Component
@RequiredArgsConstructor
public class JwtProvider {

private final Key key;

@Value("${jwt.expiration.access-token}")
private Long accessTokenExpiredSecond;
@Value("${jwt.expiration.access-token}")
private Long refreshTokenExpiredSecond;
@Value("${jwt.grant-type}")
private String grantType;
@Value("${jwt.user-key}")
private String userKey;

public JsonWebToken generateToken(final User user) {
final String accessToken = Jwts.builder()
.claim(userKey, user)
.setExpiration(new Date(System.currentTimeMillis() + accessTokenExpiredSecond))
.signWith(key, SignatureAlgorithm.HS512)
.compact();

final String refreshToken = Jwts.builder()
.setExpiration(new Date(System.currentTimeMillis() + refreshTokenExpiredSecond))
.signWith(key, SignatureAlgorithm.HS512)
.compact();

return JsonWebToken.builder()
.grantType(grantType)
.accessToken(accessToken)
.refreshToken(refreshToken)
.build();
}

public Authentication getAuthentication(String token) {
final Claims claims = parseClaims(token);

if (isNull(claims.get(userKey)) || !(claims.get(userKey) instanceof User)) {
throw new CakkException(EMPTY_AUTH_JWT);
}

OAuthUserDetails userDetails = new OAuthUserDetails((User) claims.get(userKey));

return new UsernamePasswordAuthenticationToken(userDetails, "", userDetails.getAuthorities());
}

public Claims parseClaims(String token) {
try {
return Jwts.parserBuilder()
.setSigningKey(key)
.build()
.parseClaimsJws(token)
.getBody();
} catch (ExpiredJwtException e) {
throw new CakkException(EXPIRED_JWT_TOKEN);
} catch (RuntimeException e) {
throw new CakkException(WRONG_JWT_TOKEN);
}
}

public Claims parseClaims(String token, PublicKey publicKey) {
try {
return Jwts.parserBuilder()
.setSigningKey(publicKey)
.build()
.parseClaimsJws(token)
.getBody();
} catch (ExpiredJwtException e) {
throw new CakkException(EXPIRED_JWT_TOKEN);
} catch (RuntimeException e) {
throw new CakkException(WRONG_JWT_TOKEN);
}
}
}
11 changes: 11 additions & 0 deletions cakk-api/src/main/java/com/cakk/api/vo/JsonWebToken.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.cakk.api.vo;

import lombok.Builder;

@Builder
public record JsonWebToken(
String accessToken,
String refreshToken,
String grantType
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ public enum ReturnCode {

SUCCESS("1000", "μš”μ²­μ— μ„±κ³΅ν•˜μ…¨μŠ΅λ‹ˆλ‹€."),

// 토큰 κ΄€λ ¨ (1100 ~ 1150)
NOT_EXIST_BEARER_SUFFIX("1100", "Bearer 접두사가 ν¬ν•¨λ˜μ§€ μ•Šμ•˜μŠ΅λ‹ˆλ‹€."),
WRONG_JWT_TOKEN("1101", "잘λͺ»λœ jwt ν† ν°μž…λ‹ˆλ‹€."),
EXPIRED_JWT_TOKEN("1102", "만료된 jwt ν† ν°μž…λ‹ˆλ‹€."),
EMPTY_AUTH_JWT("1103", "인증 정보가 λΉ„μ–΄μžˆλŠ” jwt ν† ν°μž…λ‹ˆλ‹€."),

// μ„œλ²„ μ—λŸ¬ (9998, 9999)
INTERNAL_SERVER_ERROR("9998", "λ‚΄λΆ€ μ„œλ²„ μ—λŸ¬ μž…λ‹ˆλ‹€."),
EXTERNAL_SERVER_ERROR("9999", "μ™ΈλΆ€ μ„œλ²„ μ—λŸ¬ μž…λ‹ˆλ‹€.");
Expand Down

0 comments on commit b79f1a0

Please sign in to comment.