Skip to content

Commit acfab17

Browse files
author
Greg Bowler
authored
Split host and path from Authenticator constructor (#3)
Closes #2
1 parent bc670d0 commit acfab17

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

src/Authenticator.php

+12-6
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@ class Authenticator {
99
const RESPONSE_QUERY_PARAMETER = "AUTHWAVE_RESPONSE_DATA";
1010

1111
private string $clientKey;
12-
private string $currentUriPath;
12+
private string $clientPath;
1313
private string $authwaveHost;
1414
private SessionContainer $session;
1515
private SessionData $sessionData;
1616
private RedirectHandler $redirectHandler;
17+
/**
18+
* @var string
19+
*/
20+
private string $clientHost;
1721

1822
public function __construct(
1923
string $clientKey,
20-
string $currentUriPath,
24+
string $clientHost,
25+
string $clientPath,
2126
string $authwaveHost = "login.authwave.com",
2227
SessionContainer $session = null,
2328
RedirectHandler $redirectHandler = null
@@ -33,7 +38,8 @@ public function __construct(
3338
}
3439

3540
$this->clientKey = $clientKey;
36-
$this->currentUriPath = $currentUriPath;
41+
$this->clientHost = $clientHost;
42+
$this->clientPath = $clientPath;
3743
$this->authwaveHost = $authwaveHost;
3844
$this->session = $session;
3945
$this->sessionData = $session->get(self::SESSION_KEY);
@@ -69,7 +75,7 @@ public function login(Token $token = null):void {
6975

7076
$loginUri = new AuthUri(
7177
$token,
72-
$this->currentUriPath,
78+
$this->clientPath,
7379
$this->authwaveHost
7480
);
7581
$this->redirectHandler->redirect($loginUri);
@@ -105,14 +111,14 @@ private function completeAuth():void {
105111
);
106112

107113
$this->redirectHandler->redirect(
108-
(new Uri($this->currentUriPath))
114+
(new Uri($this->clientPath))
109115
->withoutQueryValue(self::RESPONSE_QUERY_PARAMETER)
110116
);
111117
}
112118

113119
private function getResponseCipher():?string {
114120
$queryString = parse_url(
115-
$this->currentUriPath,
121+
$this->clientPath,
116122
PHP_URL_QUERY
117123
);
118124
if(!$queryString) {

test/phpunit/AuthenticatorTest.php

+26-4
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,20 @@
1616
class AuthenticatorTest extends TestCase {
1717
public function testConstructWithDefaultSessionNotStarted() {
1818
self::expectException(SessionNotStartedException::class);
19-
new Authenticator("test-key","/");
19+
new Authenticator(
20+
"test-key",
21+
"example.com",
22+
"/"
23+
);
2024
}
2125

2226
public function testConstructWithDefaultSession() {
2327
$_SESSION = [];
24-
new Authenticator("test-key", "/");
28+
new Authenticator(
29+
"test-key",
30+
"example.com",
31+
"/"
32+
);
2533
self::assertArrayHasKey(
2634
Authenticator::SESSION_KEY,
2735
$_SESSION
@@ -32,6 +40,7 @@ public function testIsLoggedInFalseByDefault() {
3240
$_SESSION = [];
3341
$sut = new Authenticator(
3442
"test-key",
43+
"example.com",
3544
"/"
3645
);
3746
self::assertFalse($sut->isLoggedIn());
@@ -50,7 +59,8 @@ public function testIsLoggedInTrueWhenSessionDataSet() {
5059

5160
$sut = new Authenticator(
5261
"test-key",
53-
"/",
62+
"example.com",
63+
"/"
5464
);
5565
self::assertTrue($sut->isLoggedIn());
5666
}
@@ -63,7 +73,8 @@ public function testLogoutClearsSession() {
6373

6474
$sut = new Authenticator(
6575
"test-key",
66-
"/",
76+
"example.com",
77+
"/"
6778
);
6879
$sut->logout();
6980
self::assertEmpty($_SESSION);
@@ -81,6 +92,7 @@ public function testLoginRedirects() {
8192

8293
$sut = new Authenticator(
8394
"test-key",
95+
"example.com",
8496
"/",
8597
AuthUri::DEFAULT_BASE_URI,
8698
null,
@@ -103,6 +115,7 @@ public function testLoginRedirectsLocalhost() {
103115

104116
$sut = new Authenticator(
105117
"test-key",
118+
"example.com",
106119
"/",
107120
"http://localhost:8081",
108121
null,
@@ -146,6 +159,7 @@ public function testLoginRedirectsWithCorrectQueryString() {
146159

147160
$sut = new Authenticator(
148161
$key,
162+
"example.com",
149163
$currentPath,
150164
AuthUri::DEFAULT_BASE_URI,
151165
null,
@@ -166,6 +180,7 @@ public function testLoginDoesNothingWhenAlreadyLoggedIn() {
166180

167181
$sut = new Authenticator(
168182
"test-key",
183+
"example.com",
169184
"/",
170185
AuthUri::DEFAULT_BASE_URI,
171186
null,
@@ -179,6 +194,7 @@ public function testGetUuidThrowsExceptionWhenNotLoggedIn() {
179194
$_SESSION = [];
180195
$sut = new Authenticator(
181196
"test-key",
197+
"example.com",
182198
"/"
183199
);
184200
self::expectException(NotLoggedInException::class);
@@ -200,6 +216,7 @@ public function testGetUuid() {
200216
];
201217
$sut = new Authenticator(
202218
"test-key",
219+
"example.com",
203220
"/"
204221
);
205222
self::assertEquals($expectedUuid, $sut->getUuid());
@@ -209,6 +226,7 @@ public function testGetEmailThrowsExceptionWhenNotLoggedIn() {
209226
$_SESSION = [];
210227
$sut = new Authenticator(
211228
"test-key",
229+
"example.com",
212230
"/"
213231
);
214232
self::expectException(NotLoggedInException::class);
@@ -230,6 +248,7 @@ public function testGetEmail() {
230248
];
231249
$sut = new Authenticator(
232250
"test-key",
251+
"example.com",
233252
"/"
234253
);
235254
self::assertEquals($expectedEmail, $sut->getEmail());
@@ -244,6 +263,7 @@ public function testCompleteAuthNotLoggedIn() {
244263
self::expectException(NotLoggedInException::class);
245264
new Authenticator(
246265
"test-key",
266+
"example.com",
247267
$currentUri
248268
);
249269
}
@@ -276,6 +296,7 @@ public function testCompleteAuth() {
276296
];
277297
new Authenticator(
278298
"test-key",
299+
"example.com",
279300
$currentUri,
280301
AuthUri::DEFAULT_BASE_URI,
281302
null,
@@ -303,6 +324,7 @@ public function testCompleteAuthNotAffectedByQueryString() {
303324

304325
new Authenticator(
305326
"test-key",
327+
"example.com",
306328
"/example-path?filter=something",
307329
AuthUri::DEFAULT_BASE_URI,
308330
null,

0 commit comments

Comments
 (0)