commit | author | age
|
2a4fb8
|
1 |
/* |
U |
2 |
http-realm - Authentication Extensions to jdk.httpserver |
|
3 |
Copyright (C) 2021 Ulrich Hilger |
|
4 |
|
|
5 |
This program is free software: you can redistribute it and/or modify |
|
6 |
it under the terms of the GNU Affero General Public License as |
|
7 |
published by the Free Software Foundation, either version 3 of the |
|
8 |
License, or (at your option) any later version. |
|
9 |
|
|
10 |
This program is distributed in the hope that it will be useful, |
|
11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 |
GNU Affero General Public License for more details. |
|
14 |
|
|
15 |
You should have received a copy of the GNU Affero General Public License |
|
16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
17 |
*/ |
|
18 |
package de.uhilger.httpserver.auth.realm; |
|
19 |
|
|
20 |
import java.io.BufferedReader; |
|
21 |
import java.io.File; |
|
22 |
import java.io.FileReader; |
|
23 |
import java.io.IOException; |
|
24 |
import java.security.NoSuchAlgorithmException; |
|
25 |
import java.util.ArrayList; |
|
26 |
import java.util.HashMap; |
|
27 |
import java.util.List; |
|
28 |
import java.util.Map; |
|
29 |
import java.util.logging.Level; |
|
30 |
import java.util.logging.Logger; |
|
31 |
|
|
32 |
/** |
|
33 |
* Eine einfache Implementierung der Schnittstelle Realm, die |
|
34 |
* Benutzerinformationen aus einer Datei liest. Die Datei ist dabei |
|
35 |
* wie folgt aufgebaut. |
|
36 |
* |
|
37 |
* test=test,testRolle |
|
38 |
* ulrich=ulrich,testRolle,andereRolle |
|
39 |
* |
|
40 |
* Der erste Eintrag nach dem Gleichheitszeichen ist das Passwort, die restlichen |
|
41 |
* Eintrage sind Rollen. |
|
42 |
* |
|
43 |
* @author Ulrich Hilger |
|
44 |
* @version 1, 03.06.2021 |
|
45 |
*/ |
|
46 |
public class SimpleRealm implements Realm { |
|
47 |
|
|
48 |
private static final Logger logger = Logger.getLogger(SimpleRealm.class.getName()); |
|
49 |
|
|
50 |
public static final String LIST_INDICATOR = "="; |
|
51 |
public static final String ROLE_SEPARATOR = ","; |
|
52 |
public static final String COMMENT_INDICATOR = "#"; |
|
53 |
|
|
54 |
private String name; |
|
55 |
private final Map<String, User> users; |
|
56 |
private final Map<String, List> userRoles; |
|
57 |
|
|
58 |
public SimpleRealm() { |
|
59 |
users = new HashMap<>(); |
|
60 |
userRoles = new HashMap<>(); |
|
61 |
} |
|
62 |
|
|
63 |
public void setName(String name) { |
|
64 |
this.name = name; |
|
65 |
} |
|
66 |
|
|
67 |
public void readFromFile(File file) throws IOException { |
|
68 |
BufferedReader r = new BufferedReader(new FileReader(file)); |
|
69 |
String line = r.readLine(); |
|
70 |
while(line != null) { |
|
71 |
parse(line); |
|
72 |
line = r.readLine(); |
|
73 |
} |
|
74 |
r.close(); |
|
75 |
} |
|
76 |
|
|
77 |
private void parse(String line) { |
|
78 |
if(!line.startsWith(COMMENT_INDICATOR)) { |
|
79 |
String[] teile = line.split(LIST_INDICATOR); |
|
80 |
String[] rollen = teile[1].split(ROLE_SEPARATOR); |
|
81 |
String userId = teile[0]; |
|
82 |
User user = new User(); |
|
83 |
user.setName(userId); |
|
84 |
user.setPassword(rollen[0]); |
|
85 |
try { |
|
86 |
Encoder encoder = new Encoder(); |
|
87 |
String hex = encoder.bytesToHex(encoder.encode(rollen[0])); |
|
88 |
logger.fine(hex); |
|
89 |
} catch (NoSuchAlgorithmException ex) { |
|
90 |
logger.log(Level.SEVERE, null, ex); |
|
91 |
} |
|
92 |
ArrayList rollenListe = new ArrayList(); |
|
93 |
for(int i = 1; i < rollen.length; i++) { |
|
94 |
rollenListe.add(rollen[i]); |
|
95 |
} |
|
96 |
users.put(userId, user); |
|
97 |
userRoles.put(userId, rollenListe); |
|
98 |
} |
|
99 |
} |
|
100 |
|
|
101 |
/* ------------ Realm implementation -------------- */ |
|
102 |
|
|
103 |
@Override |
|
104 |
public boolean isValid(String userId, String kennwort) { |
|
105 |
Object o = users.get(userId); |
|
106 |
if(o instanceof User) { |
|
107 |
User user = (User) o; |
|
108 |
return user.getPassword().equals(kennwort); |
|
109 |
} else { |
|
110 |
return false; |
|
111 |
} |
|
112 |
} |
|
113 |
|
|
114 |
@Override |
|
115 |
public boolean hasRole(String userId, String rollenId) { |
|
116 |
Object o = userRoles.get(userId); |
|
117 |
if(o instanceof List) { |
|
118 |
List roles = (List) o; |
|
119 |
return roles.contains(rollenId); |
|
120 |
} else { |
|
121 |
return false; |
|
122 |
} |
|
123 |
} |
|
124 |
|
|
125 |
@Override |
|
126 |
public String getName() { |
|
127 |
return name; |
|
128 |
} |
|
129 |
|
|
130 |
} |