Skip to content

Commit 144d13b

Browse files
fbourigaultNyholm
authored andcommitted
add Header authentication method (#118)
1 parent 274c2c2 commit 144d13b

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
99

1010
## Unreleased
1111

12+
### Added
13+
14+
- New Header authentication method for arbitrary header authentication.
15+
1216
## [1.8.0] - 2019-08-05
1317

1418
### Changed

spec/Authentication/HeaderSpec.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Authentication;
4+
5+
use PhpSpec\ObjectBehavior;
6+
use Psr\Http\Message\RequestInterface;
7+
8+
class HeaderSpec extends ObjectBehavior
9+
{
10+
function let()
11+
{
12+
$this->beConstructedWith('X-AUTH-TOKEN', 'REAL');
13+
}
14+
15+
function it_is_initializable()
16+
{
17+
$this->shouldHaveType('Http\Message\Authentication\Header');
18+
}
19+
20+
function it_is_an_authentication()
21+
{
22+
$this->shouldImplement('Http\Message\Authentication');
23+
}
24+
25+
function it_authenticates_a_request(RequestInterface $request, RequestInterface $newRequest)
26+
{
27+
$request->withHeader('X-AUTH-TOKEN', 'REAL')->willReturn($newRequest);
28+
29+
$this->authenticate($request)->shouldReturn($newRequest);
30+
}
31+
}

src/Authentication/Header.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Http\Message\Authentication;
4+
5+
use Http\Message\Authentication;
6+
use Psr\Http\Message\RequestInterface;
7+
8+
class Header implements Authentication
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $name;
14+
15+
/**
16+
* @var string|array
17+
*/
18+
private $value;
19+
20+
public function __construct(string $name, $value)
21+
{
22+
$this->name = $name;
23+
$this->value = $value;
24+
}
25+
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function authenticate(RequestInterface $request)
30+
{
31+
return $request->withHeader($this->name, $this->value);
32+
}
33+
}

0 commit comments

Comments
 (0)