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

WIP: Auth

This commit is contained in:
Philipp Heckel 2022-02-25 13:40:03 -05:00
parent 42016f48ff
commit 1599793de2
5 changed files with 108 additions and 32 deletions
web/src/app

View file

@ -1,4 +1,4 @@
import {topicUrlJsonPoll, fetchLinesIterator, topicUrl} from "./utils";
import {topicUrlJsonPoll, fetchLinesIterator, topicUrl, topicUrlAuth} from "./utils";
class Api {
async poll(baseUrl, topic) {
@ -19,6 +19,20 @@ class Api {
body: message
});
}
async auth(baseUrl, topic, user) {
const url = topicUrlAuth(baseUrl, topic);
console.log(`[Api] Checking auth for ${url}`);
const response = await fetch(url);
if (response.status >= 200 && response.status <= 299) {
return true;
} else if (!user && response.status === 404) {
return true; // Special case: Anonymous login to old servers return 404 since /<topic>/auth doesn't exist
} else if (response.status === 401 || response.status === 403) { // See server/server.go
return false;
}
throw new Error(`Unexpected server response ${response.status}`);
}
}
const api = new Api();