|
6 | 6 |
|
7 | 7 | class Authenticator {
|
8 | 8 | private Cipher $cipher;
|
9 |
| - private string $httpsScheme = "https"; |
10 |
| - private string $httpHost; |
| 9 | + private UriInterface $baseUri; |
11 | 10 |
|
12 |
| - public function __construct(Token $token, string $hostname) { |
| 11 | + public function __construct(Token $token, string $baseUri) { |
13 | 12 | $this->cipher = $token->generateCipher();
|
14 |
| - $this->httpHost = $hostname; |
| 13 | + $this->baseUri = $this->normaliseBaseUri($baseUri); |
15 | 14 | }
|
16 | 15 |
|
17 |
| - public function useLocalhostHttps(bool $useHttps = true) { |
18 |
| - if(!$useHttps) { |
19 |
| - if($this->httpHost !== "localhost") { |
20 |
| - throw new InsecureProtocolException(); |
21 |
| - } |
22 |
| - |
23 |
| - $this->httpsScheme = "http"; |
24 |
| - } |
| 16 | + public function getAuthUri():UriInterface { |
| 17 | + return $this->baseUri; |
25 | 18 | }
|
26 | 19 |
|
27 |
| - public function getAuthUri():UriInterface { |
| 20 | + private function normaliseBaseUri(string $baseUri):Uri { |
| 21 | + $scheme = parse_url($baseUri, PHP_URL_SCHEME) ?? "https"; |
| 22 | + $host = parse_url($baseUri, PHP_URL_HOST) ?? |
| 23 | + parse_url($baseUri, PHP_URL_PATH); |
| 24 | + |
28 | 25 | $uri = (new Uri())
|
29 |
| - ->withScheme("https") |
30 |
| - ->withHost($this->httpHost); |
| 26 | + ->withScheme($scheme) |
| 27 | + ->withHost($host); |
31 | 28 |
|
32 |
| - if($this->httpHost === "localhost") { |
33 |
| - $uri = $uri->withScheme($this->httpsScheme); |
| 29 | + if($uri->getHost() !== "localhost" |
| 30 | + && $uri->getScheme() !== "https") { |
| 31 | + throw new InsecureProtocolException($uri->getScheme()); |
34 | 32 | }
|
35 | 33 |
|
36 | 34 | return $uri;
|
|
0 commit comments