Skip to content

Commit fe9938e

Browse files
committed
Merge pull request #35 from php-http/match_auth
Add new RequestMatch authentication method
2 parents a3b45d5 + f7d3a21 commit fe9938e

File tree

4 files changed

+109
-8
lines changed

4 files changed

+109
-8
lines changed

CHANGELOG.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
## Unreleased
44

5+
### Added
6+
7+
- New RequestConditional authentication method using request matchers (deprecates Matching auth method)
58

6-
## 1.1.0
9+
10+
## 1.1.0 - 2016-02-25
711

812
### Added
913

@@ -13,7 +17,8 @@
1317

1418
### Fixed
1519

16-
- Fix casting string on a FilteredStream not filtering the output
20+
- Fix casting string on a FilteredStream not filtering the output
21+
1722

1823
## 1.0.0 - 2016-01-27
1924

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace spec\Http\Message\Authentication;
4+
5+
use Http\Message\Authentication;
6+
use Http\Message\RequestMatcher;
7+
use Psr\Http\Message\RequestInterface;
8+
use PhpSpec\ObjectBehavior;
9+
10+
class RequestConditionalSpec extends ObjectBehavior
11+
{
12+
use AuthenticationBehavior;
13+
14+
function let(RequestMatcher $requestMatcher, Authentication $authentication)
15+
{
16+
$this->beConstructedWith($requestMatcher, $authentication);
17+
}
18+
19+
function it_is_initializable()
20+
{
21+
$this->shouldHaveType('Http\Message\Authentication\RequestConditional');
22+
}
23+
24+
function it_authenticates_a_request(
25+
Authentication $authentication,
26+
RequestMatcher $requestMatcher,
27+
RequestInterface $request,
28+
RequestInterface $newRequest
29+
) {
30+
$requestMatcher->matches($request)->willReturn(true);
31+
$authentication->authenticate($request)->willReturn($newRequest);
32+
33+
$this->authenticate($request)->shouldReturn($newRequest);
34+
}
35+
36+
function it_does_not_authenticate_a_request(
37+
Authentication $authentication,
38+
RequestMatcher $requestMatcher,
39+
RequestInterface $request
40+
) {
41+
$requestMatcher->matches($request)->willReturn(false);
42+
$authentication->authenticate($request)->shouldNotBeCalled();
43+
44+
$this->authenticate($request)->shouldReturn($request);
45+
}
46+
}

src/Authentication/Matching.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@
33
namespace Http\Message\Authentication;
44

55
use Http\Message\Authentication;
6+
use Http\Message\RequestMatcher\CallbackRequestMatcher;
67
use Psr\Http\Message\RequestInterface;
78

89
/**
910
* Authenticate a PSR-7 Request if the request is matching.
1011
*
1112
* @author Márk Sági-Kazár <[email protected]>
13+
*
14+
* @deprecated since since version 1.1, to be removed in 2.0. Use {@link RequestConditional} instead.
1215
*/
1316
final class Matching implements Authentication
1417
{
@@ -18,7 +21,7 @@ final class Matching implements Authentication
1821
private $authentication;
1922

2023
/**
21-
* @var callable
24+
* @var CallbackRequestMatcher
2225
*/
2326
private $matcher;
2427

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

3740
$this->authentication = $authentication;
38-
$this->matcher = $matcher;
41+
$this->matcher = new CallbackRequestMatcher($matcher);
3942
}
4043

4144
/**
4245
* {@inheritdoc}
4346
*/
4447
public function authenticate(RequestInterface $request)
4548
{
46-
if (!call_user_func($this->matcher, $request)) {
47-
return $request;
49+
if ($this->matcher->matches($request)) {
50+
return $this->authentication->authenticate($request);
4851
}
4952

50-
return $this->authentication->authenticate($request);
53+
return $request;
5154
}
5255

5356
/**
@@ -60,7 +63,7 @@ public function authenticate(RequestInterface $request)
6063
*/
6164
public static function createUrlMatcher(Authentication $authentication, $url)
6265
{
63-
$matcher = function ($request) use ($url) {
66+
$matcher = function (RequestInterface $request) use ($url) {
6467
return preg_match($url, $request->getRequestTarget());
6568
};
6669

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Http\Message\Authentication;
4+
5+
use Http\Message\Authentication;
6+
use Http\Message\RequestMatcher;
7+
use Psr\Http\Message\RequestInterface;
8+
9+
/**
10+
* Authenticate a PSR-7 Request if the request is matching the given request matcher.
11+
*
12+
* @author Márk Sági-Kazár <[email protected]>
13+
*/
14+
final class RequestConditional implements Authentication
15+
{
16+
/**
17+
* @var RequestMatcher
18+
*/
19+
private $requestMatcher;
20+
21+
/**
22+
* @var Authentication
23+
*/
24+
private $authentication;
25+
26+
/**
27+
* @param RequestMatcher $requestMatcher
28+
* @param Authentication $authentication
29+
*/
30+
public function __construct(RequestMatcher $requestMatcher, Authentication $authentication)
31+
{
32+
$this->requestMatcher = $requestMatcher;
33+
$this->authentication = $authentication;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function authenticate(RequestInterface $request)
40+
{
41+
if ($this->requestMatcher->matches($request)) {
42+
return $this->authentication->authenticate($request);
43+
}
44+
45+
return $request;
46+
}
47+
}

0 commit comments

Comments
 (0)