Generating Tokens using JWT
When generating tokens using JWT, you can use any library that implements the web standard (RFC7519) JWT. You can include the token information in the Payload, use a secret as the encryption key, and apply the HS256 algorithm to generate the token.
Code Example
Below is an example where the token information is encrypted using the encryption key "this_is_test_secret_key_at_least_32_char".
{
"uid": "test-user-2",
"fullname": "Park Tokki",
"email": "tokki.park@test.com",
"ts": 1596611792131
}
Python
The following code provides an example of creating a JWT token using the pyjwt
library (https://github.com/jpadilla/pyjwt).
import jwt
token = jwt.encode({
"uid": "test-user-2",
"fullname": "Park Tokki",
"email": "tokki.park@test.com",
"ts": 1596611792131
}, 'this_is_test_secret_key_at_least_32_char', algorithm='HS256')
print(token.decode('utf-8'))
# Output: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJ0ZXN0LXVzZXItMiIsImZ1bGxuYW1lIjoiXHViYzE1XHVkMWEwXHViMDdjIiwiZW1haWwiOiJ0b2traS5wYXJrQHRlc3QuY29tIiwidHMiOjE1OTY2MTE3OTIxMzF9.ve6WJli1HrtU03SlWuDKAmYxFSM6dmxOe-1DGH65J8o
# The output may vary over time, so please check it at <https://jwt.io>.
C#
The following code provides an example of creating a JWT token using the System.IdentityModel.Tokens.Jwt
library (https://www.nuget.org/packages/System.IdentityModel.Tokens.Jwt/).
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Text;
namespace jwt_test
{
class Test
{
static void Main(String[] args)
{
var tokenHandler = new JwtSecurityTokenHandler();
var tokenDescriptor = new SecurityTokenDescriptor
{
Claims = new Dictionary<string, object>
{
{"uid", "test-user-2" },
{"fullname", "Park Tokki" },
{"email", "tokki.park@test.com" },
{"ts", 1596611792131L },
},
SigningCredentials = new SigningCredentials(
new SymmetricSecurityKey(Encoding.UTF8.GetBytes("this_is_test_secret_key_at_least_32_char")),
SecurityAlgorithms.HmacSha256Signature
)
};
var stoken = tokenHandler.CreateToken(tokenDescriptor);
var token = tokenHandler.WriteToken(stoken);
Console.WriteLine(token);
// Output: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJ0ZXN0LXVzZXItMiIsImZ1bGxuYW1lIjoi67CV7Yag64G8IiwiZW1haWwiOiJ0b2traS5wYXJrQHRlc3QuY29tIiwidHMiOjE1OTY2MTE3OTIxMzEsIm5iZiI6MTU5Njc2NzQwMSwiZXhwIjoxNTk2NzcxMDAxLCJpYXQiOjE1OTY3Njc0MDF9.5Ol83EyKiRWc6FoSJPEqv4zJFlIowMDKmvVPsomQVWQ
// The output may vary over time, so please check it at <https://jwt.io>.
}
}
}
Java
The following code provides an example of creating a JWT token using the jjwt
library (https://github.com/jwtk/jjwt).
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
public class Main {
public static void main(String args[]) {
String token = Jwts.builder()
.setHeaderParam("typ", "JWT") // optional
.claim("uid", "test-user-2")
.claim("fullname", "Park Tokki")
.claim("email", "tokki.park@test.com")
.claim("ts", 1596611792131L)
.signWith(SignatureAlgorithm.HS256, "this_is_test_secret_key_at_least_32_char".getBytes())
.compact();
System.out.println(token);
// Output: eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOiJ0ZXN0LXVzZXItMiIsImZ1bGxuYW1lIjoi67CV7Yag64G8IiwiZW1haWwiOiJ0b2traS5wYXJrQHRlc3QuY29tIiwidHMiOjE1OTY2MTE3OTIxMzF9.akIXRvSad0wjv6y86ko7-KkJHwjMfTPDwXQOYOlixHQ
// The output may vary over time, so please check it at <https://jwt.io>.
}
}