commit | author | age
|
696a4b
|
1 |
/* |
U |
2 |
* To change this license header, choose License Headers in Project Properties. |
|
3 |
* To change this template file, choose Tools | Templates |
|
4 |
* and open the template in the editor. |
|
5 |
*/ |
|
6 |
package de.uhilger.httpserver.auth.realm; |
|
7 |
|
|
8 |
import java.io.BufferedReader; |
|
9 |
import java.io.File; |
|
10 |
import java.io.FileReader; |
|
11 |
import java.io.IOException; |
fc54b8
|
12 |
import java.security.InvalidKeyException; |
U |
13 |
import java.security.NoSuchAlgorithmException; |
|
14 |
import java.security.spec.InvalidKeySpecException; |
696a4b
|
15 |
import java.util.ArrayList; |
U |
16 |
import java.util.HashMap; |
|
17 |
import java.util.List; |
|
18 |
import java.util.Map; |
fc54b8
|
19 |
import java.util.logging.Level; |
U |
20 |
import java.util.logging.Logger; |
|
21 |
import javax.crypto.BadPaddingException; |
|
22 |
import javax.crypto.IllegalBlockSizeException; |
|
23 |
import javax.crypto.NoSuchPaddingException; |
696a4b
|
24 |
|
U |
25 |
/** |
|
26 |
* |
fc54b8
|
27 |
* test=test,testRolle |
U |
28 |
* ulrich=ulrich,testRolle,andereRolle |
696a4b
|
29 |
* |
U |
30 |
* @author Ulrich Hilger |
|
31 |
* @version 1, 03.06.2021 |
|
32 |
*/ |
|
33 |
public class SimpleRealm implements Realm { |
fc54b8
|
34 |
|
U |
35 |
private static final Logger logger = Logger.getLogger(SimpleRealm.class.getName()); |
|
36 |
|
696a4b
|
37 |
|
U |
38 |
public static final String LIST_INDICATOR = "="; |
|
39 |
public static final String ROLE_SEPARATOR = ","; |
|
40 |
public static final String COMMENT_INDICATOR = "#"; |
|
41 |
|
|
42 |
private String name; |
|
43 |
private Map<String, User> users; |
|
44 |
private Map<String, List> userRoles; |
|
45 |
|
|
46 |
public SimpleRealm() { |
|
47 |
users = new HashMap<>(); |
|
48 |
userRoles = new HashMap<>(); |
|
49 |
} |
|
50 |
|
|
51 |
public void setName(String name) { |
|
52 |
this.name = name; |
|
53 |
} |
|
54 |
|
|
55 |
public void readFromFile(File file) throws IOException { |
|
56 |
BufferedReader r = new BufferedReader(new FileReader(file)); |
|
57 |
String line = r.readLine(); |
|
58 |
while(line != null) { |
|
59 |
parse(line); |
|
60 |
line = r.readLine(); |
|
61 |
} |
|
62 |
r.close(); |
|
63 |
} |
|
64 |
|
|
65 |
private void parse(String line) { |
|
66 |
if(!line.startsWith(COMMENT_INDICATOR)) { |
|
67 |
String[] teile = line.split(LIST_INDICATOR); |
|
68 |
String[] rollen = teile[1].split(ROLE_SEPARATOR); |
|
69 |
String userId = teile[0]; |
|
70 |
User user = new User(); |
|
71 |
user.setName(userId); |
|
72 |
user.setPassword(rollen[0]); |
fc54b8
|
73 |
try { |
U |
74 |
Encoder encoder = new Encoder(); |
|
75 |
String hex = encoder.bytesToHex(encoder.encode(rollen[0])); |
|
76 |
logger.fine(hex); |
|
77 |
} catch (NoSuchAlgorithmException ex) { |
|
78 |
logger.log(Level.SEVERE, null, ex); |
|
79 |
} |
696a4b
|
80 |
ArrayList rollenListe = new ArrayList(); |
U |
81 |
for(int i = 1; i < rollen.length; i++) { |
|
82 |
rollenListe.add(rollen[i]); |
|
83 |
} |
|
84 |
users.put(userId, user); |
|
85 |
userRoles.put(userId, rollenListe); |
|
86 |
} |
|
87 |
} |
|
88 |
|
|
89 |
/* ------------ Realm implementation -------------- */ |
|
90 |
|
|
91 |
@Override |
|
92 |
public boolean isValid(String userId, String kennwort) { |
|
93 |
Object o = users.get(userId); |
|
94 |
if(o instanceof User) { |
|
95 |
User user = (User) o; |
|
96 |
return user.getPassword().equals(kennwort); |
|
97 |
} else { |
|
98 |
return false; |
|
99 |
} |
|
100 |
} |
|
101 |
|
|
102 |
@Override |
|
103 |
public boolean hasRole(String userId, String rollenId) { |
|
104 |
Object o = userRoles.get(userId); |
|
105 |
if(o instanceof List) { |
|
106 |
List roles = (List) o; |
|
107 |
return roles.contains(rollenId); |
|
108 |
} else { |
|
109 |
return false; |
|
110 |
} |
|
111 |
} |
|
112 |
|
|
113 |
@Override |
|
114 |
public String getName() { |
|
115 |
return name; |
|
116 |
} |
|
117 |
|
|
118 |
} |