package com.jsc.oscm.security.token;

import java.util.Date;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import io.jsonwebtoken.CompressionCodecs;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

@Component
public class JwtTokenManager implements TokenManager {

    @Value("${token.expire}")
    private final long tokenExpiration = 3600;

    @Value("${token.key}")
    private String tokenSignKey;

    @Override
    public String createToken(String userName) {
        // TODO Auto-generated method stub
        return Jwts.builder().setSubject(userName)
                .setExpiration(new Date(System.currentTimeMillis() + tokenExpiration))
                .signWith(SignatureAlgorithm.HS512, tokenSignKey)
                .compressWith(CompressionCodecs.GZIP).compact();
    }

    @Override
    public String getUserFromToken(String token) {
        // TODO Auto-generated method stub
        return Jwts.parser().setSigningKey(tokenSignKey).parseClaimsJws(token).getBody().getSubject();
    }

    @Override
    public String removeToken(String token) {
        // TODO Auto-generated method stub
        return null;
    }

}
