-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtester.ts
More file actions
80 lines (67 loc) · 2.4 KB
/
tester.ts
File metadata and controls
80 lines (67 loc) · 2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import jwt from "jsonwebtoken";
import { isTester as checkIsTester, getTesterSettings } from '../db/testers.js';
import { Request, Response, NextFunction } from "express";
import type { JwtPayload } from "../types/JwtPayload.js";
const JWT_SECRET = process.env.JWT_SECRET;
function shouldBypassTesterGate(req: Request): boolean {
const host = req.get('host') || req.get('x-forwarded-host') || '';
return host === 'control.pfconnect.online';
}
export async function requireTester(req: Request, res: Response, next: NextFunction) {
try {
if (shouldBypassTesterGate(req)) {
return next();
}
const settings = await getTesterSettings();
if (!settings.tester_gate_enabled) {
return next();
}
const token = req.cookies.auth_token;
if (!token) {
return res.status(401).json({ error: "Authentication required" });
}
if (!JWT_SECRET) {
console.error('JWT_SECRET is not defined');
return res.status(500).json({ error: "Server configuration error" });
}
const decoded = jwt.verify(token, JWT_SECRET as string) as JwtPayload;
const userIsTester = await checkIsTester(decoded.userId);
if (!userIsTester) {
return res.status(403).json({ error: "Tester access required" });
}
req.user = decoded;
next();
} catch (err) {
console.error('Tester auth error:', err);
return res.status(401).json({ error: "Invalid token" });
}
}
export async function isTester(userId: string) {
try {
return await checkIsTester(userId);
} catch (error) {
console.error('Error checking tester status:', error);
return false;
}
}
export async function checkTesterGateStatus() {
try {
const settings = await getTesterSettings();
return settings.tester_gate_enabled || false;
} catch (error) {
console.error('Error checking tester gate status:', error);
return true;
}
}
export async function checkTesterGateStatusWithDomain(host?: string) {
try {
if (host === 'control.pfconnect.online') {
return false;
}
const settings = await getTesterSettings();
return settings.tester_gate_enabled || false;
} catch (error) {
console.error('Error checking tester gate status:', error);
return true;
}
}