-
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.
Improve authorization and authentication mechanics (#43)
- Loading branch information
Showing
11 changed files
with
474 additions
and
17 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,10 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Firehed\API\Authentication; | ||
|
||
use RuntimeException; | ||
|
||
class Exception extends RuntimeException | ||
{ | ||
} |
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,20 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Firehed\API\Authentication; | ||
|
||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
interface ProviderInterface | ||
{ | ||
/** | ||
* Upon successful authentication, the provider MUST return a | ||
* ContainerInterface. It is RECOMMENDED that implementations make authn | ||
* data available with fully-qualified class names when possible. | ||
* | ||
* If authentication fails, the provider MUST throw | ||
* a Firehed\API\Authentication\Exception. | ||
*/ | ||
public function authenticate(ServerRequestInterface $request): ContainerInterface; | ||
} |
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,10 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Firehed\API\Authorization; | ||
|
||
use RuntimeException; | ||
|
||
class Exception extends RuntimeException | ||
{ | ||
} |
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,13 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Firehed\API\Authorization; | ||
|
||
/** | ||
* This class exists to loosely mimic a Result type, forcing ProviderInterface | ||
* implementations to affirmatively return a success state in order to reduce | ||
* the chance of accidentally failing "open". | ||
*/ | ||
class Ok | ||
{ | ||
} |
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,22 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Firehed\API\Authorization; | ||
|
||
use Firehed\API\Interfaces\AuthenticatedEndpointInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
interface ProviderInterface | ||
{ | ||
/** | ||
* Authorize the endpoint using the authentication data provided in the | ||
* container. Implementations MUST throw an Exception upon failure, and | ||
* MUST return an Ok upon success. | ||
* | ||
* @throws Exception | ||
*/ | ||
public function authorize( | ||
AuthenticatedEndpointInterface $endpoint, | ||
ContainerInterface $auth | ||
): Ok; | ||
} |
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,36 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Firehed\API; | ||
|
||
use Psr\Container as Psr; | ||
|
||
/** | ||
* Ultra simple array wrapper for a PSR container. No closures, no evaluation, | ||
* nothing else. Just a dumb-as-rocks key/value store. | ||
*/ | ||
class Container implements Psr\ContainerInterface | ||
{ | ||
/** @var array */ | ||
private $data; | ||
|
||
public function __construct(array $data) | ||
{ | ||
$this->data = $data; | ||
} | ||
|
||
public function has($id) | ||
{ | ||
return array_key_exists($id, $this->data); | ||
} | ||
|
||
public function get($id) | ||
{ | ||
if (!$this->has($id)) { | ||
throw new class extends \Exception implements Psr\NotFoundExceptionInterface | ||
{ | ||
}; | ||
} | ||
return $this->data[$id]; | ||
} | ||
} |
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 | ||
declare(strict_types=1); | ||
|
||
namespace Firehed\API\Interfaces; | ||
|
||
use Psr\Container\ContainerInterface; | ||
|
||
interface AuthenticatedEndpointInterface extends EndpointInterface | ||
{ | ||
public function setAuthentication(ContainerInterface $auth); | ||
} |
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,59 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Firehed\API; | ||
|
||
use Psr\Container as Psr; | ||
|
||
/** | ||
* @coversDefaultClass Firehed\API\Container | ||
* @covers ::<protected> | ||
* @covers ::<private> | ||
*/ | ||
class ContainerTest extends \PHPUnit\Framework\TestCase | ||
{ | ||
/** @var Container */ | ||
private $c; | ||
|
||
public function setUp() | ||
{ | ||
$this->c = new Container(['key' => 'value']); | ||
} | ||
|
||
/** @covers ::__construct */ | ||
public function testConstruct() | ||
{ | ||
$this->assertInstanceOf(Psr\ContainerInterface::class, $this->c); | ||
} | ||
|
||
/** @covers ::has */ | ||
public function testHas() | ||
{ | ||
$this->assertTrue($this->c->has('key')); | ||
$this->assertFalse($this->c->has('nokey')); | ||
} | ||
|
||
/** @covers ::get */ | ||
public function testGet() | ||
{ | ||
$this->assertSame('value', $this->c->get('key')); | ||
} | ||
|
||
/** @covers ::get */ | ||
public function testGetDoesNotEvaluateCallables() | ||
{ | ||
$loader = function () { | ||
return new Container([]); | ||
}; | ||
|
||
$container = new Container(['loader' => $loader]); | ||
$this->assertSame($loader, $container->get('loader')); | ||
} | ||
|
||
/** @covers ::get */ | ||
public function testGetThrowsOnMissingKey() | ||
{ | ||
$this->expectException(Psr\NotFoundExceptionInterface::class); | ||
$this->c->get('nokey'); | ||
} | ||
} |
Oops, something went wrong.