Skip to content

Commit edf3807

Browse files
author
Greg Bowler
committed
Test login redirects to default Authwave host
1 parent 9000199 commit edf3807

File tree

4 files changed

+30
-29
lines changed

4 files changed

+30
-29
lines changed

src/AuthUri.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
use Psr\Http\Message\UriInterface;
66

77
class AuthUri extends Uri {
8+
const DEFAULT_BASE_URI = "login.authwave.com";
9+
810
const QUERY_STRING_CIPHER = "cipher";
911
const QUERY_STRING_INIT_VECTOR = "iv";
1012
const QUERY_STRING_RETURN_PATH = "return";
@@ -13,13 +15,14 @@ class AuthUri extends Uri {
1315
* @param Token $token This must be the same instance of the Token when
1416
* creating Authenticator for the first time as it is when checking the
1517
* response from the Authwave provider (store in a session).
18+
* @param string $returnPath
1619
* @param string $baseUri The base URI of the application. This is the
1720
* URI authority with optional scheme, as localhost allows http://
1821
*/
1922
public function __construct(
2023
Token $token,
21-
string $baseUri,
22-
string $returnPath = "/"
24+
string $returnPath = "/",
25+
string $baseUri = self::DEFAULT_BASE_URI
2326
) {
2427
$baseUri = $this->normaliseBaseUri($baseUri);
2528

src/Authenticator.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ class Authenticator {
99
private string $clientKey;
1010
private string $clientSecret;
1111
private string $redirectPath;
12+
private string $authwaveHost;
1213
private SessionContainer $session;
1314
private SessionData $sessionData;
1415
private RedirectHandler $redirectHandler;
@@ -17,6 +18,7 @@ public function __construct(
1718
string $clientKey,
1819
string $clientSecret,
1920
string $redirectPath,
21+
string $authwaveHost = "login.authwave.com",
2022
SessionContainer $session = null,
2123
RedirectHandler $redirectHandler = null
2224
) {
@@ -31,6 +33,7 @@ public function __construct(
3133
$this->clientKey = $clientKey;
3234
$this->clientSecret = $clientSecret;
3335
$this->redirectPath = $redirectPath;
36+
$this->authwaveHost = $authwaveHost;
3437
$this->session = $session;
3538
$this->sessionData = $session->get(self::SESSION_KEY);
3639
$this->redirectHandler = $redirectHandler ?? new RedirectHandler();
@@ -58,7 +61,9 @@ public function login():void {
5861
return;
5962
}
6063

61-
64+
$token = new Token($this->clientKey, $this->clientSecret);
65+
$loginUri = new AuthUri($token, $this->authwaveHost);
66+
$this->redirectHandler->redirect($loginUri);
6267
}
6368

6469
public function logout():void {

test/phpunit/AuthUriTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ 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($token, "", $baseUri);
1919
self::assertEquals(
2020
"https",
2121
$sut->getScheme()
@@ -26,7 +26,7 @@ public function testAuthUriHttps() {
2626
// But it should still default to HTTPS on localhost.
2727
public function testGetAuthUriHostnameLocalhostHttpsByDefault() {
2828
$token = self::createMock(Token::class);
29-
$sut = new AuthUri($token, "localhost");
29+
$sut = new AuthUri($token, "/", "localhost");
3030
self::assertStringStartsWith(
3131
"https://localhost",
3232
$sut
@@ -36,7 +36,7 @@ public function testGetAuthUriHostnameLocalhostHttpsByDefault() {
3636
// We should be able to set the scheme to HTTP for localhost hostname only.
3737
public function testGetAuthUriHostnameLocalhostHttpAllowed() {
3838
$token = self::createMock(Token::class);
39-
$sut = new AuthUri($token, "http://localhost");
39+
$sut = new AuthUri($token, "/", "http://localhost");
4040
self::assertStringStartsWith(
4141
"http://localhost",
4242
$sut
@@ -47,7 +47,7 @@ public function testGetAuthUriHostnameLocalhostHttpAllowed() {
4747
public function testGetAuthUriHostnameNotLocalhostHttpNotAllowed() {
4848
$token = self::createMock(Token::class);
4949
self::expectException(InsecureProtocolException::class);
50-
new AuthUri($token, "http://localhost.com");
50+
new AuthUri($token, "/", "http://localhost.com");
5151
}
5252

5353
public function testAuthUriHttpsInferred() {
@@ -57,7 +57,7 @@ public function testAuthUriHttpsInferred() {
5757
// Note on the line above, no scheme is passed in - we must assume https.
5858
$token = self::createMock(Token::class);
5959

60-
$sut = new AuthUri($token, $baseUri, "");
60+
$sut = new AuthUri($token, "/", $baseUri);
6161
self::assertEquals(
6262
"https",
6363
$sut->getScheme()
@@ -79,7 +79,7 @@ public function testQueryString() {
7979
->willReturn($iv);
8080

8181
$returnPath = "/examplePage";
82-
$sut = new AuthUri($token, $baseUri, $returnPath);
82+
$sut = new AuthUri($token, $returnPath, $baseUri);
8383
parse_str($sut->getQuery(), $queryParts);
8484

8585
self::assertEquals(

test/phpunit/AuthenticatorTest.php

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,28 @@
22
namespace Authwave\Test;
33

44
use Authwave\Authenticator;
5+
use Authwave\AuthUri;
56
use Authwave\GlobalSessionContainer;
67
use Authwave\RedirectHandler;
78
use Authwave\SessionData;
89
use Authwave\SessionNotStartedException;
910
use Authwave\Test\Helper\TestRedirectHandler;
1011
use Authwave\UserData;
1112
use PHPUnit\Framework\TestCase;
13+
use Psr\Http\Message\UriInterface;
1214

1315
class AuthenticatorTest extends TestCase {
1416
public function testConstructWithDefaultSessionNotStarted() {
1517
self::expectException(SessionNotStartedException::class);
1618
new Authenticator(
17-
"test-key",
18-
"test-secret",
19-
"/"
19+
"test-key", "test-secret", "/",
2020
);
2121
}
2222

2323
public function testConstructWithDefaultSession() {
2424
$_SESSION = [];
2525
new Authenticator(
26-
"test-key",
27-
"test-secret",
28-
"/"
26+
"test-key", "test-secret", "/",
2927
);
3028
self::assertArrayHasKey(
3129
Authenticator::SESSION_KEY,
@@ -36,9 +34,7 @@ public function testConstructWithDefaultSession() {
3634
public function testIsLoggedInFalseByDefault() {
3735
$_SESSION = [];
3836
$sut = new Authenticator(
39-
"test-key",
40-
"test-secret",
41-
"/"
37+
"test-key", "test-secret", "/",
4238
);
4339
self::assertFalse($sut->isLoggedIn());
4440
}
@@ -55,9 +51,7 @@ public function testIsLoggedInTrueWhenSessionDataSet() {
5551
];
5652

5753
$sut = new Authenticator(
58-
"test-key",
59-
"test-secret",
60-
"/"
54+
"test-key", "test-secret", "/",
6155
);
6256
self::assertTrue($sut->isLoggedIn());
6357
}
@@ -69,28 +63,27 @@ public function testLogoutClearsSession() {
6963
];
7064

7165
$sut = new Authenticator(
72-
"test-key",
73-
"test-secret",
74-
"/"
66+
"test-key", "test-secret", "/",
7567
);
7668
$sut->logout();
7769
self::assertEmpty($_SESSION);
7870
}
7971

8072
public function testLoginRedirects() {
73+
$_SESSION = [];
74+
8175
$redirectHandler = self::createMock(RedirectHandler::class);
8276
$redirectHandler->expects(self::once())
8377
->method("redirect")
84-
->with(
85-
self::callback(function($uri) {
86-
echo $uri;
87-
})
88-
);
78+
->with(self::callback(fn(UriInterface $uri) =>
79+
$uri->getHost() === AuthUri::DEFAULT_BASE_URI
80+
));
8981

9082
$sut = new Authenticator(
9183
"test-key",
9284
"test-secret",
9385
"/",
86+
AuthUri::DEFAULT_BASE_URI,
9487
null,
9588
$redirectHandler
9689
);

0 commit comments

Comments
 (0)