Skip to content
This repository has been archived by the owner on Jun 30, 2020. It is now read-only.

AttributeMapper middleware #22

Merged
merged 5 commits into from
Jan 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 45 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ $response = $dispatcher(ServerRequestFactory::fromGlobals(), new Response());
## Available middlewares

* [AccessLog](#accesslog)
* [AttributeMapper](#attributemapper)
* [AuraRouter](#aurarouter)
* [AuraSession](#aurasession)
* [BasePath](#basepath)
Expand Down Expand Up @@ -193,6 +194,36 @@ $dispatcher = $relay->getInstance([
]);
```


### AttributeMapper

Maps middleware specific attribute to regural request attribute under desired name:

```php
use Psr7Middlewares\Middleware;

$dispatcher = $relay->getInstance([

Middleware::BasicAuthentication([
'username1' => 'password1',
'username2' => 'password2'
])
->realm('My realm'), //(optional) change the realm value

Middleware::attributeMapper([
Middleware\BasicAuthentication::KEY => 'auth:username'
]),

function ($request, $response, $next) {
$username = BasicAuthentication::getUsername($request);

assert($username === $request->getAttribute('auth:username'));

return $next($request, $response);
}
]);
```

### AuraRouter

To use [Aura.Router (3.x)](https://github.com/auraphp/Aura.Router) as a middleware:
Expand Down Expand Up @@ -294,7 +325,13 @@ $dispatcher = $relay->getInstance([
'username1' => 'password1',
'username2' => 'password2'
])
->realm('My realm') //(optional) change the realm value
->realm('My realm'), //(optional) change the realm value

function ($request, $response, $next) {
$username = BasicAuthentication::getUsername($request);

return $next($request, $response);
}
]);
```

Expand Down Expand Up @@ -510,7 +547,13 @@ $dispatcher = $relay->getInstance([
'username2' => 'password2'
])
->realm('My realm') //(optional) custom realm value
->nonce(uniqid()) //(optional) custom nonce value
->nonce(uniqid()), //(optional) custom nonce value

function ($request, $response, $next) {
$username = DigestAuthentication::getUsername($request);

return $next($request, $response);
}
]);
```

Expand Down
57 changes: 57 additions & 0 deletions src/Middleware/AttributeMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
namespace Psr7Middlewares\Middleware;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr7Middlewares\Middleware;

/**
* Provides ability to route Psr7Middlewares specific attributes into scalar attributes.
*
* @todo Raise an exception if no attribute found?
*/
class AttributeMapper
{
/**
* @var array
*/
private $mapping = [];

/**
* Example:
*
* [
* BasicAuthentication::KEY => 'basic.username'
* ]
*
* @param array $mapping
*/
public function __construct(array $mapping)
{
$this->mapping = $mapping;
}

/**
* Execute the middleware.
*
* @param ServerRequestInterface $request
* @param ResponseInterface $response
* @param callable $next
*
* @return ResponseInterface
*/
public function __invoke(
ServerRequestInterface $request,
ResponseInterface $response,
callable $next
) {
foreach ($this->mapping as $middleware => $attribute) {
$request = $request->withAttribute(
$attribute,
Middleware::getAttribute($request, $middleware)
);
}

return $next($request, $response);
}
}
36 changes: 36 additions & 0 deletions tests/AttributeMapperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
use Psr7Middlewares\Middleware;

class AttributeMapperTest extends Base
{
public function testUsernameMiddlewareRouting()
{
$response = $this->execute(
[
Middleware::BasicAuthentication(['username' => 'password'])->realm('My realm'),

Middleware::attributeMapper([
Middleware\BasicAuthentication::KEY => 'auth:username'
]),

function ($request, $response, $next) {
$response->getBody()->write($request->getAttribute('auth:username'));

return $response;
}
],
'',
[
'Authorization' => $this->authHeader('username', 'password')
]
);

$this->assertSame(200, $response->getStatusCode());
$this->assertSame('username', (string)$response->getBody());
}

private function authHeader($username, $password)
{
return 'Basic ' . base64_encode("{$username}:{$password}");
}
}