Skip to content

Commit 75d68ae

Browse files
committed
Added supported algorithms
1 parent c255df1 commit 75d68ae

File tree

6 files changed

+99
-30
lines changed

6 files changed

+99
-30
lines changed

Jwt.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@
77
use Lcobucci\JWT\Parser;
88
use Lcobucci\JWT\Parsing\Decoder;
99
use Lcobucci\JWT\Parsing\Encoder;
10+
use Lcobucci\JWT\Signer;
11+
use Lcobucci\JWT\Signer\Key;
1012
use Lcobucci\JWT\Token;
1113
use Lcobucci\JWT\ValidationData;
1214
use Yii;
1315
use yii\base\Component;
14-
use yii\base\InvalidParamException;
16+
use yii\base\InvalidArgumentException;
1517

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

2628
/**
2729
* @var array Supported algorithms
28-
* @todo Add RSA, ECDSA suppport
2930
*/
3031
public $supportedAlgs = [
3132
'HS256' => 'Lcobucci\JWT\Signer\Hmac\Sha256',
3233
'HS384' => 'Lcobucci\JWT\Signer\Hmac\Sha384',
3334
'HS512' => 'Lcobucci\JWT\Signer\Hmac\Sha512',
35+
'ES256' => 'Lcobucci\JWT\Signer\Ecdsa\Sha256',
36+
'ES384' => 'Lcobucci\JWT\Signer\Ecdsa\Sha384',
37+
'ES512' => 'Lcobucci\JWT\Signer\Ecdsa\Sha512',
38+
'RS256' => 'Lcobucci\JWT\Signer\Rsa\Sha256',
39+
'RS384' => 'Lcobucci\JWT\Signer\Rsa\Sha384',
40+
'RS512' => 'Lcobucci\JWT\Signer\Rsa\Sha512',
3441
];
3542

3643
/**
37-
* @var string|array|null $key The key, or map of keys.
38-
* @todo Add RSA, ECDSA key file support
44+
* @var Key|string $key The key
3945
*/
4046
public $key;
4147

@@ -70,11 +76,12 @@ public function getValidationData($currentTime = null)
7076
* Parses the JWT and returns a token class
7177
* @param string $token JWT
7278
* @return Token|null
79+
* @throws \Throwable
7380
*/
7481
public function loadToken($token, $validate = true, $verify = true)
7582
{
7683
try {
77-
$token = $this->getParser()->parse((string)$token);
84+
$token = $this->getParser()->parse((string) $token);
7885
} catch (\RuntimeException $e) {
7986
Yii::warning("Invalid JWT provided: " . $e->getMessage(), 'jwt');
8087
return null;
@@ -111,15 +118,17 @@ public function validateToken(Token $token, $currentTime = null)
111118
* Validate token
112119
* @param Token $token token object
113120
* @return bool
121+
* @throws \Throwable
114122
*/
115123
public function verifyToken(Token $token)
116124
{
117125
$alg = $token->getHeader('alg');
118126

119127
if (empty($this->supportedAlgs[$alg])) {
120-
throw new InvalidParamException('Algorithm not supported');
128+
throw new InvalidArgumentException('Algorithm not supported');
121129
}
122130

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

125134
return $token->verify($signer, $this->key);

composer.json

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,32 @@
11
{
22
"name": "sizeg/yii2-jwt",
33
"description": "JWT based on Icobucci",
4+
"type": "yii2-extension",
5+
"keywords": ["yii2", "yii 2", "jwt"],
6+
"authors": [
7+
{
8+
"name": "Dmitriy Demin",
9+
"email": "sizemail@gmail.com",
10+
"homepage": "https://sizeg.tk"
11+
}
12+
],
413
"require": {
514
"php": ">=5.5.0",
615
"lcobucci/jwt": "~3.2.0",
7-
"yiisoft/yii2": "*"
16+
"yiisoft/yii2": "~2.0.0"
817
},
918
"require-dev": {
10-
"phpunit/phpunit": "^4.8",
11-
"doctrine/instantiator": "1.0.5",
12-
"phpdocumentor/reflection-docblock": "3.2.2"
19+
"phpunit/phpunit": "^4.8"
1320
},
1421
"autoload": {
1522
"psr-4": {
1623
"sizeg\\jwt\\": ""
1724
}
25+
},
26+
"autoload-dev": {
27+
"psr-4": {
28+
"sizeg\\jwt\\tests\\": "tests/"
29+
}
1830
},
1931
"repositories": [
2032
{

phpunit.xml.dist

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
<phpunit bootstrap="./tests/bootstrap.php">
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<phpunit bootstrap="./tests/bootstrap.php"
3+
colors="true"
4+
convertErrorsToExceptions="true"
5+
convertNoticesToExceptions="true"
6+
convertWarningsToExceptions="true"
7+
stopOnFailure="false">
28
<testsuites>
3-
<testsuite name="Hello World Test Suite">
4-
<directory>./tests/</directory>
9+
<testsuite name="Test Suite">
10+
<directory>./tests</directory>
511
</testsuite>
612
</testsuites>
7-
</phpunit>
13+
<filter>
14+
<whitelist>
15+
<directory suffix=".php">./</directory>
16+
</whitelist>
17+
</filter>
18+
</phpunit>

tests/JwtTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace jwttests;
3+
namespace sizeg\jwt\tests;
44

5-
class JwtTest extends \PHPUnit\Framework\TestCase
5+
class JwtTest extends TestCase
66
{
77

88
/**

tests/TestCase.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace sizeg\jwt\tests;
4+
5+
use yii\console\Application;
6+
7+
/**
8+
* Class TestCase
9+
* @author SiZE
10+
*/
11+
class TestCase extends \PHPUnit_Framework_TestCase
12+
{
13+
14+
/**
15+
* @inheritdoc
16+
*/
17+
protected function setUp()
18+
{
19+
parent::setUp();
20+
$this->mockApplication();
21+
}
22+
23+
/**
24+
* @inheritdoc
25+
*/
26+
protected function tearDown()
27+
{
28+
$this->destroyApplication();
29+
parent::tearDown();
30+
}
31+
32+
protected function mockApplication()
33+
{
34+
new Application([
35+
'id' => 'testapp',
36+
'basePath' => __DIR__,
37+
'vendorPath' => dirname(__DIR__) . '/vendor',
38+
'runtimePath' => __DIR__ . '/runtime',
39+
]);
40+
}
41+
42+
protected function destroyApplication()
43+
{
44+
\Yii::$app = null;
45+
}
46+
}

tests/bootstrap.php

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,7 @@
11
<?php
2-
3-
// ensure we get report on all possible php errors
4-
error_reporting(-1);
5-
6-
define('YII_ENABLE_ERROR_HANDLER', false);
7-
define('YII_DEBUG', true);
2+
3+
defined('YII_DEBUG') or define('YII_DEBUG', true);
84
defined('YII_ENV') or define('YII_ENV', 'test');
9-
10-
$_SERVER['SCRIPT_NAME'] = '/' . basename(__FILE__);
11-
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
12-
13-
require_once(__DIR__ . '/../vendor/autoload.php');
14-
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');
15-
16-
Yii::setAlias('@jwttests', __DIR__);
5+
6+
require(__DIR__ . '/../vendor/autoload.php');
7+
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

0 commit comments

Comments
 (0)