Začištění použití remote-user hlavičky

This commit is contained in:
Martin Berka 2023-06-29 07:33:05 +02:00
parent c68141575f
commit 4c9a868d6b
2 changed files with 16 additions and 12 deletions

View File

@ -6,13 +6,16 @@ import jwt from 'jsonwebtoken';
* @param login přihlašovací jméno uživatele
* @returns JWT token
*/
export function generateToken(login: string): string {
export function generateToken(login?: string): string {
if (!process.env.JWT_SECRET) {
throw Error("Není vyplněna proměnná prostředí JWT_SECRET");
}
if (process.env.JWT_SECRET.length < 32) {
throw Error("Proměnná prostředí JWT_SECRET musí být minimálně 32 znaků");
}
if (!login || login.trim().length === 0) {
throw Error("Nebyl předán login");
}
return jwt.sign({ login }, process.env.JWT_SECRET);
}

View File

@ -43,17 +43,18 @@ app.get("/api/whoami",(req,res)=>{
})
app.post("/api/login", (req, res) => {
if (!req.body?.login) {
// Autentizace pomocí trusted headers
const remoteUser = req.header('remote-user');
if (remoteUser && remoteUser.length > 0) {
res.status(200).json(generateToken(remoteUser));
return;
}
// Klasická autentizace loginem
if (!req.body?.login || req.body.login.trim().length === 0) {
throw Error("Nebyl předán login");
}
// TODO: je tohle hnusny?... bude to fungovat? lol
if (req.header('remote-user')){
let username = req.header('remote-user') || "jmenonemahlavicku"
res.status(200).json(generateToken(username));
} else {
// TODO zavést podmínky pro délku loginu (min i max)
res.status(200).json(generateToken(req.body.login));
}
});
// ----------------------------------------------------