package com.iamberry.app.tool.util;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.auth0.jwt.JWTSigner;
import com.auth0.jwt.JWTVerifier;
import com.auth0.jwt.JWTVerifyException;
/**
 * @company	深圳爱贝源科技有限公司
 * @website www.iamberry.com
 * @author 	献
 * @tel		18271840547
 * @date	2016年11月15日
 * @explain	使用JWT完成用户Token生成
 */
public class WebJsonTokenUtil {
	
	/**
	 * 发行者
	 */
	private static final String issuer = "http://app.iamberry.com/";
	/**
	 * 校验点
	 */
	private static final String verifierURL = "http://app.iamberry.com/user/verifer";
	/**
	 * 密匙
	 */
	private static final String secret = "sasdf2312ljwuio02u3roij}_+@#_IFJLKAJ";
	
	/**
	 * 签名使用的工具
	 */
	private static final JWTSigner signer = new JWTSigner(secret);
	
	/**
	 * 校验使用的工具
	 */
	public static final JWTVerifier verifier = new JWTVerifier(secret);
	
	/**
	 * 更加用户id，获取用户Token
	 * @param userid
	 * @return
	 */
	public static String getToken(Integer userid) {
		// 当前时间
		long iat = System.currentTimeMillis() / 1000L;
		// 过期时间,设置为三天以后，需要重新获取token
		long exp = iat + 259200L;
		// 创建对象
		HashMap<String, Object> claims = new HashMap<String, Object>();
		claims.put("iss", issuer);
		claims.put("exp", exp);
		claims.put("iat", iat);
		claims.put("verifierURL", verifierURL);
		claims.put("userid", AES.encrypt(userid.toString()));
		String jsonToken = signer.sign(claims);
		return jsonToken;
	}
	
	/**
	 * return:
	 * 	1:token过期，失效
	 * 	2:token正常
	 * @param userToken
	 * @return
	 */
	public static Map<String, Object> verifier(String userToken) {
		Map<String, Object> claims = null;
		try {
			// 解析TOKEN
			claims = verifier.verify(userToken);
		} catch (InvalidKeyException e) {
			// 无效的键
			Map<String, Object> error = new HashMap<String, Object>();
			error.put("status", 1);
			error.put("message", "Key error");
			return error;
		} catch (NoSuchAlgorithmException e) {
			// 没有这样的算法
			Map<String, Object> error = new HashMap<String, Object>();
			error.put("status", 1);
			error.put("message", "No Such Algorithm");
			return error;
		} catch (IllegalStateException e) {
			// 非法状态
			Map<String, Object> error = new HashMap<String, Object>();
			error.put("status", 1);
			error.put("message", "Illegal State");
			return error;
		} catch (SignatureException e) {
			// 签名有误
			Map<String, Object> error = new HashMap<String, Object>();
			error.put("status", 1);
			error.put("message", "Signature State Error");
			return error;
		} catch (IOException e) {
			Map<String, Object> error = new HashMap<String, Object>();
			error.put("status", 1);
			error.put("message", "IO Exception");
			return error;
		} catch (JWTVerifyException e) {
			Map<String, Object> error = new HashMap<String, Object>();
			error.put("status", 1);
			error.put("message", e.getMessage());
			return error;
		}
		
		// 解析userid
		String temp = AES.decrypt(claims.get("userid").toString());
		if (StringUtils.isEmpty(temp)) {
			Map<String, Object> error = new HashMap<String, Object>();
			error.put("status", 1);
			error.put("message", "Data has been tampered with");
			return error;
		}
		// 重新定义ID
		claims.put("userid", temp);

		claims.put("status", 2);
		claims.put("message", "SUCCESS");
		return claims;
	}
	

	public static void main(String[] args) {
		System.out.println(getToken(1));
		System.out.println(verifier("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOi8vYXBwLmlhbWJlcnJ5LmNvbS8iLCJleHAiOjE0Nzk0NjM2ODAsImlhdCI6MTQ3OTIwNDQ4MCwidXNlcmlkIjoiQldadmkxR0dXODYxOUpveU5tcnh1QT09In0.G8QGFzlu27D5BXdHDrnRFqgcv5cxYG2--KO9CI-A7Mk"));
	}
}
