ntfy/web/src/app/AccountApi.js

258 lines
8.6 KiB
JavaScript
Raw Normal View History

2022-12-25 17:59:44 +01:00
import {
accountPasswordUrl,
accountSettingsUrl,
accountSubscriptionSingleUrl,
accountSubscriptionUrl,
accountTokenUrl,
accountUrl,
fetchLinesIterator,
withBasicAuth,
withBearerAuth,
2022-12-25 17:59:44 +01:00
topicShortUrl,
topicUrl,
topicUrlAuth,
topicUrlJsonPoll,
topicUrlJsonPollWithSince
} from "./utils";
import userManager from "./UserManager";
import session from "./Session";
2022-12-25 19:42:44 +01:00
import subscriptionManager from "./SubscriptionManager";
const delayMillis = 45000; // 45 seconds
const intervalMillis = 900000; // 15 minutes
2022-12-25 17:59:44 +01:00
class AccountApi {
2022-12-25 19:42:44 +01:00
constructor() {
this.timer = null;
}
2022-12-25 17:59:44 +01:00
async login(user) {
const url = accountTokenUrl(config.baseUrl);
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Checking auth for ${url}`);
2022-12-25 17:59:44 +01:00
const response = await fetch(url, {
method: "POST",
headers: withBasicAuth({}, user.username, user.password)
2022-12-25 17:59:44 +01:00
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
const json = await response.json();
if (!json.token) {
throw new Error(`Unexpected server response: Cannot find token`);
}
return json.token;
}
async logout(token) {
const url = accountTokenUrl(config.baseUrl);
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Logging out from ${url} using token ${token}`);
2022-12-25 17:59:44 +01:00
const response = await fetch(url, {
method: "DELETE",
headers: withBearerAuth({}, token)
2022-12-25 17:59:44 +01:00
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
}
async create(username, password) {
const url = accountUrl(config.baseUrl);
const body = JSON.stringify({
username: username,
password: password
});
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Creating user account ${url}`);
2022-12-25 17:59:44 +01:00
const response = await fetch(url, {
method: "POST",
body: body
});
if (response.status === 409) {
throw new UsernameTakenError(username);
} else if (response.status === 429) {
throw new AccountCreateLimitReachedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
}
async get() {
const url = accountUrl(config.baseUrl);
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Fetching user account ${url}`);
2022-12-25 17:59:44 +01:00
const response = await fetch(url, {
headers: withBearerAuth({}, session.token())
2022-12-25 17:59:44 +01:00
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
const account = await response.json();
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Account`, account);
2022-12-25 17:59:44 +01:00
return account;
}
async delete() {
const url = accountUrl(config.baseUrl);
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Deleting user account ${url}`);
2022-12-25 17:59:44 +01:00
const response = await fetch(url, {
method: "DELETE",
headers: withBearerAuth({}, session.token())
2022-12-25 17:59:44 +01:00
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
}
async changePassword(newPassword) {
const url = accountPasswordUrl(config.baseUrl);
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Changing account password ${url}`);
2022-12-25 17:59:44 +01:00
const response = await fetch(url, {
method: "POST",
headers: withBearerAuth({}, session.token()),
2022-12-25 17:59:44 +01:00
body: JSON.stringify({
password: newPassword
})
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
}
async extendToken() {
const url = accountTokenUrl(config.baseUrl);
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Extending user access token ${url}`);
2022-12-25 17:59:44 +01:00
const response = await fetch(url, {
method: "PATCH",
headers: withBearerAuth({}, session.token())
2022-12-25 17:59:44 +01:00
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
}
async updateSettings(payload) {
const url = accountSettingsUrl(config.baseUrl);
const body = JSON.stringify(payload);
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Updating user account ${url}: ${body}`);
2022-12-25 17:59:44 +01:00
const response = await fetch(url, {
method: "PATCH",
headers: withBearerAuth({}, session.token()),
2022-12-25 17:59:44 +01:00
body: body
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
}
async addSubscription(payload) {
const url = accountSubscriptionUrl(config.baseUrl);
const body = JSON.stringify(payload);
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Adding user subscription ${url}: ${body}`);
2022-12-25 17:59:44 +01:00
const response = await fetch(url, {
method: "POST",
headers: withBearerAuth({}, session.token()),
2022-12-25 17:59:44 +01:00
body: body
});
2022-12-26 04:29:55 +01:00
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
const subscription = await response.json();
console.log(`[AccountApi] Subscription`, subscription);
return subscription;
}
async updateSubscription(remoteId, payload) {
const url = accountSubscriptionSingleUrl(config.baseUrl, remoteId);
const body = JSON.stringify(payload);
console.log(`[AccountApi] Updating user subscription ${url}: ${body}`);
const response = await fetch(url, {
method: "PATCH",
headers: withBearerAuth({}, session.token()),
2022-12-26 04:29:55 +01:00
body: body
});
2022-12-25 17:59:44 +01:00
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
const subscription = await response.json();
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Subscription`, subscription);
2022-12-25 17:59:44 +01:00
return subscription;
}
async deleteSubscription(remoteId) {
const url = accountSubscriptionSingleUrl(config.baseUrl, remoteId);
2022-12-25 19:42:44 +01:00
console.log(`[AccountApi] Removing user subscription ${url}`);
2022-12-25 17:59:44 +01:00
const response = await fetch(url, {
method: "DELETE",
headers: withBearerAuth({}, session.token())
2022-12-25 17:59:44 +01:00
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
} else if (response.status !== 200) {
throw new Error(`Unexpected server response ${response.status}`);
}
}
2022-12-25 19:42:44 +01:00
sync() {
// TODO
}
2022-12-25 19:42:44 +01:00
startWorker() {
if (this.timer !== null) {
return;
}
console.log(`[AccountApi] Starting worker`);
this.timer = setInterval(() => this.runWorker(), intervalMillis);
setTimeout(() => this.runWorker(), delayMillis);
}
async runWorker() {
if (!session.token()) {
return;
}
console.log(`[AccountApi] Extending user access token`);
try {
await this.extendToken();
} catch (e) {
console.log(`[AccountApi] Error extending user access token`, e);
}
}
2022-12-25 17:59:44 +01:00
}
export class UsernameTakenError extends Error {
constructor(username) {
super("Username taken");
this.username = username;
}
}
export class AccountCreateLimitReachedError extends Error {
constructor() {
super("Account creation limit reached");
}
}
export class UnauthorizedError extends Error {
constructor() {
super("Unauthorized");
}
}
const accountApi = new AccountApi();
export default accountApi;