Skip to content

Commit

Permalink
Add encoder. Massive renaming.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolay Gnatyo committed Aug 18, 2017
1 parent eeacb44 commit 6dc8048
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 45 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.3.0] - 2017-08-18
### Changed
- New names for all items.

## [0.2.0] - 2017-08-18
### Changed
- Renamed: JwtDecoderAdapterInterface -> JwtDecoderInterface
Expand Down
11 changes: 11 additions & 0 deletions src/FreeElephants/Jwt/AbstractAdapterFactory.php
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;
}
9 changes: 0 additions & 9 deletions src/FreeElephants/Jwt/AbstractJwtDecoderFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
namespace FreeElephants\Jwt;


interface JwtDecoderInterface
interface DecoderInterface
{

public function getAlgorithms(): array;
Expand Down
15 changes: 15 additions & 0 deletions src/FreeElephants/Jwt/EncoderInterface.php
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;
}
23 changes: 23 additions & 0 deletions src/FreeElephants/Jwt/Firebase/FirebaseAdapterFactory.php
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
<?php


namespace FreeElephants\Jwt;
namespace FreeElephants\Jwt\Firebase;


use Firebase\JWT\JWT;
use FreeElephants\Jwt\Exception\InvalidArgumentException;
use FreeElephants\Jwt\Exception\OutOfBoundsException;
use FreeElephants\Jwt\DecoderInterface;
use FreeElephants\Jwt\EncoderInterface;

class FirebaseJwtDecoder implements JwtDecoderInterface
class FirebaseDecoderAdapter implements DecoderInterface, EncoderInterface
{

private $algorithms;
Expand Down Expand Up @@ -55,4 +57,13 @@ public function getSupportedAlgorithms(): array
'RS256'
];
}

/**
* @param array|object $token
* @return string
*/
public function encode($token, string $algorithm): string
{
return Jwt::encode($token, $this->key, $algorithm);
}
}
29 changes: 29 additions & 0 deletions src/FreeElephants/Jwt/Firebase/FirebaseEncoderAdapter.php
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);
}
}
14 changes: 0 additions & 14 deletions src/FreeElephants/Jwt/FirebaseJwtDecoderFactory.php

This file was deleted.

12 changes: 0 additions & 12 deletions src/FreeElephants/Jwt/JwtValidatorInterface.php

This file was deleted.

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 tests/FreeElephants/Jwt/Firebase/FirebaseEncoderAdapterTest.php
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'));
}
}

0 comments on commit 6dc8048

Please sign in to comment.