From c85f7602dc607dbcb1950dd8809c0defc92040e6 Mon Sep 17 00:00:00 2001 From: Enrico Zimuel Date: Fri, 28 Sep 2018 10:25:39 +0200 Subject: [PATCH 1/2] Updated CHANGELOG for 1.0.1 --- CHANGELOG.md | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ac4e71a..76dc58b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,29 @@ All notable changes to this project will be documented in this file, in reverse chronological order by release. -## 1.0.1 - TBD +## 1.0.2 - TBD + +### Added + +- Nothing. + +### Changed + +- Nothing. + +### Deprecated + +- Nothing. + +### Removed + +- Nothing. + +### Fixed + +- Nothing. + +## 1.0.1 - 2018-09-28 ### Added From 2c725d36f0caa558d1dccc9198af58243a4c0914 Mon Sep 17 00:00:00 2001 From: Enrico Zimuel Date: Tue, 13 Nov 2018 15:49:16 +0100 Subject: [PATCH 2/2] Added IdentityInterface to manage user and client identity scenarios --- src/AuthenticationInterface.php | 4 ++-- src/AuthenticationMiddleware.php | 9 ++++++--- src/IdentityInterface.php | 18 ++++++++++++++++++ src/UserInterface.php | 7 +------ test/AuthenticationMiddlewareTest.php | 27 ++++++++++++++++++++++++--- 5 files changed, 51 insertions(+), 14 deletions(-) create mode 100644 src/IdentityInterface.php diff --git a/src/AuthenticationInterface.php b/src/AuthenticationInterface.php index 680140b..fef41be 100644 --- a/src/AuthenticationInterface.php +++ b/src/AuthenticationInterface.php @@ -15,10 +15,10 @@ interface AuthenticationInterface { /** - * Authenticate the PSR-7 request and return a valid user + * Authenticate the PSR-7 request and return a valid identity * or null if not authenticated */ - public function authenticate(ServerRequestInterface $request) : ?UserInterface; + public function authenticate(ServerRequestInterface $request) : ?IdentityInterface; /** * Generate the unauthorized response diff --git a/src/AuthenticationMiddleware.php b/src/AuthenticationMiddleware.php index 37dee31..be1bec8 100644 --- a/src/AuthenticationMiddleware.php +++ b/src/AuthenticationMiddleware.php @@ -31,9 +31,12 @@ public function __construct(AuthenticationInterface $auth) */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface { - $user = $this->auth->authenticate($request); - if (null !== $user) { - return $handler->handle($request->withAttribute(UserInterface::class, $user)); + $identity = $this->auth->authenticate($request); + if (null !== $identity) { + if ($identity instanceof UserInterface) { + return $handler->handle($request->withAttribute(UserInterface::class, $identity)); + } + return $handler->handle($request->withAttribute(IdentityInterface::class, $identity)); } return $this->auth->unauthorizedResponse($request); } diff --git a/src/IdentityInterface.php b/src/IdentityInterface.php new file mode 100644 index 0000000..d3fc6bf --- /dev/null +++ b/src/IdentityInterface.php @@ -0,0 +1,18 @@ +authentication = $this->prophesize(AuthenticationInterface::class); $this->request = $this->prophesize(ServerRequestInterface::class); - $this->authenticatedUser = $this->prophesize(UserInterface::class); + $this->user = $this->prophesize(UserInterface::class); + $this->identity = $this->prophesize(IdentityInterface::class); $this->handler = $this->prophesize(RequestHandlerInterface::class); } @@ -59,10 +61,29 @@ public function testProcessWithAuthenticatedUser() { $response = $this->prophesize(ResponseInterface::class); - $this->request->withAttribute(UserInterface::class, $this->authenticatedUser->reveal()) + $this->request->withAttribute(UserInterface::class, $this->user->reveal()) ->willReturn($this->request->reveal()); $this->authentication->authenticate($this->request->reveal()) - ->willReturn($this->authenticatedUser->reveal()); + ->willReturn($this->user->reveal()); + $this->handler->handle($this->request->reveal()) + ->willReturn($response->reveal()); + + $middleware = new AuthenticationMiddleware($this->authentication->reveal()); + $result = $middleware->process($this->request->reveal(), $this->handler->reveal()); + + $this->assertInstanceOf(ResponseInterface::class, $result); + $this->assertEquals($response->reveal(), $result); + $this->handler->handle($this->request->reveal())->shouldBeCalled(); + } + + public function testProcessWithAuthenticatedIdentity() + { + $response = $this->prophesize(ResponseInterface::class); + + $this->request->withAttribute(IdentityInterface::class, $this->identity->reveal()) + ->willReturn($this->request->reveal()); + $this->authentication->authenticate($this->request->reveal()) + ->willReturn($this->identity->reveal()); $this->handler->handle($this->request->reveal()) ->willReturn($response->reveal());