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; |
|
12 |
import java.util.ArrayList; |
|
13 |
import java.util.Arrays; |
|
14 |
import java.util.HashMap; |
|
15 |
import java.util.List; |
|
16 |
import java.util.Map; |
|
17 |
import java.util.Set; |
|
18 |
|
|
19 |
/** |
|
20 |
* |
|
21 |
* test=testRolle |
|
22 |
* ulrich=testRolle,andereRolle |
|
23 |
* |
|
24 |
* @author Ulrich Hilger |
|
25 |
* @version 1, 03.06.2021 |
|
26 |
*/ |
|
27 |
public class SimpleRealm implements Realm { |
|
28 |
|
|
29 |
public static final String LIST_INDICATOR = "="; |
|
30 |
public static final String ROLE_SEPARATOR = ","; |
|
31 |
public static final String COMMENT_INDICATOR = "#"; |
|
32 |
|
|
33 |
private String name; |
|
34 |
private Map<String, User> users; |
|
35 |
private Map<String, List> userRoles; |
|
36 |
|
|
37 |
public SimpleRealm() { |
|
38 |
users = new HashMap<>(); |
|
39 |
userRoles = new HashMap<>(); |
|
40 |
} |
|
41 |
|
|
42 |
public void setName(String name) { |
|
43 |
this.name = name; |
|
44 |
} |
|
45 |
|
|
46 |
public void readFromFile(File file) throws IOException { |
|
47 |
BufferedReader r = new BufferedReader(new FileReader(file)); |
|
48 |
String line = r.readLine(); |
|
49 |
while(line != null) { |
|
50 |
parse(line); |
|
51 |
line = r.readLine(); |
|
52 |
} |
|
53 |
r.close(); |
|
54 |
} |
|
55 |
|
|
56 |
private void parse(String line) { |
|
57 |
if(!line.startsWith(COMMENT_INDICATOR)) { |
|
58 |
String[] teile = line.split(LIST_INDICATOR); |
|
59 |
String[] rollen = teile[1].split(ROLE_SEPARATOR); |
|
60 |
String userId = teile[0]; |
|
61 |
User user = new User(); |
|
62 |
user.setName(userId); |
|
63 |
user.setPassword(rollen[0]); |
|
64 |
ArrayList rollenListe = new ArrayList(); |
|
65 |
for(int i = 1; i < rollen.length; i++) { |
|
66 |
rollenListe.add(rollen[i]); |
|
67 |
} |
|
68 |
users.put(userId, user); |
|
69 |
userRoles.put(userId, rollenListe); |
|
70 |
} |
|
71 |
} |
|
72 |
|
|
73 |
/* ------------ Realm implementation -------------- */ |
|
74 |
|
|
75 |
@Override |
|
76 |
public boolean isValid(String userId, String kennwort) { |
|
77 |
Object o = users.get(userId); |
|
78 |
if(o instanceof User) { |
|
79 |
User user = (User) o; |
|
80 |
return user.getPassword().equals(kennwort); |
|
81 |
} else { |
|
82 |
return false; |
|
83 |
} |
|
84 |
} |
|
85 |
|
|
86 |
@Override |
|
87 |
public boolean hasRole(String userId, String rollenId) { |
|
88 |
Object o = userRoles.get(userId); |
|
89 |
if(o instanceof List) { |
|
90 |
List roles = (List) o; |
|
91 |
return roles.contains(rollenId); |
|
92 |
} else { |
|
93 |
return false; |
|
94 |
} |
|
95 |
} |
|
96 |
|
|
97 |
@Override |
|
98 |
public String getName() { |
|
99 |
return name; |
|
100 |
} |
|
101 |
|
|
102 |
} |