xaamin / jwt by xaamin

PHP Implementation of JSON Web token with RSA
8,735
2
2
Package Data
Maintainer Username: xaamin
Maintainer Contact: xaamin@outlook.com (Benjamín Martínez Mateos)
Package Create Date: 2016-09-12
Package Last Update: 2024-02-14
Language: PHP
License: MIT
Last Refreshed: 2024-04-22 15:04:14
Package Statistics
Total Downloads: 8,735
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 2
Total Watchers: 2
Total Forks: 1
Total Open Issues: 0

PHP JWT Tokens with RSA Support

PHP Implementation of JSON Web token with RSA.

Supported algorithms:

RSA (Public Key/Private Key pair)

RS256 - RSA using SHA-256 hash algorithm
RS384 - RSA using SHA-384 hash algorithm
RS512 - RSA using SHA-512 hash algorithm

HMAC algorithms : 

HS256 - HMAC using SHA-256 hash algorithm (default)
HS384 - HMAC using SHA-384 hash algorithm
HS512 - HMAC using SHA-512 hash algorithm

Install

With composer

composer require xaamin/jwt

How to use with RSA public/private key

Set configuration in src/Config/config.php. Don't use key provided here in production.

    'algorithm' => 'RS512',
    
    // ... Other stuff here
    
    'keys' => [
        'public' => '../keys/public_key.pem',
        'private' => '../keys/private_key.pem',
        
        'passphrase' => null,
    ],
	$payload = [
			'sub'   => 1,
			'username' => 'xaamin'
	    ];

	// Generate token
	$token = Xaamin\JWT\Facade\Native\JWT::encode($payload);

	// Verify the token
	try{
		$token = Xaamin\JWT\Facade\Native\JWT::decode($token->get());

	    var_dump($token);
	} catch (Exception $e) {
		echo $e->getMessage();
	}

How to use with HMAC

Set configuration in src/Config/config.php

    'algorithm' => 'HS512',

    'secret' => 'some-random-string'
	$payload = [
			'sub'   => 1,
			'username' => 'xaamin'
	    ];

	// Generate token
	$token = Xaamin\JWT\Facade\Native\JWT::encode($payload);

	// Verify the token
	try{
		$token = Xaamin\JWT\Facade\Native\JWT::decode($token->get());

	    var_dump($token);
	} catch (Exception $e) {
		echo $e->getMessage();
	}