Skip to content

Commit 57abac9

Browse files
author
Greg Bowler
committed
Implement getting uuid from Authenticator instance
1 parent e230f90 commit 57abac9

File tree

6 files changed

+66
-11
lines changed

6 files changed

+66
-11
lines changed

src/Authenticator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function isLoggedIn():bool {
4949
try {
5050
$userData = $this->sessionData->getUserData();
5151
}
52-
catch(UserDataNotSetException $exception) {
52+
catch(NotLoggedInException $exception) {
5353
return false;
5454
}
5555

@@ -78,6 +78,11 @@ public function logout():void {
7878
$this->session->remove(self::SESSION_KEY);
7979
}
8080

81+
public function getUuid():string {
82+
$userData = $this->sessionData->getUserData();
83+
return $userData->getUuid();
84+
}
85+
8186
private function authInProgress():bool {
8287
return false;
8388
}

src/NotLoggedInException.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
namespace Authwave;
3+
4+
class NotLoggedInException extends AuthwaveException {}

src/SessionData.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public function removeIv():void {
2222

2323
public function getUserData():UserData {
2424
if(!isset($this->userData)) {
25-
throw new UserDataNotSetException();
25+
throw new NotLoggedInException();
2626
}
27+
28+
return $this->getUserData();
2729
}
2830
}

src/UserData.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@
22
namespace Authwave;
33

44
class UserData {
5+
private string $uuid;
56

7+
public function getUuid():string {
8+
return $this->uuid;
9+
}
610
}

src/UserDataNotSetException.php

Lines changed: 0 additions & 4 deletions
This file was deleted.

test/phpunit/AuthenticatorTest.php

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,32 @@
44
use Authwave\Authenticator;
55
use Authwave\AuthUri;
66
use Authwave\InitVector;
7+
use Authwave\NotLoggedInException;
78
use Authwave\RedirectHandler;
89
use Authwave\SessionData;
910
use Authwave\SessionNotStartedException;
1011
use Authwave\Token;
1112
use Authwave\UserData;
13+
use Gt\Session\Session;
1214
use PHPUnit\Framework\TestCase;
1315
use Psr\Http\Message\UriInterface;
1416

1517
class AuthenticatorTest extends TestCase {
1618
public function testConstructWithDefaultSessionNotStarted() {
1719
self::expectException(SessionNotStartedException::class);
1820
new Authenticator(
19-
"test-key", "test-secret", "/",
21+
"test-key",
22+
"test-secret",
23+
"/",
2024
);
2125
}
2226

2327
public function testConstructWithDefaultSession() {
2428
$_SESSION = [];
2529
new Authenticator(
26-
"test-key", "test-secret", "/",
30+
"test-key",
31+
"test-secret",
32+
"/",
2733
);
2834
self::assertArrayHasKey(
2935
Authenticator::SESSION_KEY,
@@ -34,7 +40,9 @@ public function testConstructWithDefaultSession() {
3440
public function testIsLoggedInFalseByDefault() {
3541
$_SESSION = [];
3642
$sut = new Authenticator(
37-
"test-key", "test-secret", "/",
43+
"test-key",
44+
"test-secret",
45+
"/",
3846
);
3947
self::assertFalse($sut->isLoggedIn());
4048
}
@@ -51,7 +59,9 @@ public function testIsLoggedInTrueWhenSessionDataSet() {
5159
];
5260

5361
$sut = new Authenticator(
54-
"test-key", "test-secret", "/",
62+
"test-key",
63+
"test-secret",
64+
"/",
5565
);
5666
self::assertTrue($sut->isLoggedIn());
5767
}
@@ -63,7 +73,9 @@ public function testLogoutClearsSession() {
6373
];
6474

6575
$sut = new Authenticator(
66-
"test-key", "test-secret", "/",
76+
"test-key",
77+
"test-secret",
78+
"/",
6779
);
6880
$sut->logout();
6981
self::assertEmpty($_SESSION);
@@ -157,4 +169,36 @@ public function testLoginRedirectsWithCorrectQueryString() {
157169
);
158170
$sut->login($token);
159171
}
172+
173+
public function testGetUuidThrowsExceptionWhenNotLoggedIn() {
174+
$_SESSION = [];
175+
$sut = new Authenticator(
176+
"test-key",
177+
"test-secret",
178+
"/"
179+
);
180+
self::expectException(NotLoggedInException::class);
181+
$sut->getUuid();
182+
}
183+
184+
public function testGetUuid() {
185+
$expectedUuid = uniqid("example-uuid-");
186+
187+
$userData = self::createMock(UserData::class);
188+
$userData->method("getUuid")
189+
->willReturn($expectedUuid);
190+
$sessionData = self::createMock(SessionData::class);
191+
$sessionData->method("getUserData")
192+
->willReturn($userData);
193+
194+
$_SESSION = [
195+
Authenticator::SESSION_KEY => $sessionData,
196+
];
197+
$sut = new Authenticator(
198+
"test-key",
199+
"test-secret",
200+
"/"
201+
);
202+
self::assertEquals($expectedUuid, $sut->getUuid());
203+
}
160204
}

0 commit comments

Comments
 (0)