style: run prettier on entire codebase
This commit is contained in:
@@ -1,22 +1,22 @@
|
||||
import { FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { verifyAccessToken } from '../services/auth';
|
||||
import { error } from '../utils/response';
|
||||
import { AuthData } from '../types';
|
||||
import { FastifyRequest, FastifyReply } from "fastify";
|
||||
import { verifyAccessToken } from "../services/auth";
|
||||
import { error } from "../utils/response";
|
||||
import { AuthData } from "../types";
|
||||
|
||||
export async function requireAuth(
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
): Promise<void> {
|
||||
const authHeader = request.headers.authorization;
|
||||
if (!authHeader?.startsWith('Bearer ')) {
|
||||
return error(reply, 'Vyžadována autentizace', 401);
|
||||
if (!authHeader?.startsWith("Bearer ")) {
|
||||
return error(reply, "Vyžadována autentizace", 401);
|
||||
}
|
||||
|
||||
const token = authHeader.slice(7);
|
||||
const authData = await verifyAccessToken(token);
|
||||
|
||||
if (!authData) {
|
||||
return error(reply, 'Neplatný nebo expirovaný token', 401);
|
||||
return error(reply, "Neplatný nebo expirovaný token", 401);
|
||||
}
|
||||
|
||||
request.authData = authData;
|
||||
@@ -27,25 +27,30 @@ export async function optionalAuth(
|
||||
_reply: FastifyReply,
|
||||
): Promise<void> {
|
||||
const authHeader = request.headers.authorization;
|
||||
if (!authHeader?.startsWith('Bearer ')) return;
|
||||
if (!authHeader?.startsWith("Bearer ")) return;
|
||||
|
||||
const token = authHeader.slice(7);
|
||||
request.authData = (await verifyAccessToken(token)) ?? undefined;
|
||||
}
|
||||
|
||||
export function requirePermission(...permissionNames: string[]) {
|
||||
return async (request: FastifyRequest, reply: FastifyReply): Promise<void> => {
|
||||
return async (
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
): Promise<void> => {
|
||||
await requireAuth(request, reply);
|
||||
if (reply.sent) return;
|
||||
|
||||
const authData = request.authData!;
|
||||
|
||||
// Admin has all permissions
|
||||
if (authData.roleName === 'admin') return;
|
||||
if (authData.roleName === "admin") return;
|
||||
|
||||
const hasAll = permissionNames.every((p) => authData.permissions.includes(p));
|
||||
const hasAll = permissionNames.every((p) =>
|
||||
authData.permissions.includes(p),
|
||||
);
|
||||
if (!hasAll) {
|
||||
return error(reply, 'Nedostatečná oprávnění', 403);
|
||||
return error(reply, "Nedostatečná oprávnění", 403);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,19 +1,25 @@
|
||||
import { FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { config } from '../config/env';
|
||||
import { FastifyReply, FastifyRequest } from "fastify";
|
||||
import { config } from "../config/env";
|
||||
|
||||
export async function securityHeaders(
|
||||
_request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
): Promise<void> {
|
||||
reply.header('X-Content-Type-Options', 'nosniff');
|
||||
reply.header('X-Frame-Options', 'DENY');
|
||||
reply.header('Referrer-Policy', 'strict-origin-when-cross-origin');
|
||||
reply.header('Permissions-Policy', 'camera=(), microphone=(), geolocation=(self)');
|
||||
reply.header("X-Content-Type-Options", "nosniff");
|
||||
reply.header("X-Frame-Options", "DENY");
|
||||
reply.header("Referrer-Policy", "strict-origin-when-cross-origin");
|
||||
reply.header(
|
||||
"Permissions-Policy",
|
||||
"camera=(), microphone=(), geolocation=(self)",
|
||||
);
|
||||
|
||||
if (config.isProduction) {
|
||||
reply.header('Strict-Transport-Security', 'max-age=31536000; includeSubDomains');
|
||||
reply.header(
|
||||
'Content-Security-Policy',
|
||||
"Strict-Transport-Security",
|
||||
"max-age=31536000; includeSubDomains",
|
||||
);
|
||||
reply.header(
|
||||
"Content-Security-Policy",
|
||||
[
|
||||
"default-src 'self'",
|
||||
"script-src 'self' https://unpkg.com",
|
||||
@@ -21,7 +27,7 @@ export async function securityHeaders(
|
||||
"font-src 'self' https://fonts.gstatic.com",
|
||||
"img-src 'self' data: blob: https://*.tile.openstreetmap.org",
|
||||
"connect-src 'self' https://nominatim.openstreetmap.org",
|
||||
].join('; '),
|
||||
].join("; "),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user