Implemented public salt getting

This commit is contained in:
WardPearce
2026-02-14 00:01:16 +13:00
parent 7a159948b3
commit c2156f2cac
7 changed files with 40 additions and 30 deletions
+1 -2
View File
@@ -1,11 +1,10 @@
import type { User } from '$lib/backendOnly/user';
import type { IGetChallengeResponse } from 'youtubei.js';
declare global {
namespace App {
// interface Error {}
interface Locals {
userBackend?: User;
userId: string;
}
// interface PageData {}
// interface PageState {}
+1 -2
View File
@@ -2,7 +2,6 @@ import { isOwnBackend } from '$lib/backend';
import { sequelize } from '$lib/backendOnly/database';
import { unsign } from 'cookie-signature';
import { env } from '$env/dynamic/private';
import { User } from '$lib/backendOnly/user';
let sequelizeAuthenticated = false;
export async function handle({ event, resolve }) {
@@ -23,7 +22,7 @@ export async function handle({ event, resolve }) {
if (signedUserId) {
const userId = unsign(signedUserId, env.COOKIE_SECRET);
if (userId) {
event.locals.userBackend = new User(userId);
event.locals.userId = userId;
}
}
+2
View File
@@ -15,3 +15,5 @@ export function isOwnBackend(): IsOwnBackend | null {
registrationAllowed: import.meta.env.VITE_REGISTRATION_ALLOWED === 'true'
};
}
async function backendLogin(username: string, password: string) {}
+2 -2
View File
@@ -12,7 +12,7 @@ export interface UserTableModel extends Model {
passwordHash: string;
passwordSalt: string;
created: Date;
subscriptionPasswordHash: string;
subscriptionPasswordSalt: string;
}
export const UserTable = sequelize.define('User', {
@@ -38,7 +38,7 @@ export const UserTable = sequelize.define('User', {
type: DataTypes.DATE,
allowNull: false
},
subscriptionPasswordHash: {
subscriptionPasswordSalt: {
type: DataTypes.STRING,
allowNull: false
}
+26 -16
View File
@@ -1,22 +1,30 @@
import { UserTable, type ChannelSubscriptionModel, type UserTableModel } from './database';
import { Op } from 'sequelize';
import crypto from 'crypto';
import { error } from '@sveltejs/kit';
export class User {
private userId: string;
private user: UserTableModel;
constructor(id: string) {
this.userId = id;
constructor(user: UserTableModel) {
this.user = user;
}
public get id() {
return this.userId;
return this.user.id;
}
public get publicPasswordSalts() {
return {
subscriptionPasswordSalt: this.user.subscriptionPasswordSalt,
passwordSalt: this.user.passwordSalt
};
}
private get userWhere() {
return {
where: {
[Op.or]: [{ id: this.userId }, { username: this.userId }]
[Op.or]: [{ id: this.user.id }, { username: this.user.username }]
}
};
}
@@ -28,7 +36,7 @@ export class User {
async subscriptions(): Promise<ChannelSubscriptionModel[]> {
const subscriptions = await UserTable.findAll({
where: {
userId: this.userId
userId: this.user.id
}
});
@@ -44,22 +52,24 @@ export type CreateUser = {
hash: string;
salt: string;
};
subscriptionPasswordHash: string;
subscriptionPasswordSalt: string;
};
export async function createUser(user: CreateUser): Promise<User> {
const id = crypto.randomUUID();
await UserTable.create({
const createdUser = {
id,
username: user.username,
passwordHash: user.password.hash,
passwordSalt: user.password.salt,
created: new Date(),
subscriptionPasswordHash: user.subscriptionPasswordHash
});
subscriptionPasswordSalt: user.subscriptionPasswordSalt
};
return new User(id);
await UserTable.create(createdUser);
return new User(createdUser as UserTableModel);
}
export async function getUser(identifier: string): Promise<User> {
@@ -70,10 +80,10 @@ export async function getUser(identifier: string): Promise<User> {
});
if (!user) {
throw new Error('User does not exist');
throw error(404);
}
return new User((user as UserTableModel).id);
return new User(user as UserTableModel);
}
export async function authenticateUser(username: string, passwordHash: string): Promise<User> {
@@ -84,7 +94,7 @@ export async function authenticateUser(username: string, passwordHash: string):
});
if (!user) {
throw new Error('User does not exist');
throw error(404);
}
const userModel = user as UserTableModel;
@@ -99,8 +109,8 @@ export async function authenticateUser(username: string, passwordHash: string):
textEncoder.encode(userModel.passwordHash)
)
) {
return new User(userModel.id);
return new User(userModel as UserTableModel);
}
throw new Error('User does not exist');
throw error(404);
}
@@ -0,0 +1,7 @@
import { getUser } from '$lib/backendOnly/user';
import { json } from '@sveltejs/kit';
export async function GET({ locals }) {
const user = await getUser(locals.userId);
return json(user.publicPasswordSalts);
}
@@ -14,14 +14,7 @@ export async function POST({ request, cookies }) {
if (!userLogin.success) throw error(401);
let userModel: User | undefined;
try {
userModel = await authenticateUser(userLogin.data.username, userLogin.data.passwordHash);
} catch {
// Handle outside of catch
}
if (!userModel) throw error(401);
const userModel = await authenticateUser(userLogin.data.username, userLogin.data.passwordHash);
cookies.set('userid', sign(userModel.id, env.COOKIE_SECRET), {
httpOnly: true,