Skip to content

JSON web tokens implemented in different languages

License

Notifications You must be signed in to change notification settings

onyonkaclifford/jwt

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

License: MIT

JSON web tokens implemented in different languages

Python

python tests python lint Imports: isort Code style: black Code style: flake8

Change directory: cd python/

Example usage

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)

Tests

  • Install testing tools: pip install pytest pytest-cov
  • Run tests and generate a coverage report: pytest tests/ --cov=src/ --cov-report term-missing

Linting

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.

JavaScript

javascript tests javascript lint eslint-standard-style code style: prettier

Change directory: cd javascript/

Example usage

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);

Tests

  • Run tests and generate a coverage report: npm run test

Linting

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.

PHP

php tests php lint code style: phplint

Change directory: cd php/

Example usage

<?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);

Tests

  • Run tests: php vendor/phpunit/phpunit/phpunit tests/

Linting

  • Phplint is used for linting: php vendor/bin/phplint

About

JSON web tokens implemented in different languages

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published