doufam ze jsem to hodne rozjebal lol
This commit is contained in:
parent
f85d19bbd6
commit
44de01f6eb
@ -31,26 +31,48 @@ app.use(cors({
|
||||
origin: '*'
|
||||
}));
|
||||
|
||||
// Zapínatelný login přes hlavičky - pokud je zapnutý nepovolí "basicauth"
|
||||
const HTTP_REMOTE_USER_ENABLED = process.env.HTTP_REMOTE_USER_ENABLED || false;
|
||||
const HTTP_REMOTE_USER_HEADER_NAME = process.env.HTTP_REMOTE_USER_HEADER_NAME || 'remote-user';
|
||||
let HTTP_REMOTE_TRUSTED_IPS = ['127.0.0.1']
|
||||
if (HTTP_REMOTE_USER_ENABLED) {
|
||||
if (!process.env.HTTP_REMOTE_TRUSTED_IPS) {
|
||||
throw new Error('Zapnutý login z hlaviček, ale není nastaven rozsah adres z kterých hlavička může přijít.');
|
||||
}
|
||||
HTTP_REMOTE_TRUSTED_IPS = process.env.HTTP_REMOTE_TRUSTED_IPS.split(',');
|
||||
//TODO: nevim jak udelat console.log pouze pro "debug"
|
||||
//console.log("Budu věřit hlavičkám z: " + HTTP_REMOTE_TRUSTED_IPS);
|
||||
app.set('trust proxy', HTTP_REMOTE_TRUSTED_IPS);
|
||||
app.enable('trust proxy');
|
||||
console.log('Zapnutý login přes hlaviček z proxy.');
|
||||
}
|
||||
|
||||
|
||||
// ----------- Metody nevyžadující token --------------
|
||||
|
||||
app.get("/api/whoami", (req, res) => {
|
||||
res.send(req.header('remote-user'));
|
||||
if (!HTTP_REMOTE_USER_ENABLED) {
|
||||
res.status(403).json({ error: 'Není zapnuté přihlášení z hlaviček' });
|
||||
}
|
||||
res.send(req.header(HTTP_REMOTE_USER_HEADER_NAME));
|
||||
})
|
||||
|
||||
app.post("/api/login", (req, res) => {
|
||||
if (HTTP_REMOTE_USER_ENABLED) { // je rovno app.enabled('trust proxy')
|
||||
// Autentizace pomocí trusted headers
|
||||
const remoteUser = req.header('remote-user');
|
||||
const remoteUser = req.header(HTTP_REMOTE_USER_HEADER_NAME);
|
||||
const remoteName = req.header('remote-name');
|
||||
if (remoteUser && remoteUser.length > 0 && remoteName && remoteName.length > 0) {
|
||||
res.status(200).json(generateToken(Buffer.from(remoteName, 'latin1').toString(), true));
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
// Klasická autentizace loginem
|
||||
if (!req.body?.login || req.body.login.trim().length === 0) {
|
||||
throw Error("Nebyl předán login");
|
||||
}
|
||||
// TODO zavést podmínky pro délku loginu (min i max)
|
||||
res.status(200).json(generateToken(req.body.login, false));
|
||||
}
|
||||
});
|
||||
|
||||
// TODO dočasné řešení - QR se zobrazuje přes <img>, nemáme sem jak dostat token
|
||||
@ -71,13 +93,15 @@ app.get("/api/qr", (req, res) => {
|
||||
|
||||
/** Middleware ověřující JWT token */
|
||||
app.use("/api/", (req, res, next) => {
|
||||
const userHeader = req.header('remote-user');
|
||||
if (HTTP_REMOTE_USER_ENABLED) {
|
||||
const userHeader = req.header(HTTP_REMOTE_USER_HEADER_NAME);
|
||||
const nameHeader = req.header('remote-name');
|
||||
const emailHeader = req.header('remote-email');
|
||||
if (userHeader !== undefined && nameHeader !== undefined) {
|
||||
const remoteName = Buffer.from(nameHeader, 'latin1').toString();
|
||||
console.log("Tvuj username, name a email: %s, %s, %s.", userHeader, remoteName, emailHeader);
|
||||
}
|
||||
}
|
||||
if (!req.headers.authorization) {
|
||||
return res.status(401).json({ error: 'Nebyl předán autentizační token' });
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user