Skip to content

Commit

Permalink
Added supported algorithms
Browse files Browse the repository at this point in the history
  • Loading branch information
sizeg committed Dec 21, 2018
1 parent c255df1 commit 75d68ae
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 30 deletions.
21 changes: 15 additions & 6 deletions Jwt.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
use Lcobucci\JWT\Parser;
use Lcobucci\JWT\Parsing\Decoder;
use Lcobucci\JWT\Parsing\Encoder;
use Lcobucci\JWT\Signer;
use Lcobucci\JWT\Signer\Key;
use Lcobucci\JWT\Token;
use Lcobucci\JWT\ValidationData;
use Yii;
use yii\base\Component;
use yii\base\InvalidParamException;
use yii\base\InvalidArgumentException;

/**
* JSON Web Token implementation, based on this library:
Expand All @@ -25,17 +27,21 @@ class Jwt extends Component

/**
* @var array Supported algorithms
* @todo Add RSA, ECDSA suppport
*/
public $supportedAlgs = [
'HS256' => 'Lcobucci\JWT\Signer\Hmac\Sha256',
'HS384' => 'Lcobucci\JWT\Signer\Hmac\Sha384',
'HS512' => 'Lcobucci\JWT\Signer\Hmac\Sha512',
'ES256' => 'Lcobucci\JWT\Signer\Ecdsa\Sha256',
'ES384' => 'Lcobucci\JWT\Signer\Ecdsa\Sha384',
'ES512' => 'Lcobucci\JWT\Signer\Ecdsa\Sha512',
'RS256' => 'Lcobucci\JWT\Signer\Rsa\Sha256',
'RS384' => 'Lcobucci\JWT\Signer\Rsa\Sha384',
'RS512' => 'Lcobucci\JWT\Signer\Rsa\Sha512',
];

/**
* @var string|array|null $key The key, or map of keys.
* @todo Add RSA, ECDSA key file support
* @var Key|string $key The key
*/
public $key;

Expand Down Expand Up @@ -70,11 +76,12 @@ public function getValidationData($currentTime = null)
* Parses the JWT and returns a token class
* @param string $token JWT
* @return Token|null
* @throws \Throwable
*/
public function loadToken($token, $validate = true, $verify = true)
{
try {
$token = $this->getParser()->parse((string)$token);
$token = $this->getParser()->parse((string) $token);
} catch (\RuntimeException $e) {
Yii::warning("Invalid JWT provided: " . $e->getMessage(), 'jwt');
return null;
Expand Down Expand Up @@ -111,15 +118,17 @@ public function validateToken(Token $token, $currentTime = null)
* Validate token
* @param Token $token token object
* @return bool
* @throws \Throwable
*/
public function verifyToken(Token $token)
{
$alg = $token->getHeader('alg');

if (empty($this->supportedAlgs[$alg])) {
throw new InvalidParamException('Algorithm not supported');
throw new InvalidArgumentException('Algorithm not supported');
}

/** @var Signer $signer */
$signer = Yii::createObject($this->supportedAlgs[$alg]);

return $token->verify($signer, $this->key);
Expand Down
20 changes: 16 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
{
"name": "sizeg/yii2-jwt",
"description": "JWT based on Icobucci",
"type": "yii2-extension",
"keywords": ["yii2", "yii 2", "jwt"],
"authors": [
{
"name": "Dmitriy Demin",
"email": "[email protected]",
"homepage": "https://sizeg.tk"
}
],
"require": {
"php": ">=5.5.0",
"lcobucci/jwt": "~3.2.0",
"yiisoft/yii2": "*"
"yiisoft/yii2": "~2.0.0"
},
"require-dev": {
"phpunit/phpunit": "^4.8",
"doctrine/instantiator": "1.0.5",
"phpdocumentor/reflection-docblock": "3.2.2"
"phpunit/phpunit": "^4.8"
},
"autoload": {
"psr-4": {
"sizeg\\jwt\\": ""
}
},
"autoload-dev": {
"psr-4": {
"sizeg\\jwt\\tests\\": "tests/"
}
},
"repositories": [
{
Expand Down
19 changes: 15 additions & 4 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
<phpunit bootstrap="./tests/bootstrap.php">
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="Hello World Test Suite">
<directory>./tests/</directory>
<testsuite name="Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
<filter>
<whitelist>
<directory suffix=".php">./</directory>
</whitelist>
</filter>
</phpunit>
4 changes: 2 additions & 2 deletions tests/JwtTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace jwttests;
namespace sizeg\jwt\tests;

class JwtTest extends \PHPUnit\Framework\TestCase
class JwtTest extends TestCase
{

/**
Expand Down
46 changes: 46 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace sizeg\jwt\tests;

use yii\console\Application;

/**
* Class TestCase
* @author SiZE
*/
class TestCase extends \PHPUnit_Framework_TestCase
{

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->mockApplication();
}

/**
* @inheritdoc
*/
protected function tearDown()
{
$this->destroyApplication();
parent::tearDown();
}

protected function mockApplication()
{
new Application([
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => dirname(__DIR__) . '/vendor',
'runtimePath' => __DIR__ . '/runtime',
]);
}

protected function destroyApplication()
{
\Yii::$app = null;
}
}
19 changes: 5 additions & 14 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
<?php

// ensure we get report on all possible php errors
error_reporting(-1);

define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_DEBUG', true);

defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'test');

$_SERVER['SCRIPT_NAME'] = '/' . basename(__FILE__);
$_SERVER['SCRIPT_FILENAME'] = __FILE__;

require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

Yii::setAlias('@jwttests', __DIR__);

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

0 comments on commit 75d68ae

Please sign in to comment.