Update v1.0.6

This commit is contained in:
Bhanu Slathia
2016-02-16 23:24:52 +05:30
parent c710c20b9e
commit b1f62846ab
7662 changed files with 1361647 additions and 0 deletions

3
vendor/namshi/jose/.coveralls.yml vendored Normal file
View File

@@ -0,0 +1,3 @@
service_name: travis-ci
src_dir: ./
coverage_clover: build/logs/clover.xml

2
vendor/namshi/jose/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
vendor/
build/

24
vendor/namshi/jose/.travis.yml vendored Normal file
View File

@@ -0,0 +1,24 @@
language: php
php:
- 5.4
- 5.5
- 5.6
- hhvm
before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev --no-interaction
phpunit: php vendor/bin/phpunit --prefer-dist --dev
script:
- mkdir -p build/logs
- php vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover ./build/logs/clover.xml
after_script:
- php vendor/bin/coveralls -v
matrix:
allow_failures:
- php: hhvm

21
vendor/namshi/jose/LICENSE vendored Normal file
View File

@@ -0,0 +1,21 @@
The MIT License
Copyright (c) 2014 Alessandro Nadalin
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

178
vendor/namshi/jose/README.md vendored Normal file
View File

@@ -0,0 +1,178 @@
# NAMSHI | JOSE
[![Build Status](https://travis-ci.org/namshi/jose.png?branch=master)](https://travis-ci.org/namshi/jose)
[![HHVM Status](http://hhvm.h4cc.de/badge/namshi/jose.png)](http://hhvm.h4cc.de/package/namshi/jose)
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/4beaf3d1-0bc6-4869-b99f-71dc951a2a05/mini.png)](https://insight.sensiolabs.com/projects/4beaf3d1-0bc6-4869-b99f-71dc951a2a05)
This library provides a lightweight
implementation of the JWS
([JSON Web Signature](http://tools.ietf.org/html/draft-jones-json-web-signature-04)) specification.
## Prerequisites
This library needs PHP 5.4+ and the library OpenSSL.
It has been tested using `PHP5.4` to `PHP5.6` and `HHVM`.
## Installation
You can install the library directly from
composer / [packagist](https://packagist.org/packages/namshi/jose):
```
"namshi/jose": "5.0.*"
```
## Usage
Using it is pretty straightforward:
imagine that you want to offer a service
the ability to authenticate a user via
a cookie, and the service is built with
javascript; what you would need to do is
to generate a JWS (after verifying the
credentials once), store it as a cookie
and then pass it from your JavaScript app
everytime you want to authenticate that
user.
First, generate the JWS:
``` php
<?php
use Namshi\JOSE\SimpleJWS;
if ($username == 'correctUsername' && $pass == 'ok') {
$user = Db::loadUserByUsername($username);
$jws = new SimpleJWS(array(
'alg' => 'RS256'
));
$jws->setPayload(array(
'uid' => $user->getid(),
));
$privateKey = openssl_pkey_get_private("file://path/to/private.key", self::SSL_KEY_PASSPHRASE);
$jws->sign($privateKey);
setcookie('identity', $jws->getTokenString());
}
```
Then your JS app can use the available cookie to execute
authenticated calls, without sending passwords or credentials.
Once a request is submitted, you only have to verify that it
is a valid call:
``` php
<?php
use Namshi\JOSE\SimpleJWS;
$jws = SimpleJWS::load($_COOKIE['identity']);
$public_key = openssl_pkey_get_public("/path/to/public.key");
// verify that the token is valid and had the same values
// you emitted before while setting it as a cookie
if ($jws->isValid($public_key, 'RS256')) {
$payload = $jws->getPayload();
echo sprintf("Hey, my JS app just did an action authenticated as user #%s", $payload['id']);
}
```
> PROTIP: you can omit the second argument of the isValid() method, so jose will try to validate the token with the algorithm specified in the token's header, though this might expose you to some security issues.
>
> For now we recommend to always explicitely set the algorithm you want to use to validate tokens.
### PHPSECLIB For RSA Verification
You may find that you need to use this library in an environment where
[PHP's wrappers for OpenSSL](http://php.net/manual/en/ref.openssl.php)
do not work, or OpenSSL simply is not installed. This library uses
OpenSSL to encrypt by default, but you can specify that you want to use [PHPSecLib](http://phpseclib.sourceforge.net/) for a pure PHP
implementation of RSA encryption.
In these cases, simply add the optional `'SecLib'` parameter when
constructing a JWS:
```php
$jws = new JWS(array('alg' => 'RS256'), 'SecLib');
```
You can now use the PHPSecLib implementation of RSA signing. If you use
a password protected private key, you can still submit the private key
to use for signing as a string, as long as you pass the password as the
second parameter into the `sign` method:
```php
$jws->sign(file_get_contents(SSL_KEYS_PATH . "private.key"), 'tests');
```
You may also load a JWS using the PHPSecLib implementation of RSA verification:
```php
$jws = JWS::load($tokenString, false, $encoder, 'SecLib');
```
## Under the hood
In order to [validate the JWS](https://github.com/namshi/jose/blob/master/src/Namshi/JOSE/SimpleJWS.php#L43),
the signature is first [verified](https://github.com/namshi/jose/blob/master/src/Namshi/JOSE/JWS.php#L113)
with a public key and then we will check whether the [token is expired](https://github.com/namshi/jose/blob/master/src/Namshi/JOSE/SimpleJWS.php#L55).
To give a JWS a TTL, just use the standard `exp` value in the payload:
``` php
$date = new DateTime('tomorrow');
$this->jws = new SimpleJWS(array('alg' => 'RS256'));
$this->jws->setPayload(array(
'exp' => $date->format('U'),
));
```
### Unsecure JWSes
You can allow [unsecure JWSes](https://tools.ietf.org/html/draft-ietf-jose-json-web-algorithms-40#page-12)
by setting the `$allowUnsecure` flag while loading JWSes:
``` php
JWS::load($this->jws->getTokenString(), true);
```
This allows tokens signed with the 'none' algorithms to go through, which is something
you probably don't want to do. Proceed with caution :)
**Unsecure JWSes are disabled by default since version 2.2.2. You should **not**
use previous versions other than 2.2.2 as they have a security
vulnerability. More info [here](http://tech.namshi.com/blog/2015/02/19/update-your-namshi-slash-jose-installations-as-a-security-vulnerability-was-found/).**
## Using a custom encoder
If, for some reason, you need to encode the token in a different way, you can
inject any implementation of `Namshi\JOSE\Base64\Encoder` in a `JWS` instance.
Likewise, `JWS::load()` accepts such an implementation as a second argument.
## Implementation Specifics
The library provides a base JWT Class that implements what is needed just for JSON Web Tokens. The JWS Class then extends
the JWT class and adds the implementation for signing and verifying using JSON Web Signatures. The SimpleJWS class extends
the base JWS class and adds validation of a TTL and inclusion of automatic claims.
## Major Versions
### 2.x.x to 3.x.x
Introduced the ability to specify an encryption engine. Added support of PHPSecLib to the existing OpenSSL implementation.
### 3.x.x to 4.x.x - Not Backwards Compatible
Added the ability to set custom properties in the header. Moved automatic inclusion of certain claims into an SimpleJWS class from the base JWS class.
## Credits
This library has been inspired by the
[initial work done by @ritou](https://github.com/ritou/php-Akita_JOSE).

26
vendor/namshi/jose/composer.json vendored Normal file
View File

@@ -0,0 +1,26 @@
{
"name": "namshi/jose",
"description": "JSON Object Signing and Encryption library for PHP.",
"require-dev": {
"phpunit/phpunit": "~4.5",
"satooshi/php-coveralls": "dev-master"
},
"license": "MIT",
"keywords": ["jws", "jwt", "json", "json web token", "json web signature", "token"],
"authors": [
{
"name": "Alessandro Nadalin",
"email": "alessandro.nadalin@gmail.com"
}
],
"autoload": {
"psr-0": {
"Namshi\\JOSE": "src/"
}
},
"require": {
"php": ">=5.3.3",
"lib-openssl": "*",
"phpseclib/phpseclib": "~0.3"
}
}

1535
vendor/namshi/jose/composer.lock generated vendored Normal file

File diff suppressed because it is too large Load Diff

34
vendor/namshi/jose/phpunit.xml.dist vendored Normal file
View File

@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
backupGlobals="true"
strict="false"
backupStaticAttributes="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
forceCoversAnnotation="false"
mapTestClassNameToCoveredClassName="false"
processIsolation="false"
stopOnFailure="false"
stopOnError="false"
stopOnIncomplete="false"
stopOnSkipped="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
verbose="false"
colors="true">
<testsuites>
<testsuite name="namshi/jose Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">./</directory>
<exclude>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View File

@@ -0,0 +1,26 @@
<?php
namespace Namshi\JOSE\Base64;
class Base64Encoder implements Encoder
{
/**
* @param string $data
* @return string
*/
public function encode($data)
{
return base64_encode($data);
}
/**
* @param string $data
* @return string
*/
public function decode($data)
{
return base64_decode($data);
}
}

View File

@@ -0,0 +1,18 @@
<?php
namespace Namshi\JOSE\Base64;
class Base64UrlSafeEncoder implements Encoder
{
public function encode($data)
{
return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
}
public function decode($data)
{
return base64_decode(strtr($data, '-_', '+/'));
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Namshi\JOSE\Base64;
interface Encoder
{
/**
* @param string $data
* @return string
*/
public function encode($data);
/**
* @param string $data
* @return string
*/
public function decode($data);
}

View File

@@ -0,0 +1,191 @@
<?php
namespace Namshi\JOSE;
use InvalidArgumentException;
use Namshi\JOSE\Base64\Base64Encoder;
use Namshi\JOSE\Base64\Base64UrlSafeEncoder;
use Namshi\JOSE\Signer\SignerInterface;
use Namshi\JOSE\Base64\Encoder;
/**
* Class representing a JSON Web Signature.
*/
class JWS extends JWT
{
protected $signature;
protected $isSigned = false;
protected $encodedSignature;
protected $encryptionEngine;
protected $supportedEncryptionEngines = array('OpenSSL', 'SecLib');
/**
* Constructor
*
* @param array $header An associative array of headers. The value can be any type accepted by json_encode or a JSON serializable object
* @see http://php.net/manual/en/function.json-encode.php
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php
* @see https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#section-4
* @param string $encryptionEngine
* }
*/
public function __construct($header = array(), $encryptionEngine = "OpenSSL")
{
if (!in_array($encryptionEngine, $this->supportedEncryptionEngines)) {
throw new InvalidArgumentException(sprintf("Encryption engine %s is not supported", $encryptionEngine));
}
$this->encryptionEngine = $encryptionEngine;
parent::__construct(array(), $header);
}
/**
* Signs the JWS signininput.
*
* @param resource $key
* @param optional string $password
* @return string
*/
public function sign($key, $password = null)
{
$this->signature = $this->getSigner()->sign($this->generateSigninInput(), $key, $password);
$this->isSigned = true;
return $this->signature;
}
/**
* Returns the signature representation of the JWS.
*
* @return string
*/
public function getSignature()
{
if ($this->isSigned()) {
return $this->signature;
}
return null;
}
/**
* Checks whether the JSW has already been signed.
*
* @return bool
*/
public function isSigned()
{
return (bool) $this->isSigned;
}
/**
* Returns the string representing the JWT.
*
* @return string
*/
public function getTokenString()
{
$signinInput = $this->generateSigninInput();
return sprintf("%s.%s", $signinInput, $this->encoder->encode($this->getSignature()));
}
/**
* Creates an instance of a JWS from a JWT.
*
* @param string $jwsTokenString
* @return JWS
* @throws \InvalidArgumentException
*/
public static function load($jwsTokenString, $allowUnsecure = false, Encoder $encoder = null, $encryptionEngine = 'OpenSSL')
{
if ($encoder === null) {
$encoder = strpbrk($jwsTokenString, '+/=') ? new Base64Encoder() : new Base64UrlSafeEncoder();
}
$parts = explode('.', $jwsTokenString);
if (count($parts) === 3) {
$header = json_decode($encoder->decode($parts[0]), true);
$payload = json_decode($encoder->decode($parts[1]), true);
if (is_array($header) && is_array($payload)) {
if (strtolower($header['alg']) === 'none' && !$allowUnsecure) {
throw new InvalidArgumentException(sprintf('The token "%s" cannot be validated in a secure context, as it uses the unallowed "none" algorithm', $jwsTokenString));
}
$jws = new static($header, $encryptionEngine);
$jws->setEncoder($encoder)
->setHeader($header)
->setPayload($payload)
->setEncodedSignature($parts[2]);
return $jws;
}
}
throw new InvalidArgumentException(sprintf('The token "%s" is an invalid JWS', $jwsTokenString));
}
/**
* Verifies that the internal signin input corresponds to the encoded
* signature previously stored (@see JWS::load).
*
* @param resource|string $key
* @param string $algo The algorithms this JWS should be signed with. Use it if you want to restrict which algorithms you want to allow to be validated.
* @return bool
*/
public function verify($key, $algo = null)
{
if (empty($key) || ($algo && $this->header['alg'] !== $algo)) {
return false;
}
$decodedSignature = $this->encoder->decode($this->getEncodedSignature());
$signinInput = $this->generateSigninInput();
return $this->getSigner()->verify($key, $decodedSignature, $signinInput);
}
/**
* Returns the base64 encoded signature.
*
* @return string
*/
public function getEncodedSignature()
{
return $this->encodedSignature;
}
/**
* Sets the base64 encoded signature.
*
* @param string $encodedSignature
* @return JWS
*/
public function setEncodedSignature($encodedSignature)
{
$this->encodedSignature = $encodedSignature;
return $this;
}
/**
* Returns the signer responsible to encrypting / decrypting this JWS.
*
* @return SignerInterface
* @throws \InvalidArgumentException
*/
protected function getSigner()
{
$signerClass = sprintf('Namshi\\JOSE\\Signer\\%s\\%s', $this->encryptionEngine, $this->header['alg']);
if (class_exists($signerClass)) {
return new $signerClass();
}
throw new InvalidArgumentException(
sprintf("The algorithm '%s' is not supported for %s", $this->header['alg'], $this->encryptionEngine));
}
}

View File

@@ -0,0 +1,108 @@
<?php
namespace Namshi\JOSE;
use Namshi\JOSE\Base64\Base64UrlSafeEncoder;
use Namshi\JOSE\Base64\Encoder;
/**
* Class representing a JSON Web Token.
*/
class JWT
{
/**
* @var array
*/
protected $payload;
/**
* @var array
*/
protected $header;
/**
* @var Encoder
*/
protected $encoder;
/**
* Constructor
*
* @param array $payload
* @param array $header
*/
public function __construct(array $payload, array $header)
{
$this->setPayload($payload);
$this->setHeader($header);
$this->setEncoder(new Base64UrlSafeEncoder());
}
/**
* @param Encoder $encoder
*/
public function setEncoder(Encoder $encoder)
{
$this->encoder = $encoder;
return $this;
}
/**
* Generates the signininput for the current JWT.
*
* @return string
*/
public function generateSigninInput()
{
$base64payload = $this->encoder->encode(json_encode($this->getPayload()));
$base64header = $this->encoder->encode(json_encode($this->getHeader()));
return sprintf("%s.%s", $base64header, $base64payload);
}
/**
* Returns the payload of the JWT.
*
* @return array
*/
public function getPayload()
{
return $this->payload;
}
/**
* Sets the payload of the current JWT.
*
* @param array $payload
*/
public function setPayload(array $payload)
{
$this->payload = $payload;
return $this;
}
/**
* Returns the header of the JWT.
*
* @return array
*/
public function getHeader()
{
return $this->header;
}
/**
* Sets the header of this JWT.
*
* @param array $header
*/
public function setHeader(array $header)
{
$this->header = $header;
return $this;
}
}

View File

@@ -0,0 +1,102 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
use \File_ASN1;
/**
* Class responsible to sign inputs with the a ECDSA algorithm, after hashing it.
*/
abstract class ECDSA extends PublicKey
{
/**
* @inheritdoc
*/
protected function supportsKey($key)
{
if (false === parent::supportsKey($key)) {
return false;
}
// openssl_sign with EC keys was introduced in this PHP release
$minVersions = array(
'5.4' => '5.4.26',
'5.5' => '5.5.10',
'5.6' => '5.6.0',
);
if (isset($minVersions[PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION]) &&
version_compare(PHP_VERSION, $minVersions[PHP_MAJOR_VERSION . '.' . PHP_MINOR_VERSION], '<')) {
return false;
}
$keyDetails = openssl_pkey_get_details($key);
if (0 === preg_match('/-----BEGIN PUBLIC KEY-----([^-]+)-----END PUBLIC KEY-----/', $keyDetails['key'], $matches)) {
return false;
}
$publicKey = trim($matches[1]);
$asn1 = new File_ASN1();
/**
* http://tools.ietf.org/html/rfc3279#section-2.2.3
* AlgorithmIdentifier ::= SEQUENCE {
* algorithm OBJECT IDENTIFIER,
* parameters ANY DEFINED BY algorithm OPTIONAL
* }
* For ECDSA Signature Algorithm:
* algorithm: ansi-X9-62 => 1.2.840.10045.2.1
* parameters: id-ecSigType => 1.2.840.10045.x.y.z
*
*/
$asnAlgorithmIdentifier = array(
'type' => FILE_ASN1_TYPE_SEQUENCE,
'children' => array(
'ansi-X9-62' => array(
'type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER,
),
'id-ecSigType' => array(
'type' => FILE_ASN1_TYPE_OBJECT_IDENTIFIER,
),
),
);
/**
* http://tools.ietf.org/html/rfc5280#section-4.1
* SubjectPublicKeyInfo ::= SEQUENCE {
* algorithm AlgorithmIdentifier,
* subjectPublicKey BIT STRING
* }
*/
$asnSubjectPublicKeyInfo = array(
'type' => FILE_ASN1_TYPE_SEQUENCE,
'children' => array(
'algorithm' => $asnAlgorithmIdentifier,
'subjectPublicKey' => array(
'type' => FILE_ASN1_TYPE_BIT_STRING,
),
),
);
$decoded = $asn1->decodeBER(base64_decode($publicKey));
$mappedDetails = $asn1->asn1map($decoded[0], $asnSubjectPublicKeyInfo);
return isset($mappedDetails['algorithm']['id-ecSigType']) ? $this->getSupportedECDSACurve() === $mappedDetails['algorithm']['id-ecSigType'] : false;
}
/**
* @inheritdoc
*/
protected function getSupportedPrivateKeyType()
{
return defined('OPENSSL_KEYTYPE_EC') ? OPENSSL_KEYTYPE_EC : false;
}
/**
* Returns the ECDSA curve supported in this signer.
*
* @return string
*/
abstract protected function getSupportedECDSACurve();
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
/**
* Class responsible to sign inputs with the ECDSA algorithm, after hashing it.
*/
class ES256 extends ECDSA
{
public function getHashingAlgorithm()
{
return version_compare(phpversion(), '5.4.8', '<') ? 'SHA256' : OPENSSL_ALGO_SHA256;
}
protected function getSupportedECDSACurve()
{
return '1.2.840.10045.3.1.7';
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
/**
* Class responsible to sign inputs with the ECDSA algorithm, after hashing it.
*/
class ES384 extends ECDSA
{
public function getHashingAlgorithm()
{
return version_compare(phpversion(), '5.4.8', '<') ? 'SHA384' : OPENSSL_ALGO_SHA384;
}
protected function getSupportedECDSACurve()
{
return '1.3.132.0.34';
}
}

View File

@@ -0,0 +1,21 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
/**
* Class responsible to sign inputs with the ECDSA algorithm, after hashing it.
*/
class ES512 extends ECDSA
{
public function getHashingAlgorithm()
{
return version_compare(phpversion(), '5.4.8', '<') ? 'SHA512' : OPENSSL_ALGO_SHA512;
}
protected function getSupportedECDSACurve()
{
return '1.3.132.0.35';
}
}

View File

@@ -0,0 +1,71 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
use Namshi\JOSE\Signer\SignerInterface;
/**
* This class is the base of all HMAC Signers
*/
abstract class HMAC implements SignerInterface
{
/**
* @inheritdoc
*/
public function sign($input, $key)
{
return hash_hmac($this->getHashingAlgorithm(), $input, $key, true);
}
/**
* To prevent timing attacks we are using PHP 5.6 native function hash_equals,
* in case of PHP < 5.6 a timing safe equals comparison function
*
* more info here:
* http://blog.ircmaxell.com/2014/11/its-all-about-time.
* http://blog.ircmaxell.com/2014/11/its-all-about-time.html
*
*
* @inheritdoc
*/
public function verify($key, $signature, $input)
{
$signedInput = $this->sign($input, $key);
if (version_compare(PHP_VERSION, '5.6.0', '>=')) {
return hash_equals($signature, $signedInput);
}
return $this->timingSafeEquals($signature, $signedInput);
}
/**
* A timing safe equals comparison
*
* @param string $signature the internal signature to be checked
* @param string $signedInput The signed input submitted value
*
* @return boolean true if the two strings are identical.
*/
public function timingSafeEquals($signature, $signedInput) {
$signatureLength = strlen($signature);
$signedInputLength = strlen($signedInput);
$result = 0;
if ($signedInputLength != $signatureLength) {
return false;
}
for ($i = 0; $i < $signedInputLength; $i++) {
$result |= (ord($signature[$i]) ^ ord($signedInput[$i]));
}
return $result === 0;
}
/**
* Returns the hashing algorithm used in this signer.
*
* @return string
*/
abstract public function getHashingAlgorithm();
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
/**
* HMAC Signer using SHA-256.
*/
class HS256 extends HMAC
{
public function getHashingAlgorithm()
{
return 'sha256';
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
/**
* HMAC Signer using SHA-384.
*/
class HS384 extends HMAC
{
public function getHashingAlgorithm()
{
return 'sha384';
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
/**
* HMAC Signer using SHA-512.
*/
class HS512 extends HMAC
{
public function getHashingAlgorithm()
{
return 'sha512';
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
use Namshi\JOSE\Signer\SignerInterface;
/**
* None Signer
*/
class None implements SignerInterface
{
/**
* @inheritdoc
*/
public function sign($input, $key)
{
return '';
}
/**
* @inheritdoc
*/
public function verify($key, $signature, $input)
{
return $signature === '';
}
}

View File

@@ -0,0 +1,84 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
use InvalidArgumentException;
use Namshi\JOSE\Signer\SignerInterface;
/**
* Class responsible to sign inputs with the a public key algorithm, after hashing it.
*/
abstract class PublicKey implements SignerInterface
{
/**
* @inheritdoc
*/
public function sign($input, $key, $password = null)
{
$keyResource = $this->getKeyResource($key, $password);
if (!$this->supportsKey($keyResource)) {
throw new InvalidArgumentException('Invalid key supplied.');
}
$signature = null;
openssl_sign($input, $signature, $keyResource, $this->getHashingAlgorithm());
return $signature;
}
/**
* @inheritdoc
*/
public function verify($key, $signature, $input)
{
$keyResource = $this->getKeyResource($key);
if (!$this->supportsKey($keyResource)) {
throw new InvalidArgumentException('Invalid key supplied.');
}
return (bool) openssl_verify($input, $signature, $keyResource, $this->getHashingAlgorithm());
}
/**
* Converts a string representation of a key into an OpenSSL resource
*
* @param string|resource $key
* @param string $password
* @return resource OpenSSL key resource
*/
protected function getKeyResource($key, $password = null) {
if (is_resource($key)) {
return $key;
}
return openssl_pkey_get_public($key) ? : openssl_pkey_get_private($key, $password);
}
/**
* Check if the key is supported by this signer.
*
* @param resource $key Public or private key
* @return boolean
*/
protected function supportsKey($key)
{
// OpenSSL 0.9.8+
$keyDetails = openssl_pkey_get_details($key);
return isset($keyDetails['type']) ? $this->getSupportedPrivateKeyType() === $keyDetails['type'] : false;
}
/**
* Returns the hashing algorithm used in this signer.
*
* @return string
*/
abstract protected function getHashingAlgorithm();
/**
* Returns the private key type supported in this signer.
*
* @return string
*/
abstract protected function getSupportedPrivateKeyType();
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
/**
* Class responsible to sign inputs with the RSA algorithm, after hashing it.
*/
class RS256 extends RSA
{
public function getHashingAlgorithm()
{
return version_compare(phpversion(), '5.4.8', '<') ? 'SHA256' : OPENSSL_ALGO_SHA256;
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
/**
* Class responsible to sign inputs with the RSA algorithm, after hashing it.
*/
class RS384 extends RSA
{
public function getHashingAlgorithm()
{
return version_compare(phpversion(), '5.4.8', '<') ? 'SHA384' : OPENSSL_ALGO_SHA384;
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
/**
* Class responsible to sign inputs with the RSA algorithm, after hashing it.
*/
class RS512 extends RSA
{
public function getHashingAlgorithm()
{
return version_compare(phpversion(), '5.4.8', '<') ? 'SHA512' : OPENSSL_ALGO_SHA512;
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Namshi\JOSE\Signer\OpenSSL;
/**
* Class responsible to sign inputs with the a RSA algorithm, after hashing it.
*/
abstract class RSA extends PublicKey
{
/**
* @inheritdoc
*/
protected function getSupportedPrivateKeyType()
{
return defined('OPENSSL_KEYTYPE_RSA') ? OPENSSL_KEYTYPE_RSA : false;
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Namshi\JOSE\Signer\SecLib;
use InvalidArgumentException;
use Namshi\JOSE\Signer\SignerInterface;
abstract class PublicKey implements SignerInterface
{
protected $encryptionAlgorithm;
/**
* @inheritdoc
*/
public function sign($input, $key, $password = null)
{
if ($password) {
$this->encryptionAlgorithm->setPassword($password);
}
if (!$this->encryptionAlgorithm->loadKey($key)) {
throw new InvalidArgumentException('Invalid key supplied.');
}
return $this->encryptionAlgorithm->sign($input);
}
/**
* @inheritdoc
*/
public function verify($key, $signature, $input)
{
if (!$this->encryptionAlgorithm->loadKey($key)) {
throw new InvalidArgumentException('Invalid key supplied.');
}
return $this->encryptionAlgorithm->verify($input, $signature);
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Namshi\JOSE\Signer\SecLib;
class RS256 extends RSA
{
public function __construct() {
parent::__construct();
$this->encryptionAlgorithm->setHash('sha256');
$this->encryptionAlgorithm->setMGFHash('sha256');
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Namshi\JOSE\Signer\SecLib;
class RS384 extends RSA
{
public function __construct() {
parent::__construct();
$this->encryptionAlgorithm->setHash('sha384');
$this->encryptionAlgorithm->setMGFHash('sha384');
}
}

View File

@@ -0,0 +1,14 @@
<?php
namespace Namshi\JOSE\Signer\SecLib;
class RS512 extends RSA
{
public function __construct() {
parent::__construct();
$this->encryptionAlgorithm->setHash('sha512');
$this->encryptionAlgorithm->setMGFHash('sha512');
}
}

View File

@@ -0,0 +1,12 @@
<?php
namespace Namshi\JOSE\Signer\SecLib;
class RSA extends PublicKey
{
public function __construct() {
$this->encryptionAlgorithm = new \Crypt_RSA();
}
}

View File

@@ -0,0 +1,26 @@
<?php
namespace Namshi\JOSE\Signer;
interface SignerInterface
{
/**
* Signs the $input with the $key, after hashing it.
*
* @param string $input
* @param resource|string $key
* @return string|null
*/
public function sign($input, $key);
/**
* Verifies that the input correspond to the $signature decrypted with the
* given public $key.
*
* @param resource|string $key
* @param string $signature
* @param string $input
* @return boolean
*/
public function verify($key, $signature, $input);
}

View File

@@ -0,0 +1,73 @@
<?php
namespace Namshi\JOSE;
/**
* Class providing an easy to use JWS implementation.
*/
class SimpleJWS extends JWS
{
/**
* Constructor
*
* @param array $header An associative array of headers. The value can be any type accepted by json_encode or a JSON serializable object
* @see http://php.net/manual/en/function.json-encode.php
* @see http://php.net/manual/en/jsonserializable.jsonserialize.php
* @see https://tools.ietf.org/html/draft-ietf-jose-json-web-signature-41#section-4
* }
*/
public function __construct($header = array())
{
if (!isset($header['typ'])) {
$header['typ'] = 'JWS';
}
parent::__construct($header);
}
/**
* Sets the payload of the current JWS with an issued at value in the 'iat' property.
*
* @param array $payload
*/
public function setPayload(array $payload)
{
if (!isset($payload['iat'])) {
$now = new \DateTime('now');
$payload['iat'] = $now->format('U');
}
return parent::setPayload($payload);
}
/**
* Checks that the JWS has been signed with a valid private key by verifying it with a public $key
* and the token is not expired.
*
* @param resource|string $key
* @param string $algo The algorithms this JWS should be signed with. Use it if you want to restrict which algorithms you want to allow to be validated.
*
* @return bool
*/
public function isValid($key, $algo = null)
{
return $this->verify($key, $algo) && ! $this->isExpired();
}
/**
* Checks whether the token is expired based on the 'exp' value.
*
* @return bool
*/
protected function isExpired()
{
$payload = $this->getPayload();
if (isset($payload['exp']) && is_numeric($payload['exp'])) {
$now = new \DateTime('now');
return ($now->format('U') - $payload['exp']) > 0;
}
return false;
}
}

View File

@@ -0,0 +1,38 @@
<?php
namespace Namshi\JOSE\Test;
use Namshi\JOSE\Base64\Base64Encoder;
use Namshi\JOSE\JWS;
use PHPUnit_Framework_TestCase as TestCase;
/**
* BC test for base64 url-safe fix
* Test that tokens generated the old way (non url-safe) will work with url-safe base64 decoding
*/
class BCJWSTest extends TestCase
{
const SSL_KEY_PASSPHRASE = 'tests';
public function testTestBC()
{
$data = array(
array("order_nr" => "ae123123"),
array("username" => "asdasdasd"),
array("anything" => "!@#$%^&*()_+")
);
foreach ($data as $payload) {
$jwsOld = new JWS(array("alg" => "RS256"));
$jwsOld->setEncoder(new Base64Encoder());
$jwsOld->setPayload($payload);
$jwsOld->sign(openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", self::SSL_KEY_PASSPHRASE));
$t = $jwsOld->getTokenString();
$jwsNew = JWS::load($t);
$this->assertTrue($jwsNew->verify(openssl_pkey_get_public(SSL_KEYS_PATH . "public.key")));
}
}
}

View File

@@ -0,0 +1,268 @@
<?php
namespace Namshi\JOSE\Test;
use PHPUnit_Framework_TestCase as TestCase;
use Namshi\JOSE\JWS;
use DateTime;
use Prophecy\Argument;
class JWSTest extends TestCase
{
const SSL_KEY_PASSPHRASE = 'tests';
public function setup()
{
$date = new DateTime('tomorrow');
$data = array(
'a' => 'b'
);
$this->jws = new JWS(array('alg' => 'RS256'));
$this->jws->setPayload($data);
}
/**
* @expectedException InvalidArgumentException
*/
public function testLoadingUnsecureJwsWithNoneAlgo()
{
$date = new DateTime('tomorrow');
$data = array(
'a' => 'b',
'exp' => $date->format('U')
);
$this->jws = new JWS(array('alg' => 'None'));
$this->jws->setPayload($data);
$this->jws->sign('111');
$jws = JWS::load($this->jws->getTokenString());
$this->assertFalse($jws->verify('111'));
$payload = $jws->getPayload();
$this->assertEquals('b', $payload['a']);
}
/**
* @expectedException InvalidArgumentException
*/
public function testLoadingUnsecureJwsWithLowercaseNone()
{
$date = new DateTime('tomorrow');
$data = array(
'a' => 'b',
'exp' => $date->format('U')
);
$this->jws = new JWS(array('alg' => 'none'));
$this->jws->setPayload($data);
$this->jws->sign('111');
$jws = JWS::load($this->jws->getTokenString());
$this->assertFalse($jws->verify('111'));
$payload = $jws->getPayload();
$this->assertEquals('b', $payload['a']);
}
public function testAllowingUnsecureJws()
{
$date = new DateTime('tomorrow');
$data = array(
'a' => 'b',
'exp' => $date->format('U')
);
$this->jws = new JWS(array('alg' => 'None'));
$this->jws->setPayload($data);
$this->jws->sign('111');
$jws = JWS::load($this->jws->getTokenString(), true);
$this->assertTrue($jws->verify('111'));
$payload = $jws->getPayload();
$this->assertEquals('b', $payload['a']);
}
public function testRestrictingTheAlgorithmsKo()
{
$this->jws = new JWS(array('alg' => 'HS256'));
$this->jws->sign('12345');
$jws = JWS::load($this->jws->getTokenString());
$this->assertFalse($jws->verify('12345', 'RS256'));
}
public function testRestrictingTheAlgorithmsOk()
{
$date = new DateTime('tomorrow');
$data = array(
'a' => 'b',
'exp' => $date->format('U')
);
$this->jws = new JWS(array('alg' => 'HS256'));
$this->jws->setPayload($data);
$this->jws->sign('123');
$jws = JWS::load($this->jws->getTokenString());
$this->assertTrue($jws->verify('123', 'HS256'));
}
public function testVerificationRS256()
{
$privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", self::SSL_KEY_PASSPHRASE);
$this->jws->sign($privateKey);
$jws = JWS::load($this->jws->getTokenString());
$public_key = openssl_pkey_get_public(SSL_KEYS_PATH . "public.key");
$this->assertTrue($jws->verify($public_key));
$payload = $jws->getPayload();
$this->assertEquals('b', $payload['a']);
}
public function testVerificationRS256KeyAsString()
{
$privateKey = file_get_contents(TEST_DIR . "/private.key");//, self::SSL_KEY_PASSPHRASE);
$this->jws->sign($privateKey, self::SSL_KEY_PASSPHRASE);
$jws = JWS::load($this->jws->getTokenString());
$public_key = openssl_pkey_get_public(SSL_KEYS_PATH . "public.key");
$this->assertTrue($jws->verify($public_key));
$payload = $jws->getPayload();
$this->assertEquals('b', $payload['a']);
}
public function testUseOfCustomEncoder()
{
$encoder = $this->prophesize('Namshi\JOSE\Base64\Encoder');
$encoder
->decode(Argument::any())
->willReturn('{"whatever": "the payload should be"}')
->shouldBeCalled();
$encoder
->decode(Argument::any())
->willReturn('{"alg": "test"}')
->shouldBeCalled();
JWS::load($this->jws->getTokenString(), false, $encoder->reveal());
}
public function testVerificationThatTheJWSIsSigned()
{
$privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", self::SSL_KEY_PASSPHRASE);
$this->jws->sign($privateKey);
$this->assertTrue($this->jws->isSigned());
}
public function testVerificationThatTheJWSIsNotSigned()
{
$this->assertFalse($this->jws->isSigned());
}
/**
* @expectedException InvalidArgumentException
*/
public function testWrongVerificationRS256()
{
$privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", self::SSL_KEY_PASSPHRASE);
$this->jws->sign($privateKey);
$jws = JWS::load('eyJhbGciOiJ0ZXN0In0=.eyJhbGciOiJ0ZXN0In0=.eyJhbGciOiJ0ZXN0In0=');
$public_key = openssl_pkey_get_public(SSL_KEYS_PATH . "public.key");
$this->assertFalse($jws->verify($public_key));
}
/**
* @expectedException InvalidArgumentException
*/
public function testLoadingAMalformedTokenString()
{
JWS::load('test.Test.TEST');
}
/**
* @expectedException InvalidArgumentException
*/
public function testLoadingAMalformedTokenString2()
{
JWS::load('test');
}
public function testSignAndVerifyWithFalsePublicKey()
{
$public_key = false;
$jwsHMAC = new JWS(array('alg' => 'HS256'));
$jwsHMAC->sign(false);
$jws = JWS::load($jwsHMAC->getTokenString());
$this->assertFalse($jws->verify($public_key));
}
public function testSignAndVerifyWithEmptyStringPublicKey()
{
$public_key = false;
$jwsHMAC = new JWS(array('alg' => 'HS256'));
$jwsHMAC->sign('');
$jws = JWS::load($jwsHMAC->getTokenString());
$this->assertFalse($jws->verify($public_key));
}
public function testLoadingWithAnyOrderOfHeaders()
{
$privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", self::SSL_KEY_PASSPHRASE);
$public_key = openssl_pkey_get_public(SSL_KEYS_PATH . "public.key");
$this->jws = new JWS(array('alg' => 'RS256', 'custom' => '1'));
$header = $this->jws->getHeader();
$reversedHeader = array_reverse($header);
$this->assertFalse($header === $reversedHeader);
$this->jws->setHeader($reversedHeader);
$this->jws->sign($privateKey);
$tokenString = $this->jws->getTokenString();
$jws = JWS::load($tokenString);
$this->assertTrue($reversedHeader === $jws->getHeader());
}
public function testSignAndVerifyWithSecLib()
{
$jwsRSA = new JWS(array('alg' => 'RS256'), 'SecLib');
$data = array('a' => 'b',);
$jwsRSA->setPayload($data);
$jwsRSA->sign(file_get_contents(SSL_KEYS_PATH . "private.key"), 'tests');
$jws = JWS::load($jwsRSA->getTokenString(), false, null, 'SecLib');
$this->assertTrue($jws->verify(file_get_contents(SSL_KEYS_PATH . "public.key", 'RS256')));
}
public function testConstructionFromHeader()
{
$header = array('alg' => 'RS256', 'test' => true);
$jws = new JWS($header);
$this->assertTrue($header == $jws->getHeader());
}
public function testVerificationCustomizedHeader()
{
$header = $this->jws->getHeader();
$header['test'] = true;
$this->jws->setHeader($header);
$privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", self::SSL_KEY_PASSPHRASE);
$this->jws->sign($privateKey);
$jws = JWS::load($this->jws->getTokenString());
$public_key = openssl_pkey_get_public(SSL_KEYS_PATH . "public.key");
$headerFromSig = $jws->getHeader();
$this->assertSame($headerFromSig['test'], true);
$this->assertTrue($jws->verify($public_key));
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Namshi\JOSE\Test;
use Namshi\JOSE\Base64\Base64UrlSafeEncoder;
use PHPUnit_Framework_TestCase as TestCase;
use Namshi\JOSE\JWT;
class JWTTest extends TestCase
{
public function testGenerationOfTheSigninInput()
{
$payload = array('b' => 'a', 'iat' => 1421161177);
$header = array('a' => 'b');
$jwt = new JWT($payload, $header);
$encoder = new Base64UrlSafeEncoder();
$this->assertEquals(sprintf("%s.%s", $encoder->encode(json_encode($header)), $encoder->encode(json_encode($payload))), $jwt->generateSigninInput());
}
public function testPayload()
{
$jwt = new JWT(array('a' => 'b'), array());
$payload = $jwt->getPayload();
$this->assertSame(array('a' => 'b'), $payload);
$jwt = new JWT(array('a' => 'b'), array());
$jwt->setPayload(array('b' => 'a'));
$payload = $jwt->getPayload();
$this->assertSame($payload['b'], 'a');
$this->assertSame(array('b' => 'a'), $payload);
}
}

View File

@@ -0,0 +1,44 @@
<?php
namespace Namshi\JOSE\Test\OpenSSL\Signer;
use Namshi\JOSE\Signer\OpenSSL\ES256;
use \PHPUnit_Framework_TestCase as TestCase;
class ES256Test extends TestCase
{
public function setup()
{
// https://github.com/sebastianbergmann/phpunit/issues/1356
if (defined('HHVM_VERSION')) {
$this->markTestSkipped();
}
$this->privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.es256.key");
$this->public = openssl_pkey_get_public(SSL_KEYS_PATH . "public.es256.key");
$this->signer = new ES256;
}
public function testVerificationWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKey);
$this->assertInternalType('bool', $this->signer->verify($this->public, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->public, $encrypted, 'aaa'));
}
/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Invalid key supplied.
*/
public function testWrongKeyCurve()
{
$privateKey512 = openssl_pkey_get_private(SSL_KEYS_PATH . "private.es512.key");
$this->signer->sign('aaa', $privateKey512);
}
public function testSigningWorksProperly()
{
$this->assertInternalType('string', $this->signer->sign('aaa', $this->privateKey));
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Namshi\JOSE\Test\OpenSSL\Signer;
use Namshi\JOSE\Signer\OpenSSL\ES384;
use \PHPUnit_Framework_TestCase as TestCase;
class ES384Test extends TestCase
{
public function setup()
{
// https://github.com/sebastianbergmann/phpunit/issues/1356
if (defined('HHVM_VERSION')) {
$this->markTestSkipped();
}
$this->privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.es384.key", 'tests');
$this->public = openssl_pkey_get_public(SSL_KEYS_PATH . "public.es384.key");
$this->signer = new ES384;
}
public function testVerificationWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKey);
$this->assertInternalType('bool', $this->signer->verify($this->public, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->public, $encrypted, 'aaa'));
}
public function testSigningWorksProperly()
{
$this->assertInternalType('string', $this->signer->sign('aaa', $this->privateKey));
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace Namshi\JOSE\Test\OpenSSL\Signer;
use Namshi\JOSE\Signer\OpenSSL\ES512;
use \PHPUnit_Framework_TestCase as TestCase;
class ES512Test extends TestCase
{
public function setup()
{
// https://github.com/sebastianbergmann/phpunit/issues/1356
if (defined('HHVM_VERSION')) {
$this->markTestSkipped();
}
$this->privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.es512.key");
$this->public = openssl_pkey_get_public(SSL_KEYS_PATH . "public.es512.key");
$this->signer = new ES512;
}
public function testVerificationWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKey);
$this->assertInternalType('bool', $this->signer->verify($this->public, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->public, $encrypted, 'aaa'));
}
public function testSigningWorksProperly()
{
$this->assertInternalType('string', $this->signer->sign('aaa', $this->privateKey));
}
}

View File

@@ -0,0 +1,19 @@
<?php
namespace Namshi\JOSE\Test\OpenSSL\Signer;
use Namshi\JOSE\Signer\OpenSSL\HS256;
use \PHPUnit_Framework_TestCase as TestCase;
class HS256Test extends TestCase
{
public function testSigningAndVerificationWorkProperly()
{
$signer = new HS256;
$signature = $signer->sign('aaa', 'foo');
$this->assertEquals($signature, base64_decode('P2Pb8e2Ja4P4YnTZ3EF002RKpUpOnfjIy0uLNT0R1J0='));
$this->assertTrue($signer->verify('foo', $signature, 'aaa'));
$this->assertFalse($signer->verify('bar', $signature, 'aaa'));
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Namshi\JOSE\Test\OpenSSL\Signer;
use \PHPUnit_Framework_TestCase as TestCase;
use Namshi\JOSE\Signer\OpenSSL\HS384;
class HS384Test extends TestCase
{
public function testSigningAndVerificationWorkProperly()
{
$signer = new HS384;
$signature = $signer->sign('aaa', 'foo');
$this->assertEquals($signature, base64_decode('W6Cd7qZknNYIXOxTrpEWFFwfuX0e2j59hTH4kVFh5o+9rcnfNtphLg4V8YXfkXGF'));
$this->assertTrue($signer->verify('foo', $signature, 'aaa'));
$this->assertFalse($signer->verify('bar', $signature, 'aaa'));
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace Namshi\JOSE\Test\OpenSSL\Signer;
use \PHPUnit_Framework_TestCase as TestCase;
use Namshi\JOSE\Signer\OpenSSL\HS512;
class HS512Test extends TestCase
{
public function testSigningAndVerificationWorkProperly()
{
$signer = new HS512;
$signature = $signer->sign('aaa', 'foo');
$this->assertEquals($signature, base64_decode('GysqRX8GoD6BCTrI5sJy1ptn9A7vbDlvFOnaAxO/t+BD8KVrVAUVcHMxgM68ZNxnUNkb7kNSq3YxkCV4pBvTjg=='));
$this->assertTrue($signer->verify('foo', $signature, 'aaa'));
$this->assertFalse($signer->verify('bar', $signature, 'aaa'));
}
}

View File

@@ -0,0 +1,41 @@
<?php
namespace Namshi\JOSE\Test\OpenSSL\Signer;
use \PHPUnit_Framework_TestCase as TestCase;
use Namshi\JOSE\Signer\OpenSSL\RS256;
class PublicKeyTest extends TestCase
{
public function setup()
{
$this->privateKeyResource = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", 'tests');
$this->privateKeyString = "-----BEGIN PRIVATE KEY-----\nMIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAN91kQxBuaze3WjI\nCNjeR/HD8E3kDzp89+Lhtn3tMish4yQxhNl6BEkabuS3pUj3WDP6+AFjBVqA1j3f\nu8Wqu7hRJDPHOs2kCII+LhIqvqQTLx/nvNOUhW2DimKn0HuHnlwJODq0MHFJEq5R\nrJH+mFGsP9yMGz4MxA04E2RVbUJRAgMBAAECgYEAjrDrO3Fo2GvD5Jn/lER0mnxt\nIb/kvYt5WyaYutbRN1u/SKhaVeklfWzkrSZb5DkV2LOE1JXfoEgvBnms1O9OSJXw\nqDrFF7NDebw95g6JzI+SbkIHw0Cb+/E9K92FjvW3Bi8j9PKIa8c/dpwIAIirc/q8\nuhSTf4WoIOHSFbSaQPECQQD1Wi9vynJLI5lShOs0wPomZOwNrXa73Lj8ciZC4oPS\nt6tWjbLnLsP+vTSLUyEYeQGsjdbY+y5siJmAqnV/ShB9AkEA6Sgna9gQw4dXN0jB\nSjOZSjl4S2/H3wHatclrvlYfbJVU6GlIlqWGaUkdFvCuEr9iXJAY4zpEQ4P370EZ\ntsyVZQJBAOZu/X6RNSc9GBNYo0+4rzjAMLPn50wp0dPHogfPlt+hgVqZWx2l3o6y\nRVdVjA/gFqJp1Q+VWdS1tvYRIqmadkECQCVdqQuwgedEHmcewtNod42crjmwvWBx\nBKMTl6/WT4zwVb41eUujVWo0LHRLuCoK//GDqmloIh6L3MU8MqnIGb0CQFWcpD4/\nroCkMblk0hPoQPpyapJexc438x7XuEGFEhyxxauqC5R4YFKCf+KBS2gZgr4GSwBU\nQww+qZ3eRYM7faM=\n-----END PRIVATE KEY-----";
$this->privateKeyFilePath = SSL_KEYS_PATH . 'private-ne.key';
$this->publicKeyResource = openssl_pkey_get_public(SSL_KEYS_PATH . "public.key");
$this->publicKeyString = "-----BEGIN PUBLIC KEY-----\nMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfdZEMQbms3t1oyAjY3kfxw/BN\n5A86fPfi4bZ97TIrIeMkMYTZegRJGm7kt6VI91gz+vgBYwVagNY937vFqru4USQz\nxzrNpAiCPi4SKr6kEy8f57zTlIVtg4pip9B7h55cCTg6tDBxSRKuUayR/phRrD/c\njBs+DMQNOBNkVW1CUQIDAQAB\n-----END PUBLIC KEY-----";
$this->publicKeyFilePath = SSL_KEYS_PATH . 'public-ne.key';
$this->signer = new RS256;
}
public function testStringKeyWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKeyString);
$this->assertInternalType('bool', $this->signer->verify($this->publicKeyString, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->publicKeyString, $encrypted, 'aaa'));
}
public function testFilePathKeyWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKeyFilePath);
$this->assertInternalType('bool', $this->signer->verify($this->publicKeyFilePath, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->publicKeyFilePath, $encrypted, 'aaa'));
}
public function testResourceKeyWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKeyResource);
$this->assertInternalType('bool', $this->signer->verify($this->publicKeyResource, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->publicKeyResource, $encrypted, 'aaa'));
}
}

View File

@@ -0,0 +1,24 @@
<?php
namespace Namshi\JOSE\Test\OpenSSL\Signer;
use \PHPUnit_Framework_TestCase as TestCase;
use Namshi\JOSE\Signer\OpenSSL\None;
class noneTest extends TestCase
{
public function testVerificationWorksProperly()
{
$signer = new none;
$this->assertTrue($signer->verify('bar', '', 'aaa'));
}
public function testSigningWorksProperly()
{
$signer = new none;
$signature = $signer->sign('aaa', 'foo');
$this->assertTrue($signature === '');
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Namshi\JOSE\Test\OpenSSL\Signer;
use \PHPUnit_Framework_TestCase as TestCase;
use Namshi\JOSE\Signer\OpenSSL\RS256;
class RS256Test extends TestCase
{
public function setup()
{
$this->privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", 'tests');
$this->public = openssl_pkey_get_public(SSL_KEYS_PATH . "public.key");
$this->signer = new RS256;
}
public function testVerificationWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKey);
$this->assertInternalType('bool', $this->signer->verify($this->public, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->public, $encrypted, 'aaa'));
}
public function testSigningWorksProperly()
{
$this->assertInternalType('string', $this->signer->sign('aaa', $this->privateKey));
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Namshi\JOSE\Test\OpenSSL\Signer;
use \PHPUnit_Framework_TestCase as TestCase;
use Namshi\JOSE\Signer\OpenSSL\RS384;
class RS384Test extends TestCase
{
public function setup()
{
$this->privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", 'tests');
$this->public = openssl_pkey_get_public(SSL_KEYS_PATH . "public.key");
$this->signer = new RS384;
}
public function testVerificationWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKey);
$this->assertInternalType('bool', $this->signer->verify($this->public, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->public, $encrypted, 'aaa'));
}
public function testSigningWorksProperly()
{
$this->assertInternalType('string', $this->signer->sign('aaa', $this->privateKey));
}
}

View File

@@ -0,0 +1,28 @@
<?php
namespace Namshi\JOSE\Test\OpenSSL\Signer;
use \PHPUnit_Framework_TestCase as TestCase;
use Namshi\JOSE\Signer\OpenSSL\RS512;
class RS512Test extends TestCase
{
public function setup()
{
$this->privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", 'tests');
$this->public = openssl_pkey_get_public(SSL_KEYS_PATH . "public.key");
$this->signer = new RS512;
}
public function testVerificationWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKey);
$this->assertInternalType('bool', $this->signer->verify($this->public, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->public, $encrypted, 'aaa'));
}
public function testSigningWorksProperly()
{
$this->assertInternalType('string', $this->signer->sign('aaa', $this->privateKey));
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Namshi\JOSE\Test\SecLib\Signer;
use Namshi\JOSE\Signer\SecLib\RS256;
use \PHPUnit_Framework_TestCase as TestCase;
class RS256Test extends TestCase {
public function setup()
{
$this->privateKey = file_get_contents(SSL_KEYS_PATH . "private.key");
$this->public = file_get_contents(SSL_KEYS_PATH . "public.key");
$this->password = 'tests';
$this->signer = new RS256();
}
public function testVerificationWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKey, $this->password);
$this->assertInternalType('bool', $this->signer->verify($this->public, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->public, $encrypted, 'aaa'));
}
public function testSigningWorksProperly()
{
$this->assertInternalType('string', $this->signer->sign('aaa', $this->privateKey, $this->password));
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Namshi\JOSE\Test\SecLib\Signer;
use Namshi\JOSE\Signer\SecLib\RS384;
use \PHPUnit_Framework_TestCase as TestCase;
class RS384Test extends TestCase {
public function setup()
{
$this->privateKey = file_get_contents(SSL_KEYS_PATH . "private.key");
$this->public = file_get_contents(SSL_KEYS_PATH . "public.key");
$this->password = 'tests';
$this->signer = new RS384();
}
public function testVerificationWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKey, $this->password);
$this->assertInternalType('bool', $this->signer->verify($this->public, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->public, $encrypted, 'aaa'));
}
public function testSigningWorksProperly()
{
$this->assertInternalType('string', $this->signer->sign('aaa', $this->privateKey, $this->password));
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Namshi\JOSE\Test\SecLib\Signer;
use Namshi\JOSE\Signer\SecLib\RS512;
use \PHPUnit_Framework_TestCase as TestCase;
class RS512Test extends TestCase {
public function setup()
{
$this->privateKey = file_get_contents(SSL_KEYS_PATH . "private.key");
$this->public = file_get_contents(SSL_KEYS_PATH . "public.key");
$this->password = 'tests';
$this->signer = new RS512();
}
public function testVerificationWorksProperly()
{
$encrypted = $this->signer->sign('aaa', $this->privateKey, $this->password);
$this->assertInternalType('bool', $this->signer->verify($this->public, $encrypted, 'aaa'));
$this->assertTrue($this->signer->verify($this->public, $encrypted, 'aaa'));
}
public function testSigningWorksProperly()
{
$this->assertInternalType('string', $this->signer->sign('aaa', $this->privateKey, $this->password));
}
}

View File

@@ -0,0 +1,53 @@
<?php
namespace Namshi\JOSE\Test;
use PHPUnit_Framework_TestCase as TestCase;
use Namshi\JOSE\SimpleJWS;
use DateTime;
class SimpleJWSTest extends TestCase
{
const SSL_KEY_PASSPHRASE = 'tests';
public function setup()
{
$date = new DateTime('tomorrow');
$data = array(
'a' => 'b',
'exp' => $date->format('U')
);
$this->jws = new SimpleJWS(array('alg' => 'RS256'));
$this->jws->setPayload($data);
}
public function testConstruction()
{
$this->assertSame($this->jws->getHeader(), array('alg' => 'RS256', 'typ' => 'JWS'));
$this->assertRegExp('/^\d+$/', $this->jws->getPayload()['iat'], 'iat property has integer value (from construction)');
}
public function testValidationOfAValidSimpleJWS()
{
$privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", self::SSL_KEY_PASSPHRASE);
$this->jws->sign($privateKey);
$jws = SimpleJWS::load($this->jws->getTokenString());
$public_key = openssl_pkey_get_public(SSL_KEYS_PATH . "public.key");
$this->assertTrue($jws->isValid($public_key, 'RS256'));
}
public function testValidationOfInvalidSimpleJWS()
{
$date = new DateTime('yesterday');
$this->jws->setPayload(array(
'exp' => $date->format('U')
));
$privateKey = openssl_pkey_get_private(SSL_KEYS_PATH . "private.key", self::SSL_KEY_PASSPHRASE);
$this->jws->sign($privateKey);
$jws = SimpleJWS::load($this->jws->getTokenString());
$public_key = openssl_pkey_get_public(SSL_KEYS_PATH . "public.key");
$this->assertFalse($jws->isValid($public_key, 'RS256'));
}
}

View File

@@ -0,0 +1,7 @@
<?php
$loader = require __DIR__ . '/../vendor/autoload.php';
$loader->add("Namshi\\JOSE\\Test", __DIR__);
define('TEST_DIR', __DIR__);
define('SSL_KEYS_PATH', "file://" . TEST_DIR . DIRECTORY_SEPARATOR);

16
vendor/namshi/jose/tests/private-ne.key vendored Normal file
View File

@@ -0,0 +1,16 @@
-----BEGIN PRIVATE KEY-----
MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAN91kQxBuaze3WjI
CNjeR/HD8E3kDzp89+Lhtn3tMish4yQxhNl6BEkabuS3pUj3WDP6+AFjBVqA1j3f
u8Wqu7hRJDPHOs2kCII+LhIqvqQTLx/nvNOUhW2DimKn0HuHnlwJODq0MHFJEq5R
rJH+mFGsP9yMGz4MxA04E2RVbUJRAgMBAAECgYEAjrDrO3Fo2GvD5Jn/lER0mnxt
Ib/kvYt5WyaYutbRN1u/SKhaVeklfWzkrSZb5DkV2LOE1JXfoEgvBnms1O9OSJXw
qDrFF7NDebw95g6JzI+SbkIHw0Cb+/E9K92FjvW3Bi8j9PKIa8c/dpwIAIirc/q8
uhSTf4WoIOHSFbSaQPECQQD1Wi9vynJLI5lShOs0wPomZOwNrXa73Lj8ciZC4oPS
t6tWjbLnLsP+vTSLUyEYeQGsjdbY+y5siJmAqnV/ShB9AkEA6Sgna9gQw4dXN0jB
SjOZSjl4S2/H3wHatclrvlYfbJVU6GlIlqWGaUkdFvCuEr9iXJAY4zpEQ4P370EZ
tsyVZQJBAOZu/X6RNSc9GBNYo0+4rzjAMLPn50wp0dPHogfPlt+hgVqZWx2l3o6y
RVdVjA/gFqJp1Q+VWdS1tvYRIqmadkECQCVdqQuwgedEHmcewtNod42crjmwvWBx
BKMTl6/WT4zwVb41eUujVWo0LHRLuCoK//GDqmloIh6L3MU8MqnIGb0CQFWcpD4/
roCkMblk0hPoQPpyapJexc438x7XuEGFEhyxxauqC5R4YFKCf+KBS2gZgr4GSwBU
Qww+qZ3eRYM7faM=
-----END PRIVATE KEY-----

View File

@@ -0,0 +1,5 @@
-----BEGIN EC PRIVATE KEY-----
MHcCAQEEIKv1ZMzZ8Uxt/YxwdKpMAP0nlV7ne8gh0+5G+5Gb/tMUoAoGCCqGSM49
AwEHoUQDQgAEvuYsP+QnrqAbM7Iyhzjt08hFSuzapyojCB/gFsBt65Wir4TYr5fS
Q96oa4qeGVeTFzl+fGiZFILootvLsiPwAQ==
-----END EC PRIVATE KEY-----

View File

@@ -0,0 +1,6 @@
-----BEGIN EC PRIVATE KEY-----
MIGkAgEBBDClxJJett5kQ5oEizsjCpxT0z844zzVeFm44egaCZL/Y90QLBx1BxfO
/tbz6VgvRyugBwYFK4EEACKhZANiAATp/5dmyDZO+fQSgRqlD7KUxg22ybwI9/Rx
vwcjYSR9j0Gqm3dAzPCUzuZWwVGZoxlvyc6dHCamYSe8DZTzJ1L51uc+/tvBiX6r
Wo16HxamOivdU75FO3hx7Q+fbmgYZZQ=
-----END EC PRIVATE KEY-----

View File

@@ -0,0 +1,7 @@
-----BEGIN EC PRIVATE KEY-----
MIHbAgEBBEEWnooUpGIch1H/s8/ZUrHPo6RL+mHKhCrDO/Yjz37zM/tBJyvHmvwY
Utw3mYII0m3es3dIiAjheghBs14+UCPq8aAHBgUrgQQAI6GBiQOBhgAEAVpvo7TG
pQk5P7ZLo0qkBpaT+fFDv6HQrWElBKMxcrJd/mRNapweATsVv83YON4lTIIRXzgG
kmWeqbDr6RQO+1cSAIs+MoRmLaiPyG2xmPwQCHX2CGX/uCZiT3iOxTAJEZuUbeSA
828K4WfAA4ODdGiB87YVShhPOkiQswV3LpbpPGhC
-----END EC PRIVATE KEY-----

30
vendor/namshi/jose/tests/private.key vendored Normal file
View File

@@ -0,0 +1,30 @@
-----BEGIN RSA PRIVATE KEY-----
Proc-Type: 4,ENCRYPTED
DEK-Info: AES-256-CBC,510C9E7AAF17A8397C4B79D906DAE432
W0qO1iOiS10gdomVUBbR3LdJOHRmfOmsXAjg4ezG3+Dg/0rfVkj+H3RWIpNnLq2l
ciayAqjFzLzlKYPW4/XakyhfmCrZE+mL+kFVEUjhISZ+xXLwHGltL0UeYsei/AUp
usjbxO8K0hZ1ZG2bQCEj5ff2YGmC9g1K3C7tE8XR0+QCZ/xaYkdUxDUIfYZ4MebG
+C5P+mckds6lmcdZkIZj9H+36LPH2RPlMJAdLmLGytKF37h93gcL/pgePMEY+sVj
h0YfScQ2GlE3v3S1CC1X2yjU5CtfxPHuA+2XuFIZ9twlrNFSqUWtvaCMNgSaWPLH
KdR9RC8vImz0PEu6eR4t9ytTnykYG+mujCjumeohJoWjrXJymC4ocHIa8p/EA7Hc
AWXxVlS897feG1BQ+rAiuvzIDvU8glKO8ZDjs9FUeC4O6ySCI9b4Of/57Uqo7nmQ
0nLTJntvKFeusJ9WnergOEEHg1x93n8ajM49a+eS2uEj+UlIWiT01WTVS2i5wWp7
+wcniYwlEk36HHBWSYBmhK+Uda4isE3SBBiRBa4ETh7Goor3FcxTA5TEqKwsHG0o
1infKNmPHUy2hQTrGttoJrCpAvKq2RuQJUG5Tu5mURibihcRqCCcbyz/uKY/IIJg
9WV3gzsDXrmr2UWovD9Rfudm6mdkhz1Sn+XS69z3irMJHuW/j2sO/bO5JseurPq7
0V9Ms2TCIlyT7ncjsV3Lf0PSmitf/W4KzRKgxKkgORHNWX4W9yLA+yMeP3CfwaBn
QiTk/dnN/uwVFQ5YlRkUsWOFxS7/gk/kBv15dTZV/1paqm0iPcExmtXqFvPlxnUr
J3NeVl6j6kOUGLPsijk4zIijVMquYug4WfjCHLmzsqEGOYoK+nA6BQwxeMSOxFWT
R8QWvvz8B6fm5BigBsUs7kQa3HcksE6YMz8dXr3r0cvINurGUpd3hDYLMdUJ0ttG
cjdHXG3+fBOhUD8zkQSpf21V1/B4df2PiFlTKar5Jx1IiplvkTDPv6Nea8zuiVzy
8Wp/PawNbPRs+KQAsr+pvged+VftvsVVGyG+0lXd7kPAJqxo4xEpD+NelHJKDgVy
uuQPMoxt1TIvCAP9jD+ENCmnGU2HhpJvngsVQs+7DviCOgbbdIfff91fyrLkSNkb
tck4q3RFpKDcKiU9yxjeAaYP6bXSq2ypwHV4YhivftyJN52TxAjGRqkuZCr5cG9P
+l3hgwTusJSjiNFkjSv7Tq3sq+s6p3+vdqBs7pS6wH/yfnJtSnPgdOkO9NqvYG67
ALco0hSuBmKWFjr4rk9e/fVPqOFCKZNdJ44ZFViFkF1Ry/YO7XvnxCQCOk17g/aM
daMSNDJdZliGsu6lHxzF0/gq7ukTqAYJEh0Jvb7+l8/YMJZRFzxB0SENCTn9rPIY
VTrajK8z33GMHUFzbGEyoZoGxNe0F4DarHqFteBjnRLFev0N88go0hlLP0NR2hBB
RsNRTJGbzYTVZJwZTDrA11KoU3PaWFrZglsD/ExVb+OYgYv+SlbUdu9znpZBlayj
3wJv6RqhDntrbc2yPzK/27KzToNLlqdBnUO0kl64JaZukkhpDfmKJahWO4nePxvu
-----END RSA PRIVATE KEY-----

View File

@@ -0,0 +1,6 @@
-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDfdZEMQbms3t1oyAjY3kfxw/BN
5A86fPfi4bZ97TIrIeMkMYTZegRJGm7kt6VI91gz+vgBYwVagNY937vFqru4USQz
xzrNpAiCPi4SKr6kEy8f57zTlIVtg4pip9B7h55cCTg6tDBxSRKuUayR/phRrD/c
jBs+DMQNOBNkVW1CUQIDAQAB
-----END PUBLIC KEY-----

View File

@@ -0,0 +1,4 @@
-----BEGIN PUBLIC KEY-----
MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEvuYsP+QnrqAbM7Iyhzjt08hFSuza
pyojCB/gFsBt65Wir4TYr5fSQ96oa4qeGVeTFzl+fGiZFILootvLsiPwAQ==
-----END PUBLIC KEY-----

View File

@@ -0,0 +1,5 @@
-----BEGIN PUBLIC KEY-----
MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAE6f+XZsg2Tvn0EoEapQ+ylMYNtsm8CPf0
cb8HI2EkfY9Bqpt3QMzwlM7mVsFRmaMZb8nOnRwmpmEnvA2U8ydS+dbnPv7bwYl+
q1qNeh8Wpjor3VO+RTt4ce0Pn25oGGWU
-----END PUBLIC KEY-----

View File

@@ -0,0 +1,6 @@
-----BEGIN PUBLIC KEY-----
MIGbMBAGByqGSM49AgEGBSuBBAAjA4GGAAQBWm+jtMalCTk/tkujSqQGlpP58UO/
odCtYSUEozFysl3+ZE1qnB4BOxW/zdg43iVMghFfOAaSZZ6psOvpFA77VxIAiz4y
hGYtqI/IbbGY/BAIdfYIZf+4JmJPeI7FMAkRm5Rt5IDzbwrhZ8ADg4N0aIHzthVK
GE86SJCzBXculuk8aEI=
-----END PUBLIC KEY-----

9
vendor/namshi/jose/tests/public.key vendored Normal file
View File

@@ -0,0 +1,9 @@
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAtpS1ZmfVKVP5KofIhMBP
0tSWc4qlh6fm2lrZSkuKxUjEaWjzZSzs72gEIGxraWusMdoRuV54xsWRyf5KeZT0
S+I5Prle3Idi3gICiO4NwvMk6JwSBcJWwmSLFEKyUSnB2CtfiGc0/5rQCpcEt/Dn
5iM+BNn7fqpoLIbks8rXKUIj8+qMVqkTXsEKeKinE23t1ykMldsNaaOH+hvGti5J
t2DMnH1JjoXdDXfxvSP/0gjUYb0ektudYFXoA6wekmQyJeImvgx4Myz1I4iHtkY/
Cp7J4Mn1ejZ6HNmyvoTE/4OuY1uCeYv4UyXFc1s1uUyYtj4z57qsHGsS4dQ3A2MJ
swIDAQAB
-----END PUBLIC KEY-----