37 lines
968 B
TypeScript
37 lines
968 B
TypeScript
/**
|
|
* Authentifizierungsbibliothek
|
|
* Benutzer via Umgebungsvariable konfigurieren:
|
|
* AUTH_USERS=user1:passwort1,user2:passwort2
|
|
*/
|
|
|
|
export interface User {
|
|
username: string;
|
|
password: string;
|
|
}
|
|
|
|
export function getUsers(): User[] {
|
|
const usersString = process.env.AUTH_USERS || '';
|
|
if (!usersString) {
|
|
console.warn('AUTH_USERS nicht in .env konfiguriert');
|
|
return [];
|
|
}
|
|
return usersString
|
|
.split(',')
|
|
.map((userPair) => {
|
|
const [username, password] = userPair.trim().split(':');
|
|
return { username: username?.trim(), password: password?.trim() };
|
|
})
|
|
.filter((user) => user.username && user.password);
|
|
}
|
|
|
|
export function verifyCredentials(username: string, password: string): boolean {
|
|
const users = getUsers();
|
|
const user = users.find(u => u.username === username);
|
|
if (!user) return false;
|
|
return user.password === password;
|
|
}
|
|
|
|
export function isAuthEnabled(): boolean {
|
|
return !!process.env.AUTH_USERS;
|
|
}
|