|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/** |
| 4 | + * Test: Nette\Security\User guest identity. |
| 5 | + */ |
| 6 | + |
| 7 | +use Nette\Security\IIdentity; |
| 8 | +use Nette\Security\SimpleIdentity; |
| 9 | +use Nette\Security\User; |
| 10 | +use Tester\Assert; |
| 11 | + |
| 12 | + |
| 13 | +require __DIR__ . '/../bootstrap.php'; |
| 14 | +require __DIR__ . '/MockUserStorage.php'; |
| 15 | + |
| 16 | + |
| 17 | +class GuestAuthenticator implements Nette\Security\Authenticator, Nette\Security\IdentityHandler |
| 18 | +{ |
| 19 | + public function authenticate(string $username, string $password): IIdentity |
| 20 | + { |
| 21 | + return new SimpleIdentity('john', ['admin']); |
| 22 | + } |
| 23 | + |
| 24 | + |
| 25 | + public function sleepIdentity(IIdentity $identity): IIdentity |
| 26 | + { |
| 27 | + return $identity; |
| 28 | + } |
| 29 | + |
| 30 | + |
| 31 | + public function wakeupIdentity(IIdentity $identity): ?IIdentity |
| 32 | + { |
| 33 | + return $identity; |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + public function getGuestIdentity(): ?IIdentity |
| 38 | + { |
| 39 | + return new SimpleIdentity('guest', ['guest-role'], ['name' => 'Guest']); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | + |
| 44 | +class RecordingStorage implements Nette\Security\UserStorage |
| 45 | +{ |
| 46 | + /** @var list<IIdentity> */ |
| 47 | + public array $saved = []; |
| 48 | + private bool $auth = false; |
| 49 | + private ?IIdentity $identity = null; |
| 50 | + |
| 51 | + |
| 52 | + public function saveAuthentication(IIdentity $identity): void |
| 53 | + { |
| 54 | + $this->saved[] = $identity; |
| 55 | + $this->auth = true; |
| 56 | + $this->identity = $identity; |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | + public function clearAuthentication(bool $clearIdentity): void |
| 61 | + { |
| 62 | + $this->auth = false; |
| 63 | + $this->identity = $clearIdentity ? null : $this->identity; |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + public function getState(): array |
| 68 | + { |
| 69 | + return [$this->auth, $this->identity, null]; |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + public function setExpiration(?string $expire, bool $clearIdentity): void |
| 74 | + { |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | + |
| 79 | +test('guest identity is exposed when not logged in', function () { |
| 80 | + $user = new User(new MockUserStorage, new GuestAuthenticator); |
| 81 | + |
| 82 | + Assert::false($user->isLoggedIn()); |
| 83 | + Assert::equal(new SimpleIdentity('guest', ['guest-role'], ['name' => 'Guest']), $user->getIdentity()); |
| 84 | + Assert::same('guest', $user->getId()); |
| 85 | + Assert::same(['guest-role'], $user->getRoles()); |
| 86 | + Assert::true($user->isInRole('guest-role')); |
| 87 | + Assert::false($user->isInRole('admin')); |
| 88 | +}); |
| 89 | + |
| 90 | + |
| 91 | +test('login overrides the guest identity', function () { |
| 92 | + $user = new User(new MockUserStorage, new GuestAuthenticator); |
| 93 | + $user->login('john', 'pass'); |
| 94 | + |
| 95 | + Assert::true($user->isLoggedIn()); |
| 96 | + Assert::same('john', $user->getId()); |
| 97 | + Assert::same(['admin'], $user->getRoles()); |
| 98 | +}); |
| 99 | + |
| 100 | + |
| 101 | +test('guest identity returns after logout that clears identity', function () { |
| 102 | + $user = new User(new MockUserStorage, new GuestAuthenticator); |
| 103 | + $user->login('john', 'pass'); |
| 104 | + |
| 105 | + $user->logout(clearIdentity: true); |
| 106 | + |
| 107 | + Assert::false($user->isLoggedIn()); |
| 108 | + Assert::same('guest', $user->getId()); |
| 109 | + Assert::same(['guest-role'], $user->getRoles()); |
| 110 | +}); |
| 111 | + |
| 112 | + |
| 113 | +test('retained identity stays for personalization but roles fall back to the guest identity', function () { |
| 114 | + $user = new User(new MockUserStorage, new GuestAuthenticator); |
| 115 | + $user->login('john', 'pass'); |
| 116 | + |
| 117 | + $user->logout(); // persistIdentity is on by default -> identity is retained |
| 118 | + |
| 119 | + Assert::false($user->isLoggedIn()); |
| 120 | + Assert::same('john', $user->getId()); // retained real identity for personalization |
| 121 | + Assert::same(['guest-role'], $user->getRoles()); // but NOT the real ['admin'] roles |
| 122 | +}); |
| 123 | + |
| 124 | + |
| 125 | +test('without a guest identity provider the behaviour is unchanged', function () { |
| 126 | + $authenticator = new class implements Nette\Security\Authenticator { |
| 127 | + public function authenticate(string $username, string $password): IIdentity |
| 128 | + { |
| 129 | + return new SimpleIdentity('john', ['admin']); |
| 130 | + } |
| 131 | + }; |
| 132 | + $user = new User(new MockUserStorage, $authenticator); |
| 133 | + |
| 134 | + Assert::false($user->isLoggedIn()); |
| 135 | + Assert::null($user->getIdentity()); |
| 136 | + Assert::same(['guest'], $user->getRoles()); |
| 137 | +}); |
| 138 | + |
| 139 | + |
| 140 | +test('guest identity is never written to storage', function () { |
| 141 | + $storage = new RecordingStorage; |
| 142 | + $user = new User($storage, new GuestAuthenticator); |
| 143 | + |
| 144 | + // acting as a guest must not persist anything |
| 145 | + Assert::same('guest', $user->getId()); |
| 146 | + Assert::same(['guest-role'], $user->getRoles()); |
| 147 | + Assert::equal(new SimpleIdentity('guest', ['guest-role'], ['name' => 'Guest']), $user->getIdentity()); |
| 148 | + Assert::same([], $storage->saved); |
| 149 | + |
| 150 | + // login/logout cycle, then guest again |
| 151 | + $user->login('john', 'pass'); |
| 152 | + $user->logout(clearIdentity: true); |
| 153 | + Assert::same('guest', $user->getId()); |
| 154 | + |
| 155 | + // only the real identity may ever reach the storage |
| 156 | + Assert::same(['john'], array_map(fn(IIdentity $i) => $i->getId(), $storage->saved)); |
| 157 | +}); |
0 commit comments