|
| 1 | +package kattsyn.dev.rentplace.auth; |
| 2 | + |
| 3 | +import io.jsonwebtoken.*; |
| 4 | +import io.jsonwebtoken.io.Decoders; |
| 5 | +import io.jsonwebtoken.security.Keys; |
| 6 | +import io.micrometer.common.lang.NonNull; |
| 7 | +import kattsyn.dev.rentplace.entities.User; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import org.springframework.stereotype.Component; |
| 10 | + |
| 11 | +import javax.crypto.SecretKey; |
| 12 | +import java.time.Instant; |
| 13 | + |
| 14 | +import org.springframework.beans.factory.annotation.Value; |
| 15 | + |
| 16 | +import java.time.LocalDateTime; |
| 17 | +import java.time.ZoneId; |
| 18 | +import java.util.Date; |
| 19 | + |
| 20 | +@Component |
| 21 | +@Slf4j |
| 22 | +public class JwtProvider { |
| 23 | + |
| 24 | + private final int accessTokenExpTimeInMinutes; |
| 25 | + private final int refreshTokenExpTimeInDays; |
| 26 | + |
| 27 | + private final SecretKey jwtAccessSecret; |
| 28 | + private final SecretKey jwtRefreshSecret; |
| 29 | + |
| 30 | + public JwtProvider( |
| 31 | + @Value("${jwt.secret.access}") String jwtAccessSecret, |
| 32 | + @Value("${jwt.secret.refresh}") String jwtRefreshSecret, |
| 33 | + @Value("${jwt.expiration_time_in_minutes.access}") int accessTokenExpTimeInMinutes, |
| 34 | + @Value("${jwt.expiration_time_in_days.refresh}") int refreshTokenExpTimeInDays |
| 35 | + ) { |
| 36 | + this.jwtAccessSecret = Keys.hmacShaKeyFor(Decoders.BASE64.decode(jwtAccessSecret)); |
| 37 | + this.jwtRefreshSecret = Keys.hmacShaKeyFor(Decoders.BASE64.decode(jwtRefreshSecret)); |
| 38 | + this.accessTokenExpTimeInMinutes = accessTokenExpTimeInMinutes; |
| 39 | + this.refreshTokenExpTimeInDays = refreshTokenExpTimeInDays; |
| 40 | + } |
| 41 | + |
| 42 | + public String generateAccessToken(User user) { |
| 43 | + final LocalDateTime now = LocalDateTime.now(); |
| 44 | + final Instant accessExpirationInstant = now.plusMinutes(accessTokenExpTimeInMinutes).atZone(ZoneId.systemDefault()).toInstant(); |
| 45 | + final Date accessExpiration = Date.from(accessExpirationInstant); |
| 46 | + return Jwts.builder() |
| 47 | + .subject(user.getEmail()) |
| 48 | + .expiration(accessExpiration) |
| 49 | + .signWith(jwtAccessSecret) |
| 50 | + .claim("role", user.getRole().getAuthority()) |
| 51 | + .claim("name", user.getName()) |
| 52 | + .compact(); |
| 53 | + } |
| 54 | + |
| 55 | + public String generateRefreshToken(@NonNull User user) { |
| 56 | + final LocalDateTime now = LocalDateTime.now(); |
| 57 | + final Instant refreshExpirationInstant = now.plusDays(refreshTokenExpTimeInDays).atZone(ZoneId.systemDefault()).toInstant(); |
| 58 | + final Date refreshExpiration = Date.from(refreshExpirationInstant); |
| 59 | + return Jwts.builder() |
| 60 | + .subject(user.getEmail()) |
| 61 | + .issuedAt(Date.from(Instant.now())) |
| 62 | + .expiration(refreshExpiration) |
| 63 | + .signWith(jwtRefreshSecret) |
| 64 | + .compact(); |
| 65 | + } |
| 66 | + |
| 67 | + public boolean validateAccessToken(@NonNull String accessToken) { |
| 68 | + return validateToken(accessToken, jwtAccessSecret); |
| 69 | + } |
| 70 | + |
| 71 | + public boolean validateRefreshToken(@NonNull String refreshToken) { |
| 72 | + return validateToken(refreshToken, jwtRefreshSecret); |
| 73 | + } |
| 74 | + |
| 75 | + private boolean validateToken(@NonNull String token, @NonNull SecretKey secret) { |
| 76 | + try { |
| 77 | + Jwts.parser() |
| 78 | + .verifyWith(secret) |
| 79 | + .build() |
| 80 | + .parseSignedClaims(token); |
| 81 | + return true; |
| 82 | + } catch (ExpiredJwtException expEx) { |
| 83 | + log.error("Token expired", expEx); |
| 84 | + } catch (UnsupportedJwtException unsEx) { |
| 85 | + log.error("Unsupported jwt", unsEx); |
| 86 | + } catch (MalformedJwtException mjEx) { |
| 87 | + log.error("Malformed jwt", mjEx); |
| 88 | + } catch (Exception e) { |
| 89 | + log.error("invalid token", e); |
| 90 | + } |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + public Claims getAccessClaims(@NonNull String token) { |
| 95 | + return getClaims(token, jwtAccessSecret); |
| 96 | + } |
| 97 | + |
| 98 | + public Claims getRefreshClaims(@NonNull String token) { |
| 99 | + return getClaims(token, jwtRefreshSecret); |
| 100 | + } |
| 101 | + |
| 102 | + private Claims getClaims(@NonNull String token, @NonNull SecretKey secret) { |
| 103 | + return Jwts.parser() |
| 104 | + .verifyWith(secret) |
| 105 | + .build() |
| 106 | + .parseSignedClaims(token) |
| 107 | + .getPayload(); |
| 108 | + } |
| 109 | + |
| 110 | +} |
0 commit comments