Skip to content

add Header authentication method #118

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 16, 2019
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## Unreleased

### Added

- New Header authentication method for arbitrary header authentication.

## [1.8.0] - 2019-08-05

### Changed
Expand Down
31 changes: 31 additions & 0 deletions spec/Authentication/HeaderSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace spec\Http\Message\Authentication;

use PhpSpec\ObjectBehavior;
use Psr\Http\Message\RequestInterface;

class HeaderSpec extends ObjectBehavior
{
function let()
{
$this->beConstructedWith('X-AUTH-TOKEN', 'REAL');
}

function it_is_initializable()
{
$this->shouldHaveType('Http\Message\Authentication\Header');
}

function it_is_an_authentication()
{
$this->shouldImplement('Http\Message\Authentication');
}

function it_authenticates_a_request(RequestInterface $request, RequestInterface $newRequest)
{
$request->withHeader('X-AUTH-TOKEN', 'REAL')->willReturn($newRequest);

$this->authenticate($request)->shouldReturn($newRequest);
}
}
33 changes: 33 additions & 0 deletions src/Authentication/Header.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Http\Message\Authentication;

use Http\Message\Authentication;
use Psr\Http\Message\RequestInterface;

class Header implements Authentication
{
/**
* @var string
*/
private $name;

/**
* @var string|array
*/
private $value;

public function __construct(string $name, $value)
{
$this->name = $name;
$this->value = $value;
}

/**
* {@inheritdoc}
*/
public function authenticate(RequestInterface $request)
{
return $request->withHeader($this->name, $this->value);
}
}