diff --git a/CHANGELOG.md b/CHANGELOG.md index 7278609..7133d85 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/spec/Authentication/RequestConditionalSpec.php b/spec/Authentication/RequestConditionalSpec.php new file mode 100644 index 0000000..7209b7e --- /dev/null +++ b/spec/Authentication/RequestConditionalSpec.php @@ -0,0 +1,46 @@ +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); + } +} diff --git a/src/Authentication/Matching.php b/src/Authentication/Matching.php index 87373c1..2fcea3d 100644 --- a/src/Authentication/Matching.php +++ b/src/Authentication/Matching.php @@ -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 + * + * @deprecated since since version 1.1, to be removed in 2.0. Use {@link RequestConditional} instead. */ final class Matching implements Authentication { @@ -18,7 +21,7 @@ final class Matching implements Authentication private $authentication; /** - * @var callable + * @var CallbackRequestMatcher */ private $matcher; @@ -35,7 +38,7 @@ public function __construct(Authentication $authentication, callable $matcher = } $this->authentication = $authentication; - $this->matcher = $matcher; + $this->matcher = new CallbackRequestMatcher($matcher); } /** @@ -43,11 +46,11 @@ public function __construct(Authentication $authentication, callable $matcher = */ 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; } /** @@ -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()); }; diff --git a/src/Authentication/RequestConditional.php b/src/Authentication/RequestConditional.php new file mode 100644 index 0000000..5477440 --- /dev/null +++ b/src/Authentication/RequestConditional.php @@ -0,0 +1,47 @@ + + */ +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; + } +}