Skip to content

Commit 4a328bb

Browse files
committed
Introduce UserData class
1 parent a4029d1 commit 4a328bb

7 files changed

+95
-13
lines changed

src/Authenticator.php

+42-7
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
use Gt\Session\SessionContainer;
55

66
class Authenticator {
7-
const SESSION_KEY_AUTH_FLOW = "authwave-authflow";
8-
const SESSION_KEY_USER_DATA = "authwave-userdata";
7+
const SESSION_KEY = "AUTHWAVE_SESSION";
8+
9+
private string $clientKey;
10+
private string $clientSecret;
11+
private string $redirectPath;
12+
private SessionData $sessionData;
913

1014
public function __construct(
1115
string $clientKey,
@@ -17,11 +21,42 @@ public function __construct(
1721
$session = new GlobalSessionContainer();
1822
}
1923

20-
if(!$session->contains(GlobalSessionContainer::SESSION_KEY)) {
21-
$session->set(
22-
GlobalSessionContainer::SESSION_KEY,
23-
new SessionData()
24-
);
24+
if(!$session->contains(self::SESSION_KEY)) {
25+
$session->set(self::SESSION_KEY, new SessionData());
26+
}
27+
28+
$this->clientKey = $clientKey;
29+
$this->clientSecret = $clientSecret;
30+
$this->redirectPath = $redirectPath;
31+
$this->sessionData = $session->get(self::SESSION_KEY);
32+
33+
if($this->authInProgress()) {
34+
$this->completeAuth();
35+
}
36+
}
37+
38+
public function isLoggedIn():bool {
39+
$userData = null;
40+
41+
try {
42+
$userData = $this->sessionData->getUserData();
43+
}
44+
catch(UserDataNotSetException $exception) {
45+
return false;
2546
}
47+
48+
return isset($userData);
49+
}
50+
51+
private function authInProgress():bool {
52+
return false;
53+
}
54+
55+
private function beginAuth():void {
56+
57+
}
58+
59+
private function completeAuth():void {
60+
2661
}
2762
}

src/GlobalSessionContainer.php

-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
use Gt\Session\SessionArrayWrapper;
55

66
class GlobalSessionContainer extends SessionArrayWrapper {
7-
const SESSION_KEY = "AUTHWAVE_SESSION_DATA";
8-
97
public function __construct() {
108
if(!isset($_SESSION)) {
119
throw new SessionNotStartedException();

src/InitVectorNotSetException.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
namespace Authwave;
3+
4+
class InitVectorNotSetException extends AuthwaveException {}

src/SessionData.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
class SessionData {
55
private InitVector $iv;
66

7-
// TODO: Store User data here too.
8-
97
public function getIv():InitVector {
108
if(!isset($this->iv)) {
119
throw new InitVectorNotSetException();
@@ -21,4 +19,10 @@ public function setIv(InitVector $iv):void {
2119
public function removeIv():void {
2220
unset($this->iv);
2321
}
22+
23+
public function getUserData():UserData {
24+
if(!isset($this->userData)) {
25+
throw new UserDataNotSetException();
26+
}
27+
}
2428
}

src/UserData.php

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?php
2+
namespace Authwave;
3+
4+
class UserData {
5+
6+
}

src/UserDataNotSetException.php

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?php
2+
namespace Authwave;
3+
4+
class UserDataNotSetException extends AuthwaveException {}

test/phpunit/AuthenticatorTest.php

+33-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33

44
use Authwave\Authenticator;
55
use Authwave\GlobalSessionContainer;
6+
use Authwave\SessionData;
67
use Authwave\SessionNotStartedException;
8+
use Authwave\UserData;
79
use PHPUnit\Framework\TestCase;
810

911
class AuthenticatorTest extends TestCase {
@@ -18,14 +20,43 @@ public function testConstructWithDefaultSessionNotStarted() {
1820

1921
public function testConstructWithDefaultSession() {
2022
$_SESSION = [];
21-
$sut = new Authenticator(
23+
new Authenticator(
2224
"test-key",
2325
"test-secret",
2426
"/"
2527
);
2628
self::assertArrayHasKey(
27-
GlobalSessionContainer::SESSION_KEY,
29+
Authenticator::SESSION_KEY,
2830
$_SESSION
2931
);
3032
}
33+
34+
public function testIsLoggedInFalseByDefault() {
35+
$_SESSION = [];
36+
$sut = new Authenticator(
37+
"test-key",
38+
"test-secret",
39+
"/"
40+
);
41+
self::assertFalse($sut->isLoggedIn());
42+
}
43+
44+
public function testIsLoggedInTrueWhenSessionDataSet() {
45+
$userData = self::createMock(UserData::class);
46+
$sessionData = self::createMock(SessionData::class);
47+
$sessionData->expects(self::once())
48+
->method("getUserData")
49+
->willReturn($userData);
50+
51+
$_SESSION = [
52+
Authenticator::SESSION_KEY => $sessionData
53+
];
54+
55+
$sut = new Authenticator(
56+
"test-key",
57+
"test-secret",
58+
"/"
59+
);
60+
self::assertTrue($sut->isLoggedIn());
61+
}
3162
}

0 commit comments

Comments
 (0)