package com.chenyang.nse.bussiness.tools.token;

import com.auth0.jwt.JWT;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.exceptions.JWTDecodeException;
import com.auth0.jwt.interfaces.DecodedJWT;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class TokenUtil {
   private static final long EXPIRE_DATE = 7200000L;
   private static final String TOKEN_SECRET = "GHCA";

   public static String getToken(String username, String password) {
      String token = "";

      try {
         Date date = new Date(System.currentTimeMillis() + 7200000L);
         Algorithm algorithm = Algorithm.HMAC256("GHCA");
         Map<String, Object> header = new HashMap();
         header.put("typ", "JWT");
         header.put("alg", "HS256");
         token = JWT.create().withHeader(header).withClaim("username", username).withClaim("password", password).withExpiresAt(date).sign(algorithm);
         return token;
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
   }

   public static String getNoDateToken(String username, String password) {
      String token = "";

      try {
         new Date(System.currentTimeMillis() + 7200000L);
         Algorithm algorithm = Algorithm.HMAC256("GHCA");
         Map<String, Object> header = new HashMap();
         header.put("typ", "JWT");
         header.put("alg", "HS256");
         token = JWT.create().withHeader(header).withClaim("username", username).withClaim("password", password).sign(algorithm);
         return token;
      } catch (Exception e) {
         e.printStackTrace();
         return null;
      }
   }

   public static boolean verify(String token) {
      try {
         Algorithm algorithm = Algorithm.HMAC256("GHCA");
         JWTVerifier verifier = JWT.require(algorithm).build();
         verifier.verify(token);
         return true;
      } catch (Exception e) {
         e.printStackTrace();
         return false;
      }
   }

   public static String getUserId(String token) {
      try {
         DecodedJWT jwt = JWT.decode(token);
         return jwt.getClaim("username").asString();
      } catch (JWTDecodeException var2) {
         return null;
      }
   }

   public static String getPassword(String token) {
      try {
         DecodedJWT jwt = JWT.decode(token);
         return jwt.getClaim("password").asString();
      } catch (JWTDecodeException var2) {
         return null;
      }
   }
}
