Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit e340d55

Browse files
committed
Merging release-1.0.0 to master in preparation for 0.3.0 release
2 parents edec9b1 + bd9c98e commit e340d55

7 files changed

+96
-50
lines changed

CHANGELOG.md

+36
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,42 @@
22

33
All notable changes to this project will be documented in this file, in reverse chronological order by release.
44

5+
## 0.3.0 - 2018-03-15
6+
7+
### Added
8+
9+
- Nothing.
10+
11+
### Changed
12+
13+
- Updates the zendframework/zend-expressive-authentication minimum supported
14+
version to 0.4.
15+
16+
- [#5](https://github.com/zendframework/zend-expressive-authentication-basic/pull/5)
17+
changes the constructor of the `Zend\Expressive\Authentication\Basic\BasicAccess`
18+
class to accept a callable `$responseFactory` instead of a
19+
`Psr\Http\Message\ResponseInterface` response prototype. The
20+
`$responseFactory` should produce a `ResponseInterface` implementation when
21+
invoked.
22+
23+
- [#5](https://github.com/zendframework/zend-expressive-authentication-basic/pull/5)
24+
updates the `BasicAccessFactory` to no longer use
25+
`Zend\Expressive\Authentication\ResponsePrototypeTrait`, and instead always
26+
depend on the `Psr\Http\Message\ResponseInterface` service to correctly return
27+
a PHP callable capable of producing a `ResponseInterface` instance.
28+
29+
### Deprecated
30+
31+
- Nothing.
32+
33+
### Removed
34+
35+
- Nothing.
36+
37+
### Fixed
38+
39+
- Nothing.
40+
541
## 0.2.0 - 2018-02-26
642

743
### Added

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"php": "^7.1",
2222
"psr/container": "^1.0",
2323
"psr/http-message": "^1.0.1",
24-
"zendframework/zend-expressive-authentication": "^1.0.0alpha3"
24+
"zendframework/zend-expressive-authentication": "^0.4.0 || ^1.0"
2525
},
2626
"require-dev": {
2727
"phpunit/phpunit": "^7.0.1",

composer.lock

+10-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/BasicAccess.php

+15-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication-basic for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication-basic/blob/master/LICENSE.md
66
* New BSD License
77
*/
@@ -14,6 +14,11 @@
1414
use Zend\Expressive\Authentication\UserInterface;
1515
use Zend\Expressive\Authentication\UserRepositoryInterface;
1616

17+
use function base64_decode;
18+
use function explode;
19+
use function preg_match;
20+
use function sprintf;
21+
1722
class BasicAccess implements AuthenticationInterface
1823
{
1924
/**
@@ -27,30 +32,24 @@ class BasicAccess implements AuthenticationInterface
2732
protected $realm;
2833

2934
/**
30-
* @var ResponseInterface
35+
* @var callable
3136
*/
32-
protected $responsePrototype;
37+
protected $responseFactory;
3338

34-
/**
35-
* Constructor
36-
*
37-
* @param UserRepositoryInterface $repository
38-
* @param string $realm
39-
* @param ResponseInterface $responsePrototype
40-
*/
4139
public function __construct(
4240
UserRepositoryInterface $repository,
4341
string $realm,
44-
ResponseInterface $responsePrototype
42+
callable $responseFactory
4543
) {
4644
$this->repository = $repository;
4745
$this->realm = $realm;
48-
$this->responsePrototype = $responsePrototype;
46+
47+
// Ensures type safety of the composed factory
48+
$this->responseFactory = function () use ($responseFactory) : ResponseInterface {
49+
return $responseFactory();
50+
};
4951
}
5052

51-
/**
52-
* {@inheritDoc}
53-
*/
5453
public function authenticate(ServerRequestInterface $request) : ?UserInterface
5554
{
5655
$authHeader = $request->getHeader('Authorization');
@@ -67,12 +66,9 @@ public function authenticate(ServerRequestInterface $request) : ?UserInterface
6766
return $this->repository->authenticate($username, $password);
6867
}
6968

70-
/**
71-
* {@inheritDoc}
72-
*/
7369
public function unauthorizedResponse(ServerRequestInterface $request) : ResponseInterface
7470
{
75-
return $this->responsePrototype
71+
return ($this->responseFactory)()
7672
->withHeader(
7773
'WWW-Authenticate',
7874
sprintf('Basic realm="%s"', $this->realm)

src/BasicAccessFactory.php

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,20 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication-basic for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication-basic/blob/master/LICENSE.md
66
* New BSD License
77
*/
88

99
namespace Zend\Expressive\Authentication\Basic;
1010

1111
use Psr\Container\ContainerInterface;
12+
use Psr\Http\Message\ResponseInterface;
1213
use Zend\Expressive\Authentication\Exception;
13-
use Zend\Expressive\Authentication\ResponsePrototypeTrait;
1414
use Zend\Expressive\Authentication\UserRepositoryInterface;
1515

1616
class BasicAccessFactory
1717
{
18-
use ResponsePrototypeTrait;
19-
2018
public function __invoke(ContainerInterface $container) : BasicAccess
2119
{
2220
$userRegister = $container->has(UserRepositoryInterface::class)
@@ -40,7 +38,7 @@ public function __invoke(ContainerInterface $container) : BasicAccess
4038
return new BasicAccess(
4139
$userRegister,
4240
$realm,
43-
$this->getResponsePrototype($container)
41+
$container->get(ResponseInterface::class)
4442
);
4543
}
4644
}

test/BasicAccessFactoryTest.php

+17-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication-basic for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication-basic/blob/master/LICENSE.md
66
* New BSD License
77
*/
88

99
namespace ZendTest\Expressive\Authentication\Basic;
1010

11+
use PHPUnit\Framework\Assert;
1112
use PHPUnit\Framework\TestCase;
12-
use Prophecy\Argument;
1313
use Prophecy\Prophecy\ObjectProphecy;
1414
use Psr\Container\ContainerInterface;
1515
use Psr\Http\Message\ResponseInterface;
16+
use ReflectionProperty;
1617
use Zend\Expressive\Authentication\Basic\BasicAccess;
1718
use Zend\Expressive\Authentication\Basic\BasicAccessFactory;
1819
use Zend\Expressive\Authentication\Exception\InvalidConfigException;
@@ -29,6 +30,9 @@ class BasicAccessFactoryTest extends TestCase
2930
/** @var UserRepositoryInterface|ObjectProphecy */
3031
private $userRegister;
3132

33+
/** @var ResponseInterface|ObjectProphecy */
34+
private $responsePrototype;
35+
3236
/** @var callback */
3337
private $responseFactory;
3438

@@ -37,8 +41,9 @@ protected function setUp()
3741
$this->container = $this->prophesize(ContainerInterface::class);
3842
$this->factory = new BasicAccessFactory();
3943
$this->userRegister = $this->prophesize(UserRepositoryInterface::class);
44+
$this->responsePrototype = $this->prophesize(ResponseInterface::class);
4045
$this->responseFactory = function () {
41-
return $this->prophesize(ResponseInterface::class)->reveal();
46+
return $this->responsePrototype->reveal();
4247
};
4348
}
4449

@@ -92,5 +97,14 @@ public function testInvokeWithContainerAndConfig()
9297

9398
$basicAccess = ($this->factory)($this->container->reveal());
9499
$this->assertInstanceOf(BasicAccess::class, $basicAccess);
100+
$this->assertResponseFactoryReturns($this->responsePrototype->reveal(), $basicAccess);
101+
}
102+
103+
public static function assertResponseFactoryReturns(ResponseInterface $expected, BasicAccess $service) : void
104+
{
105+
$r = new ReflectionProperty($service, 'responseFactory');
106+
$r->setAccessible(true);
107+
$responseFactory = $r->getValue($service);
108+
Assert::assertSame($expected, $responseFactory());
95109
}
96110
}

test/BasicAccessTest.php

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/**
33
* @see https://github.com/zendframework/zend-expressive-authentication-basic for the canonical source repository
4-
* @copyright Copyright (c) 2017 Zend Technologies USA Inc. (http://www.zend.com)
4+
* @copyright Copyright (c) 2017-2018 Zend Technologies USA Inc. (http://www.zend.com)
55
* @license https://github.com/zendframework/zend-expressive-authentication-basic/blob/master/LICENSE.md
66
* New BSD License
77
*/
@@ -11,8 +11,8 @@
1111
use PHPUnit\Framework\TestCase;
1212
use Prophecy\Argument;
1313
use Prophecy\Prophecy\ObjectProphecy;
14-
use Psr\Http\Message\ServerRequestInterface;
1514
use Psr\Http\Message\ResponseInterface;
15+
use Psr\Http\Message\ServerRequestInterface;
1616
use Zend\Expressive\Authentication\AuthenticationInterface;
1717
use Zend\Expressive\Authentication\Basic\BasicAccess;
1818
use Zend\Expressive\Authentication\UserInterface;
@@ -32,20 +32,26 @@ class BasicAccessTest extends TestCase
3232
/** @var ResponseInterface|ObjectProphecy */
3333
private $responsePrototype;
3434

35+
/** @var callable */
36+
private $responseFactory;
37+
3538
protected function setUp()
3639
{
3740
$this->request = $this->prophesize(ServerRequestInterface::class);
3841
$this->userRepository = $this->prophesize(UserRepositoryInterface::class);
3942
$this->authenticatedUser = $this->prophesize(UserInterface::class);
4043
$this->responsePrototype = $this->prophesize(ResponseInterface::class);
44+
$this->responseFactory = function () {
45+
return $this->responsePrototype->reveal();
46+
};
4147
}
4248

4349
public function testConstructor()
4450
{
4551
$basicAccess = new BasicAccess(
4652
$this->userRepository->reveal(),
4753
'test',
48-
$this->responsePrototype->reveal()
54+
$this->responseFactory
4955
);
5056
$this->assertInstanceOf(AuthenticationInterface::class, $basicAccess);
5157
}
@@ -59,7 +65,7 @@ public function testIsAuthenticatedWithoutHeader()
5965
$basicAccess = new BasicAccess(
6066
$this->userRepository->reveal(),
6167
'test',
62-
$this->responsePrototype->reveal()
68+
$this->responseFactory
6369
);
6470
$this->assertNull($basicAccess->authenticate($this->request->reveal()));
6571
}
@@ -73,7 +79,7 @@ public function testIsAuthenticatedWithoutBasic()
7379
$basicAccess = new BasicAccess(
7480
$this->userRepository->reveal(),
7581
'test',
76-
$this->responsePrototype->reveal()
82+
$this->responseFactory
7783
);
7884

7985
$this->assertNull($basicAccess->authenticate($this->request->reveal()));
@@ -98,7 +104,7 @@ public function testIsAuthenticatedWithValidCredential()
98104
$basicAccess = new BasicAccess(
99105
$this->userRepository->reveal(),
100106
'test',
101-
$this->responsePrototype->reveal()
107+
$this->responseFactory
102108
);
103109

104110
$user = $basicAccess->authenticate($this->request->reveal());
@@ -119,7 +125,7 @@ public function testIsAuthenticatedWithNoCredential()
119125
$basicAccess = new BasicAccess(
120126
$this->userRepository->reveal(),
121127
'test',
122-
$this->responsePrototype->reveal()
128+
$this->responseFactory
123129
);
124130

125131
$this->assertNull($basicAccess->authenticate($this->request->reveal()));
@@ -140,7 +146,7 @@ public function testGetUnauthenticatedResponse()
140146
$basicAccess = new BasicAccess(
141147
$this->userRepository->reveal(),
142148
'test',
143-
$this->responsePrototype->reveal()
149+
$this->responseFactory
144150
);
145151

146152
$response = $basicAccess->unauthorizedResponse($this->request->reveal());

0 commit comments

Comments
 (0)