Skip to content

Commit 4d4e841

Browse files
author
Greg Bowler
committed
Refactor: merge Authenticator into AuthUri class
1 parent 3477e05 commit 4d4e841

File tree

4 files changed

+74
-111
lines changed

4 files changed

+74
-111
lines changed

src/AuthUri.php

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@ class AuthUri extends Uri {
99
const QUERY_STRING_INIT_VECTOR = "iv";
1010
const QUERY_STRING_RETURN_PATH = "return";
1111

12+
/**
13+
* @param Token $token This must be the same instance of the Token when
14+
* creating Authenticator for the first time as it is when checking the
15+
* response from the Authwave provider (store in a session).
16+
* @param string $baseUri The base URI of the application. This is the
17+
* URI authority with optional scheme, as localhost allows http://
18+
*/
1219
public function __construct(
13-
UriInterface $baseUri,
1420
Token $token,
15-
string $returnPath
21+
string $baseUri,
22+
string $returnPath = "/"
1623
) {
24+
$baseUri = $this->normaliseBaseUri($baseUri);
25+
1726
parent::__construct($baseUri);
1827

1928
$this->query = http_build_query([
@@ -22,4 +31,22 @@ public function __construct(
2231
self::QUERY_STRING_RETURN_PATH => base64_encode($returnPath),
2332
]);
2433
}
34+
35+
private function normaliseBaseUri(string $baseUri):Uri {
36+
$scheme = parse_url($baseUri, PHP_URL_SCHEME)
37+
?? "https";
38+
$host = parse_url($baseUri, PHP_URL_HOST)
39+
?? parse_url($baseUri, PHP_URL_PATH);
40+
41+
$uri = (new Uri())
42+
->withScheme($scheme)
43+
->withHost($host);
44+
45+
if($uri->getHost() !== "localhost"
46+
&& $uri->getScheme() !== "https") {
47+
throw new InsecureProtocolException($uri->getScheme());
48+
}
49+
50+
return $uri;
51+
}
2552
}

src/Authenticator.php

Lines changed: 0 additions & 58 deletions
This file was deleted.

test/phpunit/AuthUriTest.php

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Authwave\AuthUri;
55
use Authwave\InitVector;
6+
use Authwave\InsecureProtocolException;
67
use Authwave\Token;
78
use PHPUnit\Framework\TestCase;
89
use Psr\Http\Message\UriInterface;
@@ -14,7 +15,49 @@ public function testAuthUriHttps() {
1415
->willReturn("https://example.com");
1516
$token = self::createMock(Token::class);
1617

17-
$sut = new AuthUri($baseUri, $token, "");
18+
$sut = new AuthUri($token, $baseUri, "");
19+
self::assertEquals(
20+
"https",
21+
$sut->getScheme()
22+
);
23+
}
24+
25+
// All AuthUris MUST be served over HTTPS, with the one exception of localhost.
26+
// But it should still default to HTTPS on localhost.
27+
public function testGetAuthUriHostnameLocalhostHttpsByDefault() {
28+
$token = self::createMock(Token::class);
29+
$sut = new AuthUri($token, "localhost");
30+
self::assertStringStartsWith(
31+
"https://localhost",
32+
$sut
33+
);
34+
}
35+
36+
// We should be able to set the scheme to HTTP for localhost hostname only.
37+
public function testGetAuthUriHostnameLocalhostHttpAllowed() {
38+
$token = self::createMock(Token::class);
39+
$sut = new AuthUri($token, "http://localhost");
40+
self::assertStringStartsWith(
41+
"http://localhost",
42+
$sut
43+
);
44+
}
45+
46+
// We should NOT be able to set the scheme to HTTP for other hostnames.
47+
public function testGetAuthUriHostnameNotLocalhostHttpNotAllowed() {
48+
$token = self::createMock(Token::class);
49+
self::expectException(InsecureProtocolException::class);
50+
new AuthUri($token, "http://localhost.com");
51+
}
52+
53+
public function testAuthUriHttpsInferred() {
54+
$baseUri = self::createMock(UriInterface::class);
55+
$baseUri->method("__toString")
56+
->willReturn("example.com");
57+
// Note on the line above, no scheme is passed in - we must assume https.
58+
$token = self::createMock(Token::class);
59+
60+
$sut = new AuthUri($token, $baseUri, "");
1861
self::assertEquals(
1962
"https",
2063
$sut->getScheme()
@@ -36,7 +79,7 @@ public function testQueryString() {
3679
->willReturn($iv);
3780

3881
$returnPath = "/examplePage";
39-
$sut = new AuthUri($baseUri, $token, $returnPath);
82+
$sut = new AuthUri($token, $baseUri, $returnPath);
4083
parse_str($sut->getQuery(), $queryParts);
4184

4285
self::assertEquals(

test/phpunit/AuthenticatorTest.php

Lines changed: 0 additions & 49 deletions
This file was deleted.

0 commit comments

Comments
 (0)