|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Active Collab Authentication project. |
| 5 | + * |
| 6 | + * (c) A51 doo <[email protected]>. All rights reserved. |
| 7 | + */ |
| 8 | + |
| 9 | +namespace ActiveCollab\Authentication\Test; |
| 10 | + |
| 11 | +use ActiveCollab\Authentication\Adapter\BrowserSessionAdapter; |
| 12 | +use ActiveCollab\Authentication\Adapter\TokenBearerAdapter; |
| 13 | +use ActiveCollab\Authentication\AuthenticatedUser\AuthenticatedUserInterface; |
| 14 | +use ActiveCollab\Authentication\AuthenticatedUser\RepositoryInterface; |
| 15 | +use ActiveCollab\Authentication\Authentication; |
| 16 | +use ActiveCollab\Authentication\Session\SessionInterface; |
| 17 | +use ActiveCollab\Authentication\Test\AuthenticatedUser\AuthenticatedUser; |
| 18 | +use ActiveCollab\Authentication\Test\AuthenticatedUser\Repository as UserRepository; |
| 19 | +use ActiveCollab\Authentication\Test\Session\Repository as SessionRepository; |
| 20 | +use ActiveCollab\Authentication\Test\Session\Session; |
| 21 | +use ActiveCollab\Authentication\Test\TestCase\RequestResponseTestCase; |
| 22 | +use ActiveCollab\Authentication\Test\Token\Repository as TokenRepository; |
| 23 | +use ActiveCollab\Authentication\Test\Token\Token; |
| 24 | +use ActiveCollab\Authentication\Token\TokenInterface; |
| 25 | +use ActiveCollab\Cookies\Adapter\Adapter; |
| 26 | +use ActiveCollab\Cookies\Cookies; |
| 27 | +use ActiveCollab\Cookies\CookiesInterface; |
| 28 | +use Psr\Http\Message\ResponseInterface; |
| 29 | +use Psr\Http\Message\ServerRequestInterface; |
| 30 | + |
| 31 | +/** |
| 32 | + * @package ActiveCollab\Authentication\Test |
| 33 | + */ |
| 34 | +class AuthenticationMiddlewareTest extends RequestResponseTestCase |
| 35 | +{ |
| 36 | + /** |
| 37 | + * @var CookiesInterface |
| 38 | + */ |
| 39 | + private $cookies; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var AuthenticatedUserInterface |
| 43 | + */ |
| 44 | + private $user; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var RepositoryInterface |
| 48 | + */ |
| 49 | + private $user_repository; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var \ActiveCollab\Authentication\Session\RepositoryInterface |
| 53 | + */ |
| 54 | + private $session_repository; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var string |
| 58 | + */ |
| 59 | + private $browser_session_cookie_name = 'test-session-cookie'; |
| 60 | + |
| 61 | + /** |
| 62 | + * @var BrowserSessionAdapter |
| 63 | + */ |
| 64 | + private $browser_session_adapter; |
| 65 | + |
| 66 | + /** |
| 67 | + * @var TokenRepository |
| 68 | + */ |
| 69 | + private $token_repository; |
| 70 | + |
| 71 | + /** |
| 72 | + * @var TokenBearerAdapter |
| 73 | + */ |
| 74 | + private $token_bearer_adapter; |
| 75 | + |
| 76 | + /** |
| 77 | + * {@inheritdoc} |
| 78 | + */ |
| 79 | + public function setUp() |
| 80 | + { |
| 81 | + parent::setUp(); |
| 82 | + |
| 83 | + $this->cookies = new Cookies(new Adapter()); |
| 84 | + |
| 85 | + $this-> user = new AuthenticatedUser( 1, '[email protected]', 'Ilija Studen', '123'); |
| 86 | + $this->user_repository = new UserRepository([ |
| 87 | + '[email protected]' => new AuthenticatedUser( 1, '[email protected]', 'Ilija Studen', '123'), |
| 88 | + ]); |
| 89 | + |
| 90 | + $this-> session_repository = new SessionRepository([ new Session( 'my-session-id', '[email protected]')]); |
| 91 | + $this->browser_session_adapter = new BrowserSessionAdapter($this->user_repository, $this->session_repository, $this->cookies, $this->browser_session_cookie_name); |
| 92 | + |
| 93 | + $this-> token_repository = new TokenRepository([ 'awesome-token' => new Token( 'awesome-token', '[email protected]')]); |
| 94 | + $this->token_bearer_adapter = new TokenBearerAdapter($this->user_repository, $this->token_repository); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * @expectedException \LogicException |
| 99 | + * @expectedExceptionMessage Invalid authentication adapter provided |
| 100 | + */ |
| 101 | + public function testExceptionIfInvalidAdaptersAreSet() |
| 102 | + { |
| 103 | + new Authentication([new \stdClass()]); |
| 104 | + } |
| 105 | + |
| 106 | + public function testMiddlewareAcceptsMultipleAdapters() |
| 107 | + { |
| 108 | + $middleware = new Authentication([$this->browser_session_adapter, $this->token_bearer_adapter]); |
| 109 | + |
| 110 | + $this->assertInternalType('array', $middleware->getAdapters()); |
| 111 | + $this->assertCount(2, $middleware->getAdapters()); |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Test that user is authenticated. |
| 116 | + */ |
| 117 | + public function testBrowserSessionAuthentication() |
| 118 | + { |
| 119 | + /** @var ServerRequestInterface $request */ |
| 120 | + /** @var ResponseInterface $response */ |
| 121 | + list($request, $response) = $this->cookies->set($this->request, $this->response, $this->browser_session_cookie_name, 'my-session-id'); |
| 122 | + |
| 123 | + $middleware = new Authentication([$this->browser_session_adapter]); |
| 124 | + |
| 125 | + /** @var ServerRequestInterface $modified_request */ |
| 126 | + $modified_request = null; |
| 127 | + |
| 128 | + /** @var ResponseInterface $response */ |
| 129 | + $response = call_user_func($middleware, $request, $response, function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) use (&$modified_request) { |
| 130 | + $modified_request = $request; |
| 131 | + |
| 132 | + if ($next) { |
| 133 | + $response = $next($request, $response); |
| 134 | + } |
| 135 | + |
| 136 | + return $response; |
| 137 | + }); |
| 138 | + |
| 139 | + $this->assertInstanceOf(ServerRequestInterface::class, $modified_request); |
| 140 | + $this->assertInstanceOf(ResponseInterface::class, $response); |
| 141 | + |
| 142 | + // Test if authentication attributes are set |
| 143 | + $this->assertArrayHasKey('authentication_adapter', $modified_request->getAttributes()); |
| 144 | + $this->assertArrayHasKey('authenticated_user', $modified_request->getAttributes()); |
| 145 | + $this->assertArrayHasKey('authenticated_with', $modified_request->getAttributes()); |
| 146 | + |
| 147 | + // Test if session cookie is set |
| 148 | + $set_cookie_header = $response->getHeaderLine('Set-Cookie'); |
| 149 | + |
| 150 | + $this->assertNotEmpty($set_cookie_header); |
| 151 | + $this->assertContains($this->browser_session_cookie_name, $set_cookie_header); |
| 152 | + $this->assertContains('my-session-id', $set_cookie_header); |
| 153 | + |
| 154 | + $this->assertInstanceOf(AuthenticatedUserInterface::class, $middleware->getAuthenticatedUser()); |
| 155 | + $this->assertInstanceOf(SessionInterface::class, $middleware->getAuthenticatedWith()); |
| 156 | + } |
| 157 | + |
| 158 | + public function testTokenBearerAuthentication() |
| 159 | + { |
| 160 | + /** @var ServerRequestInterface $request */ |
| 161 | + $request = $this->request->withHeader('Authorization', 'Bearer awesome-token'); |
| 162 | + |
| 163 | + $middleware = new Authentication([$this->token_bearer_adapter]); |
| 164 | + |
| 165 | + /** @var ServerRequestInterface $modified_request */ |
| 166 | + $modified_request = null; |
| 167 | + |
| 168 | + $response = call_user_func($middleware, $request, $this->response, function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) use (&$modified_request) { |
| 169 | + $modified_request = $request; |
| 170 | + |
| 171 | + if ($next) { |
| 172 | + $response = $next($request, $response); |
| 173 | + } |
| 174 | + |
| 175 | + return $response; |
| 176 | + }); |
| 177 | + |
| 178 | + $this->assertInstanceOf(ServerRequestInterface::class, $modified_request); |
| 179 | + $this->assertInstanceOf(ResponseInterface::class, $response); |
| 180 | + |
| 181 | + // Test if authentication attributes are set |
| 182 | + $this->assertArrayHasKey('authentication_adapter', $modified_request->getAttributes()); |
| 183 | + $this->assertArrayHasKey('authenticated_user', $modified_request->getAttributes()); |
| 184 | + $this->assertArrayHasKey('authenticated_with', $modified_request->getAttributes()); |
| 185 | + |
| 186 | + // Test if session cookie is set |
| 187 | + $set_cookie_header = $response->getHeaderLine('Set-Cookie'); |
| 188 | + $this->assertEmpty($set_cookie_header); |
| 189 | + |
| 190 | + $this->assertInstanceOf(AuthenticatedUserInterface::class, $middleware->getAuthenticatedUser()); |
| 191 | + $this->assertInstanceOf(TokenInterface::class, $middleware->getAuthenticatedWith()); |
| 192 | + } |
| 193 | + |
| 194 | + /** |
| 195 | + * @expectedException \ActiveCollab\Authentication\Exception\InvalidAuthenticationRequestException |
| 196 | + * @expectedExceptionMessage You can not be authenticated with more than one authentication method |
| 197 | + */ |
| 198 | + public function testExceptionOnMultipleIds() |
| 199 | + { |
| 200 | + /** @var ServerRequestInterface $request */ |
| 201 | + /** @var ResponseInterface $response */ |
| 202 | + list($request, $response) = $this->cookies->set($this->request, $this->response, $this->browser_session_cookie_name, 'my-session-id'); |
| 203 | + |
| 204 | + /** @var ServerRequestInterface $request */ |
| 205 | + $request = $request->withHeader('Authorization', 'Bearer awesome-token'); |
| 206 | + |
| 207 | + call_user_func(new Authentication([$this->browser_session_adapter, $this->token_bearer_adapter]), $request, $response); |
| 208 | + } |
| 209 | + |
| 210 | + public function testOnAuthenticatedUserCallback() |
| 211 | + { |
| 212 | + /** @var ServerRequestInterface $request */ |
| 213 | + $request = $this->request->withHeader('Authorization', 'Bearer awesome-token'); |
| 214 | + |
| 215 | + $middleware = new Authentication([$this->token_bearer_adapter]); |
| 216 | + |
| 217 | + $callback_is_called = false; |
| 218 | + $middleware->setOnAuthenciatedUserChanged(function () use (&$callback_is_called) { |
| 219 | + $callback_is_called = true; |
| 220 | + }); |
| 221 | + |
| 222 | + call_user_func($middleware, $request, $this->response); |
| 223 | + |
| 224 | + $this->assertTrue($callback_is_called); |
| 225 | + } |
| 226 | +} |
0 commit comments