-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpAuthorization.php
141 lines (119 loc) · 4.16 KB
/
HttpAuthorization.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
declare(strict_types=1);
/*
* This file is part of the Composer package "eliashaeussler/cpanel-requests".
*
* Copyright (C) 2020-2025 Elias Häußler <elias@haeussler.dev>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace EliasHaeussler\CpanelRequests\Application\Authorization;
use EliasHaeussler\CpanelRequests\Application;
use EliasHaeussler\CpanelRequests\Exception;
use EliasHaeussler\CpanelRequests\Http;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie;
use GuzzleHttp\Psr7;
use GuzzleHttp\RequestOptions;
use OTPHP\TOTP;
use Psr\Http\Client as PsrClient;
use Psr\Http\Message;
use function assert;
use function is_string;
use function register_shutdown_function;
/**
* HttpAuthorization.
*
* @author Elias Häußler <elias@haeussler.dev>
* @license GPL-3.0-or-later
*/
final class HttpAuthorization implements AuthorizationInterface
{
private readonly Client $client;
private readonly Message\RequestFactoryInterface $requestFactory;
private ?Http\UriBuilder\UriBuilderInterface $uriBuilder = null;
private ?Application\Session\WebSession $session = null;
public function __construct(
private readonly string $username,
private readonly string $password,
private readonly ?string $otpSecret = null,
) {
$this->client = new Client();
$this->requestFactory = new Psr7\HttpFactory();
}
public function sendAuthorizedRequest(
string $method,
Http\Request\ApiRequest $request,
array $options = [],
): Message\ResponseInterface {
// Assure active session exists
$this->login($request->getBaseUri());
assert($this->session instanceof Application\Session\WebSession);
// Add cookie file
$cookieJar = new Cookie\FileCookieJar($this->session->getCookie()->getPathname(), true);
$options[RequestOptions::COOKIES] = $cookieJar;
// Create URI
$uriBuilder = $this->createUriBuilder();
$uri = $uriBuilder->buildUriForRequest($request);
return $this->client->send($this->requestFactory->createRequest($method, $uri), $options);
}
/**
* @throws PsrClient\ClientExceptionInterface
*/
private function login(Message\UriInterface $baseUri): void
{
if ($this->hasActiveSession()) {
return;
}
$this->session = new Application\Session\WebSession($baseUri);
$this->session->start($this->username, $this->password, $this->getOTP());
// Perform logout on shutdown
register_shutdown_function($this->logout(...));
}
/**
* @throws PsrClient\ClientExceptionInterface
*
* @codeCoverageIgnore
*/
private function logout(): void
{
if (!$this->hasActiveSession()) {
return;
}
$this->session?->stop();
}
private function hasActiveSession(): bool
{
return true === $this->session?->isActive();
}
private function getOTP(): ?string
{
if (!is_string($this->otpSecret) || '' === trim($this->otpSecret)) {
return null;
}
return TOTP::createFromSecret(trim($this->otpSecret))->now();
}
private function createUriBuilder(): Http\UriBuilder\UriBuilderInterface
{
// @codeCoverageIgnoreStart
if (null === $this->session) {
throw Exception\SessionException::forInactiveSession();
}
// @codeCoverageIgnoreEnd
if (null === $this->uriBuilder) {
$this->uriBuilder = new Http\UriBuilder\SessionBasedUriBuilder($this->session);
}
return $this->uriBuilder;
}
}