-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nikolay Gnatyo
committed
Aug 18, 2017
1 parent
eeacb44
commit 6dc8048
Showing
12 changed files
with
124 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace FreeElephants\Jwt; | ||
|
||
abstract class AbstractAdapterFactory | ||
{ | ||
|
||
abstract public function createDecoder(string $key, array $allowedAlgorithms): DecoderInterface; | ||
|
||
abstract public function createEncoder($key): EncoderInterface; | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
|
||
namespace FreeElephants\Jwt; | ||
|
||
|
||
interface EncoderInterface | ||
{ | ||
|
||
/** | ||
* @param array|object $token | ||
* @return string | ||
*/ | ||
public function encode($token, string $algorithm): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
|
||
namespace FreeElephants\Jwt\Firebase; | ||
|
||
|
||
use FreeElephants\Jwt\AbstractAdapterFactory; | ||
use FreeElephants\Jwt\DecoderInterface; | ||
use FreeElephants\Jwt\EncoderInterface; | ||
|
||
class FirebaseAdapterFactory extends AbstractAdapterFactory | ||
{ | ||
|
||
public function createDecoder(string $key, array $allowedAlgorithms): DecoderInterface | ||
{ | ||
return new FirebaseDecoderAdapter($key, $allowedAlgorithms); | ||
} | ||
|
||
public function createEncoder($key): EncoderInterface | ||
{ | ||
return new FirebaseEncoderAdapter($key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
|
||
namespace FreeElephants\Jwt\Firebase; | ||
|
||
|
||
use Firebase\JWT\JWT; | ||
use FreeElephants\Jwt\EncoderInterface; | ||
|
||
class FirebaseEncoderAdapter implements EncoderInterface | ||
{ | ||
|
||
/** @var string|array*/ | ||
private $key; | ||
|
||
public function __construct($key) | ||
{ | ||
$this->key = $key; | ||
} | ||
|
||
/** | ||
* @param array|object $token | ||
* @return string | ||
*/ | ||
public function encode($token, string $algorithm): string | ||
{ | ||
return Jwt::encode($token, $this->key, $algorithm); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
16 changes: 9 additions & 7 deletions
16
...eElephants/Jwt/FirebaseJwtAdapterTest.php → ...t/Firebase/FirebaseDecoderAdapterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,41 @@ | ||
<?php | ||
|
||
|
||
namespace FreeElephants\Jwt; | ||
namespace FreeElephants\Jwt\Firebase; | ||
|
||
|
||
use FreeElephants\Jwt\Exception\InvalidArgumentException; | ||
use FreeElephants\Jwt\Exception\OutOfBoundsException; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class FirebaseJwtAdapterTest extends TestCase | ||
class FirebaseDecoderAdapterTest extends TestCase | ||
{ | ||
|
||
const EXAMPLE_JWT = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoaWQiOiJqb2UiLCJhdXRocm9sZXMiOlsic3Vic2NyaWJlciJdfQ.Lxyy1H3gfs1FV5UJLGxfAYvS1TJeiJhVInu5GIlccg4'; | ||
|
||
public function testDecode() | ||
{ | ||
$decoder = new FirebaseJwtDecoder('example_key', ['HS256', 'HS384']); | ||
$decoder = new FirebaseDecoderAdapter('example_key', ['HS256', 'HS384']); | ||
|
||
$signature = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoaWQiOiJqb2UiLCJhdXRocm9sZXMiOlsic3Vic2NyaWJlciJdfQ.Lxyy1H3gfs1FV5UJLGxfAYvS1TJeiJhVInu5GIlccg4'; | ||
$expected = new \stdClass(); | ||
$expected->authid = 'joe'; | ||
$expected->authroles = [ | ||
'subscriber' | ||
]; | ||
|
||
$this->assertEquals($expected, $decoder->decode($signature)); | ||
$this->assertEquals($expected, $decoder->decode(self::EXAMPLE_JWT)); | ||
} | ||
|
||
public function testUseEmptyAllowedAlgorithmsListInvalidArgumentException() | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
new FirebaseJwtDecoder('example_key', []); | ||
new FirebaseDecoderAdapter('example_key', []); | ||
} | ||
|
||
public function testSetAllowedAlgorithmsOutOfBoundsException() | ||
{ | ||
$this->expectException(OutOfBoundsException::class); | ||
new FirebaseJwtDecoder('example_key', ['foo bar']); | ||
new FirebaseDecoderAdapter('example_key', ['foo bar']); | ||
} | ||
|
||
} |
19 changes: 19 additions & 0 deletions
19
tests/FreeElephants/Jwt/Firebase/FirebaseEncoderAdapterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
|
||
namespace FreeElephants\Jwt\Firebase; | ||
|
||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class FirebaseEncoderAdapterTest extends TestCase | ||
{ | ||
|
||
const EXAMPLE_JWT = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhdXRoaWQiOiJqb2UiLCJhdXRocm9sZXMiOlsic3Vic2NyaWJlciJdfQ.Lxyy1H3gfs1FV5UJLGxfAYvS1TJeiJhVInu5GIlccg4'; | ||
|
||
public function testEncode() | ||
{ | ||
$encoder = new FirebaseEncoderAdapter('example_key', ['HS256', 'HS384']); | ||
$this->assertSame(self::EXAMPLE_JWT, $encoder->encode(['authid' => 'joe', 'authroles' => ['subscriber']], 'HS256')); | ||
} | ||
} |