commit | author | age
|
9ee357
|
1 |
/* |
U |
2 |
jwtTest - JSON Web Token Testimplementierung |
|
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.util.ArrayList; |
|
21 |
import java.util.HashMap; |
|
22 |
import java.util.List; |
|
23 |
import java.util.Map; |
|
24 |
|
|
25 |
/** |
|
26 |
* |
|
27 |
* @author Ulrich Hilger |
|
28 |
* @version 1, 22.05.2021 |
|
29 |
*/ |
|
30 |
public class TestRealm implements Realm { |
|
31 |
|
|
32 |
private String name = "Testverzeichnis"; |
|
33 |
|
|
34 |
private Map nutzerMap; |
|
35 |
|
|
36 |
private Map rollenMap; |
|
37 |
|
|
38 |
public TestRealm() { |
|
39 |
nutzerMap = new HashMap(); |
|
40 |
User nutzer = new User(); |
|
41 |
nutzer.setName("test"); |
|
42 |
nutzer.setPassword("test"); |
|
43 |
nutzerMap.put(nutzer.getName(), nutzer); |
|
44 |
|
|
45 |
rollenMap = new HashMap(); |
|
46 |
List nutzerRollen = new ArrayList(); |
|
47 |
nutzerRollen.add("testRolle"); |
|
48 |
rollenMap.put(nutzer.getName(), nutzerRollen); |
|
49 |
} |
|
50 |
|
|
51 |
/** |
|
52 |
* Uberpruefen, ob die Benutzerkennung und das Kennwort gueltig sind. |
|
53 |
* |
|
54 |
* @param nutzerId |
|
55 |
* @param kennwort |
|
56 |
* @return |
|
57 |
*/ |
|
58 |
public boolean isValid(String nutzerId, String kennwort) { |
|
59 |
Object o = nutzerMap.get(nutzerId); |
|
60 |
if(o instanceof User) { |
|
61 |
User nutzer = (User) o; |
|
62 |
if(nutzer.getPassword().equals(kennwort)) { |
|
63 |
return true; |
|
64 |
} else { |
|
65 |
return false; |
|
66 |
} |
|
67 |
} else { |
|
68 |
return false; |
|
69 |
} |
|
70 |
} |
|
71 |
|
|
72 |
public String getName() { |
|
73 |
return name; |
|
74 |
} |
|
75 |
|
|
76 |
public void setName(String name) { |
|
77 |
this.name = name; |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public boolean hasRole(String nutzerId, String rollenId) { |
|
82 |
Object o = rollenMap.get(nutzerId); |
|
83 |
if(o instanceof List) { |
|
84 |
List nutzerRollen = (List) o; |
|
85 |
return nutzerRollen.contains(rollenId); |
|
86 |
} else { |
|
87 |
return false; |
|
88 |
} |
|
89 |
} |
|
90 |
} |