Skip to content

Commit

Permalink
Revert "feat(notifier): add support for basic auth"
Browse files Browse the repository at this point in the history
  • Loading branch information
mmgoodnow authored Sep 24, 2023
1 parent d65f000 commit d12e717
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 48 deletions.
2 changes: 0 additions & 2 deletions src/config.template.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ module.exports = {
* cross-seed will send POST requests to this url
* with a JSON payload of { title, body }.
* Conforms to the caronc/apprise REST API.
* If necessary, supply your username and password inside the url like so:
* "http://username:password@localhost:8000/notify/cross-seed"
*/
notificationWebhookUrl: undefined,

Expand Down
2 changes: 0 additions & 2 deletions src/config.template.docker.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,6 @@ module.exports = {
* cross-seed will send POST requests to this url
* with a JSON payload of { title, body }.
* Conforms to the caronc/apprise REST API.
* If necessary, supply your username and password inside the url like so:
* "http://username:password@localhost:8000/notify/cross-seed"
*/
notificationWebhookUrl: undefined,

Expand Down
53 changes: 9 additions & 44 deletions src/pushNotifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,54 +20,19 @@ interface PushNotification {

export class PushNotifier {
url: string;
username: string | undefined;
password: string | undefined;

constructor(url: string) {
const urlObj = new URL(url);

if (urlObj.username && urlObj.password) {
this.username = urlObj.username;
this.password = urlObj.password;

// Remove the username and password from the URL
urlObj.username = "";
urlObj.password = "";

this.url = urlObj.toString();
} else {
constructor(url: string) {
this.url = url;
}
}

async notify({ title = "cross-seed", body, ...rest }: PushNotification): Promise<void> {
notify({ title = "cross-seed", body, ...rest }: PushNotification): void {
if (this.url) {
const headers = new Headers();
headers.append("Content-Type", "application/json");

if (this.username && this.password) {
const credentials = `${this.username}:${this.password}`;
const basicAuth = "Basic " + btoa(credentials);
headers.append("Authorization", basicAuth);
}

logger.verbose(`Notification request send to ${this.url}`);

try {
const response = await fetch(this.url, {
method: "POST",
headers,
body: JSON.stringify({ title, body, ...rest }),
});

const responseText = await response.text();
response.ok
? logger.verbose(`Notifiaction server response:\n${responseText}`)
: logger.error(`Notifiaction server error: ${response.status}, ${response.statusText}`);

} catch (error) {
logger.error({ message: "Failed to send push notification", error });
}
fetch(this.url, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ title, body, ...rest }),
}).catch(() => {
logger.error({ message: "Failed to send push notification" });
});
}
}
}
Expand Down

0 comments on commit d12e717

Please sign in to comment.