Skip to content

Commit 5d61d33

Browse files
authored
Introduce App ID, closes #3 (#5)
1 parent bc670d0 commit 5d61d33

5 files changed

+84
-26
lines changed

src/AuthUri.php

+9-6
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
use Gt\Http\Uri;
55

66
class AuthUri extends Uri {
7-
const DEFAULT_BASE_URI = "login.authwave.com";
7+
const DEFAULT_BASE_REMOTE_URI = "login.authwave.com";
88

9+
const QUERY_STRING_ID = "id";
910
const QUERY_STRING_CIPHER = "cipher";
1011
const QUERY_STRING_INIT_VECTOR = "iv";
1112
const QUERY_STRING_CURRENT_PATH = "path";
@@ -14,20 +15,22 @@ class AuthUri extends Uri {
1415
* @param Token $token This must be the same instance of the Token when
1516
* creating Authenticator for the first time as it is when checking the
1617
* response from the Authwave provider (store in a session).
18+
* @param string $clientId
1719
* @param string $currentPath
18-
* @param string $baseUri The base URI of the application. This is the
20+
* @param string $baseRemoteUri The base URI of the application. This is the
1921
* URI authority with optional scheme, as localhost allows http://
2022
*/
2123
public function __construct(
2224
Token $token,
25+
string $clientId,
2326
string $currentPath = "/",
24-
string $baseUri = self::DEFAULT_BASE_URI
27+
string $baseRemoteUri = self::DEFAULT_BASE_REMOTE_URI
2528
) {
26-
$baseUri = $this->normaliseBaseUri($baseUri);
27-
28-
parent::__construct($baseUri);
29+
$baseRemoteUri = $this->normaliseBaseUri($baseRemoteUri);
30+
parent::__construct($baseRemoteUri);
2931

3032
$this->query = http_build_query([
33+
self::QUERY_STRING_ID => $clientId,
3134
self::QUERY_STRING_CIPHER => (string)$token->generateRequestCipher(),
3235
self::QUERY_STRING_INIT_VECTOR => (string)$token->getIv(),
3336
self::QUERY_STRING_CURRENT_PATH => $currentPath,

src/Authenticator.php

+4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ class Authenticator {
1414
private SessionContainer $session;
1515
private SessionData $sessionData;
1616
private RedirectHandler $redirectHandler;
17+
private string $clientId;
1718

1819
public function __construct(
20+
string $clientId,
1921
string $clientKey,
2022
string $currentUriPath,
2123
string $authwaveHost = "login.authwave.com",
@@ -32,6 +34,7 @@ public function __construct(
3234
$session->set(self::SESSION_KEY, new SessionData());
3335
}
3436

37+
$this->clientId = $clientId;
3538
$this->clientKey = $clientKey;
3639
$this->currentUriPath = $currentUriPath;
3740
$this->authwaveHost = $authwaveHost;
@@ -69,6 +72,7 @@ public function login(Token $token = null):void {
6972

7073
$loginUri = new AuthUri(
7174
$token,
75+
$this->clientId,
7276
$this->currentUriPath,
7377
$this->authwaveHost
7478
);

src/InitVectorNotSetException.php

-4
This file was deleted.

test/phpunit/AuthUriTest.php

+37-6
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ public function testAuthUriHttps() {
1515
->willReturn("https://example.com");
1616
$token = self::createMock(Token::class);
1717

18-
$sut = new AuthUri($token, "", $baseUri);
18+
$sut = new AuthUri(
19+
$token,
20+
"example-app-id",
21+
"",
22+
$baseUri
23+
);
1924
self::assertEquals(
2025
"https",
2126
$sut->getScheme()
@@ -26,7 +31,13 @@ public function testAuthUriHttps() {
2631
// But it should still default to HTTPS on localhost.
2732
public function testGetAuthUriHostnameLocalhostHttpsByDefault() {
2833
$token = self::createMock(Token::class);
29-
$sut = new AuthUri($token, "/", "localhost");
34+
$sut = new AuthUri(
35+
$token,
36+
"example-app-id",
37+
"/",
38+
"localhost"
39+
);
40+
3041
self::assertStringStartsWith(
3142
"https://localhost",
3243
$sut
@@ -36,7 +47,12 @@ public function testGetAuthUriHostnameLocalhostHttpsByDefault() {
3647
// We should be able to set the scheme to HTTP for localhost hostname only.
3748
public function testGetAuthUriHostnameLocalhostHttpAllowed() {
3849
$token = self::createMock(Token::class);
39-
$sut = new AuthUri($token, "/", "http://localhost");
50+
$sut = new AuthUri(
51+
$token,
52+
"example-app-id",
53+
"/",
54+
"http://localhost"
55+
);
4056
self::assertStringStartsWith(
4157
"http://localhost",
4258
$sut
@@ -47,7 +63,12 @@ public function testGetAuthUriHostnameLocalhostHttpAllowed() {
4763
public function testGetAuthUriHostnameNotLocalhostHttpNotAllowed() {
4864
$token = self::createMock(Token::class);
4965
self::expectException(InsecureProtocolException::class);
50-
new AuthUri($token, "/", "http://localhost.com");
66+
new AuthUri(
67+
$token,
68+
"example-app-id",
69+
"/",
70+
"http://localhost.com"
71+
);
5172
}
5273

5374
public function testAuthUriHttpsInferred() {
@@ -57,7 +78,12 @@ public function testAuthUriHttpsInferred() {
5778
// Note on the line above, no scheme is passed in - we must assume https.
5879
$token = self::createMock(Token::class);
5980

60-
$sut = new AuthUri($token, "/", $baseUri);
81+
$sut = new AuthUri(
82+
$token,
83+
"example-app-id",
84+
"/",
85+
$baseUri);
86+
6187
self::assertEquals(
6288
"https",
6389
$sut->getScheme()
@@ -79,7 +105,12 @@ public function testQueryString() {
79105
->willReturn($iv);
80106

81107
$returnPath = "/examplePage";
82-
$sut = new AuthUri($token, $returnPath, $baseUri);
108+
$sut = new AuthUri(
109+
$token,
110+
"example-app-id",
111+
$returnPath,
112+
$baseUri
113+
);
83114
parse_str($sut->getQuery(), $queryParts);
84115

85116
self::assertEquals(

test/phpunit/AuthenticatorTest.php

+34-10
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+
"example-app-id",
21+
"test-key",
22+
"/"
23+
);
2024
}
2125

2226
public function testConstructWithDefaultSession() {
2327
$_SESSION = [];
24-
new Authenticator("test-key", "/");
28+
new Authenticator(
29+
"example-app-id",
30+
"test-key",
31+
"/"
32+
);
2533
self::assertArrayHasKey(
2634
Authenticator::SESSION_KEY,
2735
$_SESSION
@@ -31,6 +39,7 @@ public function testConstructWithDefaultSession() {
3139
public function testIsLoggedInFalseByDefault() {
3240
$_SESSION = [];
3341
$sut = new Authenticator(
42+
"example-app-id",
3443
"test-key",
3544
"/"
3645
);
@@ -49,8 +58,9 @@ public function testIsLoggedInTrueWhenSessionDataSet() {
4958
];
5059

5160
$sut = new Authenticator(
61+
"example-app-id",
5262
"test-key",
53-
"/",
63+
"/"
5464
);
5565
self::assertTrue($sut->isLoggedIn());
5666
}
@@ -62,8 +72,9 @@ public function testLogoutClearsSession() {
6272
];
6373

6474
$sut = new Authenticator(
75+
"example-app-id",
6576
"test-key",
66-
"/",
77+
"/"
6778
);
6879
$sut->logout();
6980
self::assertEmpty($_SESSION);
@@ -76,13 +87,14 @@ public function testLoginRedirects() {
7687
$redirectHandler->expects(self::once())
7788
->method("redirect")
7889
->with(self::callback(fn(UriInterface $uri) =>
79-
$uri->getHost() === AuthUri::DEFAULT_BASE_URI
90+
$uri->getHost() === AuthUri::DEFAULT_BASE_REMOTE_URI
8091
));
8192

8293
$sut = new Authenticator(
94+
"example-app-id",
8395
"test-key",
8496
"/",
85-
AuthUri::DEFAULT_BASE_URI,
97+
AuthUri::DEFAULT_BASE_REMOTE_URI,
8698
null,
8799
$redirectHandler
88100
);
@@ -102,6 +114,7 @@ public function testLoginRedirectsLocalhost() {
102114
));
103115

104116
$sut = new Authenticator(
117+
"example-app-id",
105118
"test-key",
106119
"/",
107120
"http://localhost:8081",
@@ -117,6 +130,7 @@ public function testLoginRedirectsWithCorrectQueryString() {
117130
$key = uniqid("key-");
118131
$currentPath = uniqid("/path/");
119132

133+
$id = "example-app-id";
120134
$cipher = "example-cipher";
121135
$ivString = "example-iv";
122136

@@ -131,6 +145,7 @@ public function testLoginRedirectsWithCorrectQueryString() {
131145
->willReturn($iv);
132146

133147
$expectedQueryParts = [
148+
AuthUri::QUERY_STRING_ID => $id,
134149
AuthUri::QUERY_STRING_CIPHER => $cipher,
135150
AuthUri::QUERY_STRING_INIT_VECTOR => $ivString,
136151
AuthUri::QUERY_STRING_CURRENT_PATH => $currentPath,
@@ -145,9 +160,10 @@ public function testLoginRedirectsWithCorrectQueryString() {
145160
));
146161

147162
$sut = new Authenticator(
163+
$id,
148164
$key,
149165
$currentPath,
150-
AuthUri::DEFAULT_BASE_URI,
166+
AuthUri::DEFAULT_BASE_REMOTE_URI,
151167
null,
152168
$redirectHandler
153169
);
@@ -165,9 +181,10 @@ public function testLoginDoesNothingWhenAlreadyLoggedIn() {
165181
->method("redirect");
166182

167183
$sut = new Authenticator(
184+
"example-app-id",
168185
"test-key",
169186
"/",
170-
AuthUri::DEFAULT_BASE_URI,
187+
AuthUri::DEFAULT_BASE_REMOTE_URI,
171188
null,
172189
$redirectHandler
173190
);
@@ -178,6 +195,7 @@ public function testLoginDoesNothingWhenAlreadyLoggedIn() {
178195
public function testGetUuidThrowsExceptionWhenNotLoggedIn() {
179196
$_SESSION = [];
180197
$sut = new Authenticator(
198+
"example-app-id",
181199
"test-key",
182200
"/"
183201
);
@@ -199,6 +217,7 @@ public function testGetUuid() {
199217
Authenticator::SESSION_KEY => $sessionData,
200218
];
201219
$sut = new Authenticator(
220+
"example-app-id",
202221
"test-key",
203222
"/"
204223
);
@@ -208,6 +227,7 @@ public function testGetUuid() {
208227
public function testGetEmailThrowsExceptionWhenNotLoggedIn() {
209228
$_SESSION = [];
210229
$sut = new Authenticator(
230+
"example-app-id",
211231
"test-key",
212232
"/"
213233
);
@@ -229,6 +249,7 @@ public function testGetEmail() {
229249
Authenticator::SESSION_KEY => $sessionData,
230250
];
231251
$sut = new Authenticator(
252+
"example-app-id",
232253
"test-key",
233254
"/"
234255
);
@@ -243,6 +264,7 @@ public function testCompleteAuthNotLoggedIn() {
243264
$_SESSION = [];
244265
self::expectException(NotLoggedInException::class);
245266
new Authenticator(
267+
"example-app-id",
246268
"test-key",
247269
$currentUri
248270
);
@@ -275,9 +297,10 @@ public function testCompleteAuth() {
275297
Authenticator::SESSION_KEY => $sessionData,
276298
];
277299
new Authenticator(
300+
"example-app-id",
278301
"test-key",
279302
$currentUri,
280-
AuthUri::DEFAULT_BASE_URI,
303+
AuthUri::DEFAULT_BASE_REMOTE_URI,
281304
null,
282305
$redirectHandler
283306
);
@@ -302,9 +325,10 @@ public function testCompleteAuthNotAffectedByQueryString() {
302325
$_SESSION = [];
303326

304327
new Authenticator(
328+
"example-app-id",
305329
"test-key",
306330
"/example-path?filter=something",
307-
AuthUri::DEFAULT_BASE_URI,
331+
AuthUri::DEFAULT_BASE_REMOTE_URI,
308332
null,
309333
$redirectHandler
310334
);

0 commit comments

Comments
 (0)