Skip to content
maksimovicPublic
forked from nov/jose-php

About

PHP JOSE Library (JWT, JWS, JWE, JWK, JWK Set, JWK Thumbprint are supported)

Resources

Stars

0 stars

Watchers

0 watching

Forks

 
 

Latest commit

 

History

176 Commits

Folders and files

Repository files navigation

JOSE

Fork Notice: This is a maintained fork of the abandoned gree/jose package. Updated with verified support for PHP 7.2 through 8.6.

PHP JOSE (JSON Object Signing and Encryption) Implementation

CI codecov

Installation

composer require maksimovic/jose-php

Composer 2.9 and later refuse to install phpseclib 2.x, because GitHub's copy of advisory GHSA-q97c-8qh3-fpc6 (PKSA-12sz-bcny-pk4m on Packagist) lists every version below 3.0.57. The problem it describes is in phpseclib 3's elliptic-curve code, which 2.x doesn't have, and phpseclib's own copy of the advisory starts at 3.0.0. Until GitHub's copy is corrected, ignore the advisory in your project's composer.json:

"config": {
    "audit": {
        "ignore": {
            "PKSA-12sz-bcny-pk4m": "Affects phpseclib 3 EC code only; 2.x has none and uses libsodium for curve25519"
        }
    }
}

Requirements

PHP 7.2 or later. phpseclib 2.0.56 or later in the 2.x line is required (installed automatically via Composer).

Example

JWT

Encoding

$jwt = new JOSE_JWT(array(
    'foo' => 'bar'
));
$jwt->toString();

Decoding

$jwt_string = 'eyJ...';
$jwt = JOSE_JWT::decode($jwt_string);

JWS

Signing

$private_key = "-----BEGIN RSA PRIVATE KEY-----\n....";
$jwt = new JOSE_JWT(array(
    'foo' => 'bar'
));
$jws = $jwt->sign($private_key, 'RS256');

NOTE: $private_key can be a phpseclib\Crypt\RSA instance.

Verification

$public_key = "-----BEGIN RSA PUBLIC KEY-----\n....";
$jwt_string = 'eyJ...';
$jws = JOSE_JWT::decode($jwt_string);
$jws->verify($public_key, 'RS256');

NOTE: $public_key can be a JOSE_JWK or phpseclib\Crypt\RSA instance.

JWE

Encryption

$jwe = new JOSE_JWE($plain_text);
$jwe->encrypt(file_get_contents('/path/to/public_key.pem'));
$jwe->toString();

Decryption

$jwt_string = 'eyJ...';
$jwe = JOSE_JWT::decode($jwt_string);
$jwe->decrypt($private_key);

JWK

Encode

RSA Public Key
$public_key = new phpseclib\Crypt\RSA();
$public_key->loadKey('-----BEGIN RSA PUBLIC KEY-----\n...');
JOSE_JWK::encode($public_key); # => JOSE_JWK instance
RSA Private Key
$private_key = new phpseclib\Crypt\RSA();
$private_key->setPassword($pass_phrase); # skip if not encrypted
$private_key->loadKey('-----BEGIN RSA PRIVATE KEY-----\n...');
JOSE_JWK::encode($private_key); # => JOSE_JWK instance

Decode

RSA Public Key
$components = array(
    'kty' => 'RSA',
    'e' => 'AQAB',
    'n' => 'x9vNhcvSrxjsegZAAo4OEuo...'
);
JOSE_JWK::decode($components); # => phpseclib\Crypt\RSA instance
RSA Private Key

Not supported.

Development

git clone https://github.com/maksimovic/jose-php.git
cd jose-php
composer install
vendor/bin/phpunit test

License

MIT. Copyright © 2013 Nov Matake & GREE Inc. See LICENSE for details.

About

PHP JOSE Library (JWT, JWS, JWE, JWK, JWK Set, JWK Thumbprint are supported)

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages