content: package com.alifetvaci.credit.gateway.service;
  
  import com.alifetvaci.credit.gateway.exception.UnauthorizedException;
  import io.jsonwebtoken.Claims;
  import io.jsonwebtoken.Jwts;
  import io.jsonwebtoken.io.Decoders;
  import io.jsonwebtoken.security.Keys;
  import org.springframework.beans.factory.annotation.Value;
  import org.springframework.stereotype.Service;
  
  import java.security.Key;
  import java.util.Date;
  import java.util.function.Function;
  
  @Service
  public class JwtService {
  
      @Value("${security.jwt.secret-key}")
      private String secretKey;
  
      public Claims isTokenValid(String token) {
          try{
              return isTokenExpired(token) ?null:extractClaims(token);
          }catch (Exception e){
              throw new UnauthorizedException();
          }
      }
  
      private Claims extractClaims(String token) {
          final Claims claims = extractAllClaims(token);
          claims.get("name",String.class);
          return claims;
      }
  
      private <T> T extractClaim(String token, Function<Claims, T> claimsResolver) {
          final Claims claims = extractAllClaims(token);
          return claimsResolver.apply(claims);
      }
  
      private boolean isTokenExpired(String token) {
          return extractExpiration(token).before(new Date());
      }
  
      private Date extractExpiration(String token) {
          return extractClaim(token, Claims::getExpiration);
      }
  
      private Claims extractAllClaims(String token) {
          return Jwts
                  .parserBuilder()
                  .setSigningKey(getSignInKey())
                  .build()
                  .parseClaimsJws(token)
                  .getBody();
      }
  
      private Key getSignInKey() {
          byte[] keyBytes = Decoders.BASE64.decode(secretKey);
          return Keys.hmacShaKeyFor(keyBytes);
      }
  }