Skip to content

Commit

Permalink
fix/GH-147-token-entity (#40)
Browse files Browse the repository at this point in the history
  • Loading branch information
inh2613 authored Jul 26, 2023
2 parents 45336d7 + 350f042 commit f304edb
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
package org.swmaestro.repl.gifthub.auth.entity;

import java.time.LocalDateTime;

import org.springframework.data.annotation.CreatedDate;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;

import java.time.LocalDateTime;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class UserToken {
public class DeviceToken {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
Expand All @@ -36,7 +29,7 @@ public class UserToken {
private LocalDateTime createdAt;

@Builder
public UserToken(Long id, Member member, String token, LocalDateTime createdAt) {
public DeviceToken(Long id, Member member, String token, LocalDateTime createdAt) {
this.id = id;
this.member = member;
this.token = token;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
package org.swmaestro.repl.gifthub.auth.entity;

import org.swmaestro.repl.gifthub.util.BaseTimeEntity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;

import java.time.LocalDateTime;

@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class RefreshToken extends BaseTimeEntity {
public class RefreshToken {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(nullable = false)
private String refreshToken;
private String token;

@Column(length = 60, nullable = false)
private String username;

@CreatedDate
@Column(nullable = false)
private LocalDateTime createdAt;

@Builder
public RefreshToken(Long id, String refreshToken, String username) {
public RefreshToken(Long id, String token, String username, LocalDateTime createdAt) {
this.id = id;
this.refreshToken = refreshToken;
this.token = token;
this.username = username;
this.createdAt = createdAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.swmaestro.repl.gifthub.auth.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import org.swmaestro.repl.gifthub.auth.entity.DeviceToken;

public interface DeviceTokenRepository extends JpaRepository<DeviceToken, Long> {
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ public class RefreshTokenService {
@Transactional
public void storeRefreshToken(TokenDto tokenDto, String username) {
RefreshToken refreshToken = RefreshToken.builder()
.refreshToken(tokenDto.getRefreshToken())
.token(tokenDto.getRefreshToken())
.username(username)
.createdAt(jwtProvider.getIssuedAt(tokenDto.getRefreshToken()))
.build();

if (refreshTokenRepository.findByUsername(username).isPresent()) {
Expand Down
75 changes: 47 additions & 28 deletions src/main/java/org/swmaestro/repl/gifthub/util/JwtProvider.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.swmaestro.repl.gifthub.util;

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Date;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
Expand All @@ -15,10 +15,10 @@
import org.swmaestro.repl.gifthub.exception.ErrorCode;
import org.swmaestro.repl.gifthub.security.JpaUserDetailsService;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import jakarta.servlet.http.HttpServletRequest;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Date;

@Component
@PropertySource("classpath:application.yml")
Expand All @@ -30,8 +30,8 @@ public class JwtProvider {
private final RefreshTokenRepository refreshTokenRepository;

public JwtProvider(@Value("${jwt.secret-key}") String secretKey, @Value("${jwt.expiration-time}") long expiration,
@Value("${issuer}") String issuer, JpaUserDetailsService userDetailsService,
RefreshTokenRepository refreshTokenRepository) {
@Value("${issuer}") String issuer, JpaUserDetailsService userDetailsService,
RefreshTokenRepository refreshTokenRepository) {
this.secretKey = secretKey;
this.expiration = expiration;
this.issuer = issuer;
Expand All @@ -47,12 +47,12 @@ public JwtProvider(@Value("${jwt.secret-key}") String secretKey, @Value("${jwt.e
*/
public String generateToken(String username) {
return io.jsonwebtoken.Jwts.builder()
.setSubject(username)
.setIssuer(issuer)
.setIssuedAt(new java.util.Date(System.currentTimeMillis()))
.setExpiration(new java.util.Date(System.currentTimeMillis() + expiration))
.signWith(io.jsonwebtoken.SignatureAlgorithm.HS512, secretKey.getBytes())
.compact();
.setSubject(username)
.setIssuer(issuer)
.setIssuedAt(new java.util.Date(System.currentTimeMillis()))
.setExpiration(new java.util.Date(System.currentTimeMillis() + expiration))
.signWith(io.jsonwebtoken.SignatureAlgorithm.HS512, secretKey.getBytes())
.compact();
}

/**
Expand Down Expand Up @@ -106,11 +106,11 @@ public Authentication getAuthentication(String token) {
*/
public String getUsername(String token) {
return Jwts.parserBuilder()
.setSigningKey(secretKey.getBytes())
.build()
.parseClaimsJws(token)
.getBody()
.getSubject();
.setSigningKey(secretKey.getBytes())
.build()
.parseClaimsJws(token)
.getBody()
.getSubject();
}

/**
Expand All @@ -121,12 +121,12 @@ public String getUsername(String token) {
*/
public String generateRefreshToken(String username) {
return io.jsonwebtoken.Jwts.builder()
.setSubject(username)
.setIssuer(issuer)
.setIssuedAt(new java.util.Date(System.currentTimeMillis()))
.setExpiration(Date.from(Instant.now().plus(15, ChronoUnit.DAYS)))
.signWith(io.jsonwebtoken.SignatureAlgorithm.HS512, secretKey.getBytes())
.compact();
.setSubject(username)
.setIssuer(issuer)
.setIssuedAt(new java.util.Date(System.currentTimeMillis()))
.setExpiration(Date.from(Instant.now().plus(15, ChronoUnit.DAYS)))
.signWith(io.jsonwebtoken.SignatureAlgorithm.HS512, secretKey.getBytes())
.compact();
}

/**
Expand All @@ -137,11 +137,30 @@ public String generateRefreshToken(String username) {
*/
public String reissueAccessToken(String refreshToken) {
String username = getUsername(refreshToken);
String storedRefreshToken = refreshTokenRepository.findByUsername(username).get().getRefreshToken();
String storedRefreshToken = refreshTokenRepository.findByUsername(username).get().getToken();

if (!refreshToken.equals(storedRefreshToken)) {
throw new BusinessException("RefreshToken이 유효하지 않습니다.", ErrorCode.INVALID_AUTHENTICATION);
}
return generateToken(username);
}

/**
* 토큰 발급 시간을 가져오는 메소드
*
* @param token
* @return 발급 시간
*/
public LocalDateTime getIssuedAt(String token) {
return Jwts.parserBuilder()
.setSigningKey(secretKey.getBytes())
.build()
.parseClaimsJws(token)
.getBody()
.getIssuedAt()
.toInstant()
.atZone(java.time.ZoneId.systemDefault())
.toLocalDateTime();

}
}

0 comments on commit f304edb

Please sign in to comment.