/*
http-auth - Authentication Extensions to jdk.httpserver
Copyright (C) 2021 Ulrich Hilger
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
*/
package de.uhilger.httpserver.auth.realm;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Eine einfache Implementierung der Schnittstelle Realm, die
* Benutzerinformationen aus einer Datei liest. Die Datei ist dabei
* wie folgt aufgebaut.
*
* test=test,testRolle
* ulrich=ulrich,testRolle,andereRolle
*
* Der erste Eintrag nach dem Gleichheitszeichen ist das Passwort, die restlichen
* Eintrage sind Rollen.
*
* @author Ulrich Hilger
* @version 1, 03.06.2021
*/
public class SimpleRealm implements Realm {
private static final Logger logger = Logger.getLogger(SimpleRealm.class.getName());
public static final String LIST_INDICATOR = "=";
public static final String ROLE_SEPARATOR = ",";
public static final String COMMENT_INDICATOR = "#";
private String name;
private final Map users;
private final Map userRoles;
public SimpleRealm() {
users = new HashMap<>();
userRoles = new HashMap<>();
}
public void setName(String name) {
this.name = name;
}
public void readFromFile(File file) throws IOException {
BufferedReader r = new BufferedReader(new FileReader(file));
String line = r.readLine();
while(line != null) {
parse(line);
line = r.readLine();
}
r.close();
}
private void parse(String line) {
if(!line.startsWith(COMMENT_INDICATOR)) {
String[] teile = line.split(LIST_INDICATOR);
String[] rollen = teile[1].split(ROLE_SEPARATOR);
String userId = teile[0];
User user = new User();
user.setName(userId);
user.setPassword(rollen[0]);
try {
Encoder encoder = new Encoder();
String hex = encoder.bytesToHex(encoder.encode(rollen[0]));
logger.fine(hex);
} catch (NoSuchAlgorithmException ex) {
logger.log(Level.SEVERE, null, ex);
}
ArrayList rollenListe = new ArrayList();
for(int i = 1; i < rollen.length; i++) {
rollenListe.add(rollen[i]);
}
users.put(userId, user);
userRoles.put(userId, rollenListe);
}
}
/* ------------ Realm implementation -------------- */
@Override
public boolean isValid(String userId, String kennwort) {
Object o = users.get(userId);
if(o instanceof User) {
User user = (User) o;
return user.getPassword().equals(kennwort);
} else {
return false;
}
}
@Override
public boolean hasRole(String userId, String rollenId) {
Object o = userRoles.get(userId);
if(o instanceof List) {
List roles = (List) o;
return roles.contains(rollenId);
} else {
return false;
}
}
@Override
public String getName() {
return name;
}
}