/*
 * Copyright (c) 2024. Elex. All Rights Reesrved.
 * https://www.elex-project.com/
 */

package kr.pe.elex.examples;

import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import com.auth0.jwt.interfaces.DecodedJWT;

import java.util.Random;

public class Jwt0Sample {
	private static final byte[] key;

	static {
		// HMACSHA256을 사용하므로 키의 길이는 32바이트이다.
		key = new byte[32];
		new Random().nextBytes(key);
	}
	public static String genToken(){
		return JWT.create()
				.withIssuer("Elex")
				.withSubject("Hello")
				.sign(Algorithm.HMAC256(key));
	}
	public static DecodedJWT parseToken(String token, byte[] key){
		return JWT.require(Algorithm.HMAC256(key))
				.withIssuer("Elex")
				.build()
				.verify(token);
	}
	public static DecodedJWT parseToken(String token){
		return JWT.decode(token);
	}

	public static void main(String... args){
		String token = genToken();
		System.out.println(token);

		DecodedJWT jwt = parseToken(token);
		System.out.println(jwt.getSubject());
	}
}
