package com.cardpaymentsystem.cardplatform.security; import com.cardpaymentsystem.cardplatform.exceptions.CardPlatformException; import com.cardpaymentsystem.cardplatform.exceptions.JwtExpiryException; import com.cardpaymentsystem.cardplatform.models.RefreshToken; import io.jsonwebtoken.Claims; import io.jsonwebtoken.Jwts; import org.springframework.beans.factory.annotation.Value; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.User; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import java.io.IOException; import java.io.InputStream; import java.security.*; import java.security.cert.CertificateException; import java.time.Instant; import static io.jsonwebtoken.Jwts.parserBuilder; import static java.util.Date.from; @Service public class JwtProvider { private KeyStore keyStore; @Value("${jwt.expiration.time}") private Long jwtExpirationInMillis; @PostConstruct public void init() throws CardPlatformException { try{ keyStore = KeyStore.getInstance("JKS"); InputStream is = getClass().getResourceAsStream("/cardplatform.jks"); keyStore.load(is, "bypassthispass".toCharArray()); } catch (KeyStoreException | CertificateException | NoSuchAlgorithmException | IOException e) { throw new CardPlatformException("Exception occurred while loading keystore! (JWT error)"); } } public String generateToken(Authentication auth, String usertype) throws CardPlatformException { User principal = (User) auth.getPrincipal(); return Jwts.builder() .setSubject(principal.getUsername()+usertype) .setIssuedAt(from(Instant.now())) .signWith(getPrivateKey()) .setExpiration(from(Instant.now().plusMillis(jwtExpirationInMillis))) .compact(); } public String generateTokenWithRefresh(RefreshToken refreshToken) throws CardPlatformException { return Jwts.builder() .setSubject(refreshToken.getUsername()) .setIssuedAt(from(Instant.now())) .signWith(getPrivateKey()) .setExpiration(from(Instant.now().plusMillis(jwtExpirationInMillis))) .compact(); } private PrivateKey getPrivateKey() throws CardPlatformException { try{ return (PrivateKey) keyStore.getKey("cardplatform", "bypassthispass".toCharArray()); } catch (KeyStoreException | NoSuchAlgorithmException | UnrecoverableKeyException e){ throw new CardPlatformException("Exception occurred while retrieving private key from loaded keystore! (JWT error)"); } } public boolean validateToken(String jwt) throws CardPlatformException, JwtExpiryException { //AES encryption is used to sign tokens with a private key //public key is used to validate our token parserBuilder().setSigningKey(getPublicKey()).build().parseClaimsJws(jwt);//check if different parse claims should be used instead //if doesnt work use parser instead of parserBuilder return true; } private PublicKey getPublicKey() throws CardPlatformException { try{ return keyStore.getCertificate("cardplatform").getPublicKey(); } catch (KeyStoreException e){ throw new CardPlatformException("Exception occurred while retrieving public key from loaded keystore! (JWT error)"); } } public String getUserNameFromJwt(String token) throws CardPlatformException { //if doesnt work use parser instead of parserBuilder Claims claims = parserBuilder().setSigningKey(getPublicKey()) .build().parseClaimsJws(token) //check if other parseclaims .getBody(); return claims.getSubject(); } public Long getJwtExpirationInMillis(){ return jwtExpirationInMillis; } }