1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2025-07-05 18:44:46 +02:00

Fully support auth in Web UI; persist users in localStorage (for now); add ugly ?auth=... param

This commit is contained in:
Philipp Heckel 2022-02-25 23:25:04 -05:00
parent 6d343c0f1a
commit 530f55c234
16 changed files with 237 additions and 72 deletions
web/src/app

View file

@ -1,31 +1,32 @@
import {topicUrlJsonPoll, fetchLinesIterator, topicUrl, topicUrlAuth} from "./utils";
import {topicUrlJsonPoll, fetchLinesIterator, topicUrl, topicUrlAuth, maybeWithBasicAuth} from "./utils";
class Api {
async poll(baseUrl, topic) {
async poll(baseUrl, topic, user) {
const url = topicUrlJsonPoll(baseUrl, topic);
const messages = [];
const headers = maybeWithBasicAuth({}, user);
console.log(`[Api] Polling ${url}`);
for await (let line of fetchLinesIterator(url)) {
for await (let line of fetchLinesIterator(url, headers)) {
messages.push(JSON.parse(line));
}
return messages;
}
async publish(baseUrl, topic, message) {
async publish(baseUrl, topic, user, message) {
const url = topicUrl(baseUrl, topic);
console.log(`[Api] Publishing message to ${url}`);
await fetch(url, {
method: 'PUT',
body: message
body: message,
headers: maybeWithBasicAuth({}, user)
});
}
async auth(baseUrl, topic, user) {
const url = topicUrlAuth(baseUrl, topic);
console.log(`[Api] Checking auth for ${url}`);
const headers = this.maybeAddAuthorization({}, user);
const response = await fetch(url, {
headers: headers
headers: maybeWithBasicAuth({}, user)
});
if (response.status >= 200 && response.status <= 299) {
return true;
@ -36,14 +37,6 @@ class Api {
}
throw new Error(`Unexpected server response ${response.status}`);
}
maybeAddAuthorization(headers, user) {
if (user) {
const encoded = new Buffer(`${user.username}:${user.password}`).toString('base64');
headers['Authorization'] = `Basic ${encoded}`;
}
return headers;
}
}
const api = new Api();