mirror of
https://github.com/Komediruzecki/token-circles.git
synced 2026-10-20 09:46:43 +02:00
getProfileId/getProfileIds threw plain Errors carrying a statusCode, but the first error handler only maps AppError, so they surfaced as HTTP 500 instead of 401/403. Throw UnauthorizedError / AppError(403) so the status codes are right. Also normalize a trailing slash in the auth gate so /api/auth/login/ is still treated as public, and remove the now-unused toCamelCase import in portfolio.js.
88 lines
2.9 KiB
JavaScript
88 lines
2.9 KiB
JavaScript
/**
|
|
* Profile helpers — extracted from index.js
|
|
* Provides getProfileId, getProfileIds, profileWhere, profileInClause, profileParams
|
|
*/
|
|
|
|
const { AppError, UnauthorizedError } = require('../lib/errors');
|
|
|
|
// Helper: get profile ID from request (header first, then query param, then 1)
|
|
function getProfileId(req) {
|
|
const id = parseInt(req.headers['x-profile-id'] || req.query.profile_id || 1);
|
|
// Fail closed: require a session and verify the profile belongs to the
|
|
// authenticated user. Previously the ownership check was skipped entirely when
|
|
// no session was present, which let unauthenticated callers read/modify any
|
|
// profile simply by passing its id.
|
|
if (!req.session || !req.session.userId) {
|
|
throw new UnauthorizedError('Unauthorized');
|
|
}
|
|
if (req.repos && req.repos.profiles) {
|
|
const profile = req.repos.profiles.getById(id);
|
|
if (!profile || profile.user_id !== req.session.userId) {
|
|
throw new AppError('Access denied to this profile', 403);
|
|
}
|
|
}
|
|
return id;
|
|
}
|
|
|
|
// Helper: get profile IDs from request (supports JSON array via header)
|
|
function getProfileIds(req) {
|
|
let ids = [];
|
|
const header = req.headers['x-profile-ids'];
|
|
if (header) {
|
|
try {
|
|
const parsed = JSON.parse(header);
|
|
if (Array.isArray(parsed) && parsed.length > 0)
|
|
ids = parsed.map((id) => parseInt(id)).filter((id) => !isNaN(id));
|
|
} catch (e) {
|
|
// single ID fallback
|
|
}
|
|
}
|
|
if (ids.length === 0) {
|
|
const qp = req.query.profile_ids;
|
|
if (qp) {
|
|
ids = String(qp)
|
|
.split(',')
|
|
.map((id) => parseInt(id.trim()))
|
|
.filter((id) => !isNaN(id));
|
|
}
|
|
}
|
|
if (ids.length === 0) {
|
|
ids = [parseInt(req.headers['x-profile-id'] || req.query.profile_id || 1)];
|
|
}
|
|
|
|
// Fail closed: require a session and verify every requested profile belongs to
|
|
// the authenticated user (see getProfileId for rationale).
|
|
if (!req.session || !req.session.userId) {
|
|
throw new UnauthorizedError('Unauthorized');
|
|
}
|
|
if (req.repos && req.repos.profiles) {
|
|
for (const id of ids) {
|
|
const profile = req.repos.profiles.getById(id);
|
|
if (!profile || profile.user_id !== req.session.userId) {
|
|
throw new AppError('Access denied to profile ' + id, 403);
|
|
}
|
|
}
|
|
}
|
|
return ids;
|
|
}
|
|
|
|
// Helper: wrap all data queries with profile_id
|
|
function profileWhere(tableAlias = 't', extra = '') {
|
|
return `${tableAlias}.profile_id = ?${extra ? ' AND ' + extra : ''}`;
|
|
}
|
|
|
|
// Helper: build profile IN clause for multiple profiles
|
|
function profileInClause(tableAlias = 't', extra = '') {
|
|
const placeholder = extra
|
|
? `${tableAlias}.profile_id IN (?) AND ${extra}`
|
|
: `${tableAlias}.profile_id IN (?)`;
|
|
return placeholder;
|
|
}
|
|
|
|
// Helper: wrap query params with profile IDs for IN clause
|
|
function profileParams(pids, extra = []) {
|
|
return [...pids, ...extra];
|
|
}
|
|
|
|
module.exports = { getProfileId, getProfileIds, profileWhere, profileInClause, profileParams };
|