Skip to content

Add new RequestMatch authentication method #35

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
Mar 4, 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
9 changes: 7 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

## Unreleased

### Added

- New RequestConditional authentication method using request matchers (deprecates Matching auth method)

## 1.1.0

## 1.1.0 - 2016-02-25

### Added

Expand All @@ -13,7 +17,8 @@

### Fixed

- Fix casting string on a FilteredStream not filtering the output
- Fix casting string on a FilteredStream not filtering the output


## 1.0.0 - 2016-01-27

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

namespace spec\Http\Message\Authentication;

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

class RequestConditionalSpec extends ObjectBehavior
{
use AuthenticationBehavior;

function let(RequestMatcher $requestMatcher, Authentication $authentication)
{
$this->beConstructedWith($requestMatcher, $authentication);
}

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

function it_authenticates_a_request(
Authentication $authentication,
RequestMatcher $requestMatcher,
RequestInterface $request,
RequestInterface $newRequest
) {
$requestMatcher->matches($request)->willReturn(true);
$authentication->authenticate($request)->willReturn($newRequest);

$this->authenticate($request)->shouldReturn($newRequest);
}

function it_does_not_authenticate_a_request(
Authentication $authentication,
RequestMatcher $requestMatcher,
RequestInterface $request
) {
$requestMatcher->matches($request)->willReturn(false);
$authentication->authenticate($request)->shouldNotBeCalled();

$this->authenticate($request)->shouldReturn($request);
}
}
15 changes: 9 additions & 6 deletions src/Authentication/Matching.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
namespace Http\Message\Authentication;

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

/**
* Authenticate a PSR-7 Request if the request is matching.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*
* @deprecated since since version 1.1, to be removed in 2.0. Use {@link RequestConditional} instead.
*/
final class Matching implements Authentication
{
Expand All @@ -18,7 +21,7 @@ final class Matching implements Authentication
private $authentication;

/**
* @var callable
* @var CallbackRequestMatcher
*/
private $matcher;

Expand All @@ -35,19 +38,19 @@ public function __construct(Authentication $authentication, callable $matcher =
}

$this->authentication = $authentication;
$this->matcher = $matcher;
$this->matcher = new CallbackRequestMatcher($matcher);
}

/**
* {@inheritdoc}
*/
public function authenticate(RequestInterface $request)
{
if (!call_user_func($this->matcher, $request)) {
return $request;
if ($this->matcher->matches($request)) {
return $this->authentication->authenticate($request);
}

return $this->authentication->authenticate($request);
return $request;
}

/**
Expand All @@ -60,7 +63,7 @@ public function authenticate(RequestInterface $request)
*/
public static function createUrlMatcher(Authentication $authentication, $url)
{
$matcher = function ($request) use ($url) {
$matcher = function (RequestInterface $request) use ($url) {
return preg_match($url, $request->getRequestTarget());
};

Expand Down
47 changes: 47 additions & 0 deletions src/Authentication/RequestConditional.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Http\Message\Authentication;

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

/**
* Authenticate a PSR-7 Request if the request is matching the given request matcher.
*
* @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
*/
final class RequestConditional implements Authentication
{
/**
* @var RequestMatcher
*/
private $requestMatcher;

/**
* @var Authentication
*/
private $authentication;

/**
* @param RequestMatcher $requestMatcher
* @param Authentication $authentication
*/
public function __construct(RequestMatcher $requestMatcher, Authentication $authentication)
{
$this->requestMatcher = $requestMatcher;
$this->authentication = $authentication;
}

/**
* {@inheritdoc}
*/
public function authenticate(RequestInterface $request)
{
if ($this->requestMatcher->matches($request)) {
return $this->authentication->authenticate($request);
}

return $request;
}
}