1
0
Fork 0
mirror of https://github.com/MetaProvide/talked-client.git synced 2025-04-15 01:18:34 +00:00

Implement support for HTTP Basic auth, and remove trail slash from server url ()

This commit is contained in:
Magnus Walbeck 2021-08-12 16:53:57 +02:00 committed by GitHub
parent 8140443561
commit 3662e09040
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 6 deletions

View file

@ -76,11 +76,7 @@ class Record extends Command
}
if ($argument === 'info') {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $serverUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
$result = $this->sendGetRequest($serverUrl, '');
$output->writeln($result);
@ -127,7 +123,26 @@ class Record extends Command
return 0;
}
protected function sendGetRequest($serverUrl, $endpoint, $headers = []) {
if ($this->config->getAppValue('talked', 'server_url', '0')) {
$headers = $this->addBasicAuthHeaders($headers);
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $serverUrl . "/" . $endpoint);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
protected function sendPostRequest($serverUrl, $endpoint, $payload, $headers = []) {
if ($this->config->getAppValue('talked', 'server_url', '0')) {
$headers = $this->addBasicAuthHeaders($headers);
}
$headers[] = 'Content-Type: application/json';
$ch = curl_init();
@ -141,4 +156,15 @@ class Record extends Command
return $result;
}
protected function addBasicAuthHeaders() {
$username = $this->config->getAppValue('talked', 'http_basic_auth_username');
$password = $this->config->getAppValue('talked', 'http_basic_auth_password');
$base64EncodedAuth = base64_encode($username . ':' . $password);
$headers[] = 'Authorization: Basic ' . $base64EncodedAuth;
return $headers;
}
}

View file

@ -58,7 +58,7 @@ class SettingsController extends Controller
public function admin(): void
{
if ($this->request->getParam("server_url")) {
$this->config->setAppValue(self::APP_NAME, "server_url", $this->request->getParam("server_url"));
$this->config->setAppValue(self::APP_NAME, "server_url", rtrim($this->request->getParam("server_url"), '/'));
} else {
$this->config->setAppValue(self::APP_NAME, "server_url", "");
}