JSON web tokens implemented in different languages
Change directory: cd python/
from src import JWT, keys_utils
# Example 1 (HMAC)
secret_key = "secret key"
jwt = JWT.encode({"sample": "claim"}, secret_key, 234.23, 300000)
claims = JWT.decode(jwt, secret_key)
# Example 2 (RSA)
private_key, public_key = keys_utils.generate_rsa_keys()
jwt = JWT.encode({"sample": "claim"}, private_key, 234.23, 300000, algorithm="RS256")
claims = JWT.decode(jwt, public_key)
- Install testing tools:
pip install pytest pytest-cov
- Run tests and generate a coverage report:
pytest tests/ --cov=src/ --cov-report term-missing
Isort, black and flake8 are used for linting. To automate this task, pre-commit hooks are used.
- Install the git hook scripts:
pre-commit install
- (optional) Run against all the files:
pre-commit run --all-files
For full documentation, view the pre-commit docs.
Change directory: cd javascript/
import { JWT } from "./src/jwt.js";
import { generateRSAKeys } from "./src/keys_utils.js";
// Example 1 (HMAC)
const secretKey = "secret key";
const jwt = JWT.encode({ sample: "claim" }, secretKey, 234.23, 300000);
const claims = JWT.decode(jwt, secretKey);
// Example 2 (RSA)
const { privateKey, publicKey } = generateRSAKeys();
const jwt = JWT.encode(
{ sample: "claim" },
privateKey,
234.23,
300000,
undefined,
"RS256"
);
const claims = JWT.decode(jwt, publicKey);
- Run tests and generate a coverage report:
npm run test
ESLint and prettier are used for linting. To automate this task, pre-commit hooks are used.
- Install the git hook scripts:
pre-commit install
- (optional) Run against all the files:
pre-commit run --all-files
For full documentation, view the pre-commit docs.
Change directory: cd php/
<?php
require_once("src/jwt.php");
require_once("src/keys_utils.php");
use JWT\JWT;
use function JWT\generateRSAKeys;
// Example 1 (HMAC)
$secretKey = "secret key";
$jwt = JWT::encode(["sample" => "claim"], $secretKey, 234.23, 300000);
$claims = JWT::decode($jwt, $secretKey);
// Example 2 (RSA)
$keys = generateRSAKeys();
$privateKey = $keys[0];
$publicKey = $keys[1];
$jwt = JWT::encode(["sample" => "claim"], $privateKey, 234.23, 300000, null, "RS256");
$claims = JWT::decode($jwt, $publicKey);
- Run tests:
php vendor/phpunit/phpunit/phpunit tests/
- Phplint is used for linting:
php vendor/bin/phplint