Skip to content

Commit e230f90

Browse files
author
Greg Bowler
committed
Test login uri matches expected pattern
1 parent 086ff76 commit e230f90

File tree

4 files changed

+62
-12
lines changed

4 files changed

+62
-12
lines changed

src/AuthUri.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ class AuthUri extends Uri {
88

99
const QUERY_STRING_CIPHER = "cipher";
1010
const QUERY_STRING_INIT_VECTOR = "iv";
11-
const QUERY_STRING_RETURN_PATH = "return";
11+
const QUERY_STRING_CURRENT_PATH = "path";
1212

1313
/**
1414
* @param Token $token This must be the same instance of the Token when
1515
* creating Authenticator for the first time as it is when checking the
1616
* response from the Authwave provider (store in a session).
17-
* @param string $returnPath
17+
* @param string $currentPath
1818
* @param string $baseUri The base URI of the application. This is the
1919
* URI authority with optional scheme, as localhost allows http://
2020
*/
2121
public function __construct(
2222
Token $token,
23-
string $returnPath = "/",
23+
string $currentPath = "/",
2424
string $baseUri = self::DEFAULT_BASE_URI
2525
) {
2626
$baseUri = $this->normaliseBaseUri($baseUri);
@@ -30,7 +30,7 @@ public function __construct(
3030
$this->query = http_build_query([
3131
self::QUERY_STRING_CIPHER => (string)$token->generateCipher(),
3232
self::QUERY_STRING_INIT_VECTOR => (string)$token->getIv(),
33-
self::QUERY_STRING_RETURN_PATH => base64_encode($returnPath),
33+
self::QUERY_STRING_CURRENT_PATH => $currentPath,
3434
]);
3535
}
3636

src/Authenticator.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Authenticator {
88

99
private string $clientKey;
1010
private string $clientSecret;
11-
private string $redirectPath;
11+
private string $currentUriPath;
1212
private string $authwaveHost;
1313
private SessionContainer $session;
1414
private SessionData $sessionData;
@@ -17,7 +17,7 @@ class Authenticator {
1717
public function __construct(
1818
string $clientKey,
1919
string $clientSecret,
20-
string $redirectPath,
20+
string $currentUriPath,
2121
string $authwaveHost = "login.authwave.com",
2222
SessionContainer $session = null,
2323
RedirectHandler $redirectHandler = null
@@ -32,7 +32,7 @@ public function __construct(
3232

3333
$this->clientKey = $clientKey;
3434
$this->clientSecret = $clientSecret;
35-
$this->redirectPath = $redirectPath;
35+
$this->currentUriPath = $currentUriPath;
3636
$this->authwaveHost = $authwaveHost;
3737
$this->session = $session;
3838
$this->sessionData = $session->get(self::SESSION_KEY);
@@ -56,15 +56,18 @@ public function isLoggedIn():bool {
5656
return isset($userData);
5757
}
5858

59-
public function login():void {
59+
public function login(Token $token = null):void {
6060
if($this->isLoggedIn()) {
6161
return;
6262
}
6363

64-
$token = new Token($this->clientKey, $this->clientSecret);
64+
if(is_null($token)) {
65+
$token = new Token($this->clientKey, $this->clientSecret);
66+
}
67+
6568
$loginUri = new AuthUri(
6669
$token,
67-
$this->redirectPath,
70+
$this->currentUriPath,
6871
$this->authwaveHost
6972
);
7073
$this->redirectHandler->redirect($loginUri);

test/phpunit/AuthUriTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ public function testQueryString() {
9393
);
9494

9595
self::assertEquals(
96-
base64_encode($returnPath),
97-
$queryParts[AuthUri::QUERY_STRING_RETURN_PATH]
96+
$returnPath,
97+
$queryParts[AuthUri::QUERY_STRING_CURRENT_PATH]
9898
);
9999
}
100100
}

test/phpunit/AuthenticatorTest.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33

44
use Authwave\Authenticator;
55
use Authwave\AuthUri;
6+
use Authwave\InitVector;
67
use Authwave\RedirectHandler;
78
use Authwave\SessionData;
89
use Authwave\SessionNotStartedException;
10+
use Authwave\Token;
911
use Authwave\UserData;
1012
use PHPUnit\Framework\TestCase;
1113
use Psr\Http\Message\UriInterface;
@@ -110,4 +112,49 @@ public function testLoginRedirectsLocalhost() {
110112
);
111113
$sut->login();
112114
}
115+
116+
public function testLoginRedirectsWithCorrectQueryString() {
117+
$_SESSION = [];
118+
119+
$key = uniqid("key-");
120+
$secret = uniqid("secret-");
121+
$currentPath = uniqid("/path/");
122+
123+
$cipher = "example-cipher";
124+
$ivString = "example-iv";
125+
126+
$iv = self::createMock(InitVector::class);
127+
$iv->method("__toString")
128+
->willReturn($ivString);
129+
130+
$token = self::createMock(Token::class);
131+
$token->method("generateCipher")
132+
->willReturn($cipher);
133+
$token->method("getIv")
134+
->willReturn($iv);
135+
136+
$expectedQueryParts = [
137+
AuthUri::QUERY_STRING_CIPHER => $cipher,
138+
AuthUri::QUERY_STRING_INIT_VECTOR => $ivString,
139+
AuthUri::QUERY_STRING_CURRENT_PATH => $currentPath,
140+
];
141+
$expectedQuery = http_build_query($expectedQueryParts);
142+
143+
$redirectHandler = self::createMock(RedirectHandler::class);
144+
$redirectHandler->expects(self::once())
145+
->method("redirect")
146+
->with(self::callback(fn(UriInterface $uri) =>
147+
$uri->getQuery() === $expectedQuery
148+
));
149+
150+
$sut = new Authenticator(
151+
$key,
152+
$secret,
153+
$currentPath,
154+
AuthUri::DEFAULT_BASE_URI,
155+
null,
156+
$redirectHandler
157+
);
158+
$sut->login($token);
159+
}
113160
}

0 commit comments

Comments
 (0)