From e96beb416bc0e79688461a5dd6c11bbc20960411 Mon Sep 17 00:00:00 2001 From: jirikmik Date: Mon, 26 Aug 2024 09:04:18 +0200 Subject: [PATCH 1/3] Fix authorization header with self-hosted Ntfy --- src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyTransport.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyTransport.php b/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyTransport.php index da08588638182..eb6569d9a3308 100644 --- a/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyTransport.php @@ -87,7 +87,7 @@ protected function doSend(MessageInterface $message): SentMessage $headers = []; if (null !== $this->user && null !== $this->password) { - $headers['Authorization'] = 'Basic '.rtrim(base64_encode($this->user.':'.$this->password), '='); + $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->password); } elseif (null !== $this->password) { $headers['Authorization'] = 'Bearer '.$this->password; } From db86f7f165cf31c941e4e28a208ff64bbe674995 Mon Sep 17 00:00:00 2001 From: jirikmik Date: Mon, 26 Aug 2024 09:09:10 +0200 Subject: [PATCH 2/3] Added example for token auth --- .../Component/Notifier/Bridge/Ntfy/README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Notifier/Bridge/Ntfy/README.md b/src/Symfony/Component/Notifier/Bridge/Ntfy/README.md index 8b685d245bf2c..eb399935bc799 100644 --- a/src/Symfony/Component/Notifier/Bridge/Ntfy/README.md +++ b/src/Symfony/Component/Notifier/Bridge/Ntfy/README.md @@ -5,16 +5,24 @@ Provides [Ntfy](https://docs.ntfy.sh/) integration for Symfony Notifier. DSN example ----------- - ``` -NTFY_DSN=ntfy://[USER:PASSWORD]@default[:PORT]/TOPIC?[secureHttp=[on]] +NTFY_DSN=ntfy://URL[:PORT]/TOPIC?[secureHttp=[on]] +``` +or with username and password authorization: +``` +NTFY_DSN=ntfy://USER:PASSWORD@URL[:PORT]/TOPIC?[secureHttp=[on]] +``` +or with token authorization: +``` +NTFY_DSN=ntfy://:TOKEN@URL[:PORT]/TOPIC?[secureHttp=[on]] ``` where: - `URL` is the ntfy server which you are using - - if `default` is provided, this will default to the public ntfy server hosted on [ntfy.sh](https://ntfy.sh/). + - if value `default` is provided, this will default to the public ntfy server hosted on [ntfy.sh](https://ntfy.sh/). - `TOPIC` is the topic on this ntfy server. - `PORT` is an optional specific port. - `USER`and `PASSWORD` are username and password in case of access control supported by the server +- `TOKEN` is token value provided by server for user In case of a non-secure server, you can disable https by setting `secureHttp=off`. For example if you use a local [Ntfy Docker image](https://hub.docker.com/r/binwiederhier/ntfy) during development or testing. From b6e97321897881aff50b0aafbfa85cda871eb85d Mon Sep 17 00:00:00 2001 From: jirikmik Date: Mon, 26 Aug 2024 18:38:55 +0200 Subject: [PATCH 3/3] Auth headers created by http client --- .../Notifier/Bridge/Ntfy/NtfyTransport.php | 23 ++++++++----------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyTransport.php b/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyTransport.php index eb6569d9a3308..e4ffecbb9e479 100644 --- a/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyTransport.php +++ b/src/Symfony/Component/Notifier/Bridge/Ntfy/NtfyTransport.php @@ -73,29 +73,26 @@ protected function doSend(MessageInterface $message): SentMessage $opts = NtfyOptions::fromNotification($notification); } - $options = $opts ? $opts->toArray() : []; + $json_options = $opts? $opts->toArray() : []; - $options['topic'] = $this->getTopic(); + $json_options['topic'] = $this->getTopic(); - if (!isset($options['title'])) { - $options['title'] = $message->getSubject(); + if (!isset($json_options['title'])) { + $json_options['title'] = $message->getSubject(); } - if (!isset($options['message'])) { - $options['message'] = $message->getContent(); + if (!isset($json_options['message'])) { + $json_options['message'] = $message->getContent(); } - $headers = []; + $client_options = ['json' => $json_options]; if (null !== $this->user && null !== $this->password) { - $headers['Authorization'] = 'Basic '.base64_encode($this->user.':'.$this->password); + $client_options['auth_basic'] = [$this->user, $this->password]; } elseif (null !== $this->password) { - $headers['Authorization'] = 'Bearer '.$this->password; + $client_options['auth_bearer'] = $this->password; } - $response = $this->client->request('POST', ($this->secureHttp ? 'https' : 'http').'://'.$this->getEndpoint(), [ - 'headers' => $headers, - 'json' => $options, - ]); + $response = $this->client->request('POST', ($this->secureHttp? 'https' : 'http') . '://' . $this->getEndpoint(), $client_options); try { $statusCode = $response->getStatusCode();