Figure out user manager for account user

This commit is contained in:
binwiederhier 2022-12-26 21:27:07 -05:00
parent 3492558e06
commit 95a8e64fbb
16 changed files with 152 additions and 106 deletions

View file

@ -6,8 +6,8 @@ import {
accountTokenUrl,
accountUrl,
fetchLinesIterator,
maybeWithBasicAuth,
maybeWithBearerAuth,
withBasicAuth,
withBearerAuth,
topicShortUrl,
topicUrl,
topicUrlAuth,
@ -31,7 +31,7 @@ class AccountApi {
console.log(`[AccountApi] Checking auth for ${url}`);
const response = await fetch(url, {
method: "POST",
headers: maybeWithBasicAuth({}, user)
headers: withBasicAuth({}, user.username, user.password)
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
@ -50,7 +50,7 @@ class AccountApi {
console.log(`[AccountApi] Logging out from ${url} using token ${token}`);
const response = await fetch(url, {
method: "DELETE",
headers: maybeWithBearerAuth({}, token)
headers: withBearerAuth({}, token)
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
@ -83,7 +83,7 @@ class AccountApi {
const url = accountUrl(config.baseUrl);
console.log(`[AccountApi] Fetching user account ${url}`);
const response = await fetch(url, {
headers: maybeWithBearerAuth({}, session.token())
headers: withBearerAuth({}, session.token())
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
@ -100,7 +100,7 @@ class AccountApi {
console.log(`[AccountApi] Deleting user account ${url}`);
const response = await fetch(url, {
method: "DELETE",
headers: maybeWithBearerAuth({}, session.token())
headers: withBearerAuth({}, session.token())
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
@ -114,7 +114,7 @@ class AccountApi {
console.log(`[AccountApi] Changing account password ${url}`);
const response = await fetch(url, {
method: "POST",
headers: maybeWithBearerAuth({}, session.token()),
headers: withBearerAuth({}, session.token()),
body: JSON.stringify({
password: newPassword
})
@ -131,7 +131,7 @@ class AccountApi {
console.log(`[AccountApi] Extending user access token ${url}`);
const response = await fetch(url, {
method: "PATCH",
headers: maybeWithBearerAuth({}, session.token())
headers: withBearerAuth({}, session.token())
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
@ -146,7 +146,7 @@ class AccountApi {
console.log(`[AccountApi] Updating user account ${url}: ${body}`);
const response = await fetch(url, {
method: "PATCH",
headers: maybeWithBearerAuth({}, session.token()),
headers: withBearerAuth({}, session.token()),
body: body
});
if (response.status === 401 || response.status === 403) {
@ -162,7 +162,7 @@ class AccountApi {
console.log(`[AccountApi] Adding user subscription ${url}: ${body}`);
const response = await fetch(url, {
method: "POST",
headers: maybeWithBearerAuth({}, session.token()),
headers: withBearerAuth({}, session.token()),
body: body
});
if (response.status === 401 || response.status === 403) {
@ -181,7 +181,7 @@ class AccountApi {
console.log(`[AccountApi] Updating user subscription ${url}: ${body}`);
const response = await fetch(url, {
method: "PATCH",
headers: maybeWithBearerAuth({}, session.token()),
headers: withBearerAuth({}, session.token()),
body: body
});
if (response.status === 401 || response.status === 403) {
@ -199,7 +199,7 @@ class AccountApi {
console.log(`[AccountApi] Removing user subscription ${url}`);
const response = await fetch(url, {
method: "DELETE",
headers: maybeWithBearerAuth({}, session.token())
headers: withBearerAuth({}, session.token())
});
if (response.status === 401 || response.status === 403) {
throw new UnauthorizedError();
@ -208,6 +208,10 @@ class AccountApi {
}
}
sync() {
// TODO
}
startWorker() {
if (this.timer !== null) {
return;

View file

@ -5,9 +5,9 @@ import {
accountSubscriptionUrl,
accountTokenUrl,
accountUrl,
fetchLinesIterator,
maybeWithBasicAuth,
maybeWithBearerAuth,
fetchLinesIterator, maybeWithAuth,
withBasicAuth,
withBearerAuth,
topicShortUrl,
topicUrl,
topicUrlAuth,
@ -24,7 +24,7 @@ class Api {
? topicUrlJsonPollWithSince(baseUrl, topic, since)
: topicUrlJsonPoll(baseUrl, topic);
const messages = [];
const headers = maybeWithBasicAuth({}, user);
const headers = maybeWithAuth({}, user);
console.log(`[Api] Polling ${url}`);
for await (let line of fetchLinesIterator(url, headers)) {
console.log(`[Api, ${shortUrl}] Received message ${line}`);
@ -45,7 +45,7 @@ class Api {
const response = await fetch(baseUrl, {
method: 'PUT',
body: JSON.stringify(body),
headers: maybeWithBasicAuth(headers, user)
headers: maybeWithAuth(headers, user)
});
if (response.status < 200 || response.status > 299) {
throw new Error(`Unexpected response: ${response.status}`);
@ -111,7 +111,7 @@ class Api {
const url = topicUrlAuth(baseUrl, topic);
console.log(`[Api] Checking auth for ${url}`);
const response = await fetch(url, {
headers: maybeWithBasicAuth({}, user)
headers: maybeWithAuth({}, user)
});
if (response.status >= 200 && response.status <= 299) {
return true;

View file

@ -1,4 +1,4 @@
import {basicAuth, encodeBase64Url, topicShortUrl, topicUrlWs} from "./utils";
import {basicAuth, bearerAuth, encodeBase64Url, topicShortUrl, topicUrlWs} from "./utils";
const retryBackoffSeconds = [5, 10, 15, 20, 30];
@ -96,12 +96,18 @@ class Connection {
params.push(`since=${this.since}`);
}
if (this.user) {
const auth = encodeBase64Url(basicAuth(this.user.username, this.user.password));
params.push(`auth=${auth}`);
params.push(`auth=${this.authParam()}`);
}
const wsUrl = topicUrlWs(this.baseUrl, this.topic);
return (params.length === 0) ? wsUrl : `${wsUrl}?${params.join('&')}`;
}
authParam() {
if (this.user.password) {
return encodeBase64Url(basicAuth(this.user.username, this.user.password));
}
return encodeBase64Url(bearerAuth(this.user.token));
}
}
export class ConnectionState {

View file

@ -109,7 +109,7 @@ class ConnectionManager {
const makeConnectionId = async (subscription, user) => {
return (user)
? hashCode(`${subscription.id}|${user.username}|${user.password}`)
? hashCode(`${subscription.id}|${user.username}|${user.password ?? ""}|${user.token ?? ""}`)
: hashCode(`${subscription.id}`);
}

View file

@ -1,21 +1,46 @@
import db from "./db";
import session from "./Session";
class UserManager {
async all() {
return db.users.toArray();
const users = await db.users.toArray();
if (session.exists()) {
users.unshift(this.localUser());
}
return users;
}
async get(baseUrl) {
if (session.exists() && baseUrl === config.baseUrl) {
return this.localUser();
}
return db.users.get(baseUrl);
}
async save(user) {
if (user.baseUrl === config.baseUrl) {
return;
}
await db.users.put(user);
}
async delete(baseUrl) {
if (session.exists() && baseUrl === config.baseUrl) {
return;
}
await db.users.delete(baseUrl);
}
localUser() {
if (!session.exists()) {
return null;
}
return {
baseUrl: config.baseUrl,
username: session.username(),
token: session.token() // Not "password"!
};
}
}
const userManager = new UserManager();

View file

@ -99,17 +99,17 @@ export const unmatchedTags = (tags) => {
else return tags.filter(tag => !(tag in emojis));
}
export const maybeWithBasicAuth = (headers, user) => {
if (user) {
headers['Authorization'] = `Basic ${encodeBase64(`${user.username}:${user.password}`)}`;
export const maybeWithAuth = (headers, user) => {
if (user && user.password) {
return withBasicAuth(headers, user.username, user.password);
} else if (user && user.token) {
return withBearerAuth(headers, user.token);
}
return headers;
}
export const maybeWithBearerAuth = (headers, token) => {
if (token) {
headers['Authorization'] = `Bearer ${token}`;
}
export const withBasicAuth = (headers, username, password) => {
headers['Authorization'] = basicAuth(username, password);
return headers;
}
@ -117,6 +117,15 @@ export const basicAuth = (username, password) => {
return `Basic ${encodeBase64(`${username}:${password}`)}`;
}
export const withBearerAuth = (headers, token) => {
headers['Authorization'] = bearerAuth(token);
return headers;
}
export const bearerAuth = (token) => {
return `Bearer ${token}`;
}
export const encodeBase64 = (s) => {
return Base64.encode(s);
}