commit | author | age
|
6d44a4
|
1 |
/* |
U |
2 |
http-auth - Authentication Classes for 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.security.NoSuchAlgorithmException; |
|
21 |
import java.security.SecureRandom; |
|
22 |
import java.security.spec.InvalidKeySpecException; |
|
23 |
import java.security.spec.KeySpec; |
|
24 |
import java.util.Map; |
|
25 |
import java.util.logging.Level; |
|
26 |
import java.util.logging.Logger; |
|
27 |
import javax.crypto.SecretKeyFactory; |
|
28 |
import javax.crypto.spec.PBEKeySpec; |
|
29 |
|
|
30 |
/** |
|
31 |
* Ein Nutzerverzeichnis, das die Nutzerinformationen im Speicher haelt. |
|
32 |
* |
|
33 |
* @author ulrich |
|
34 |
* @version 1, 02.06.2021 |
|
35 |
*/ |
|
36 |
public class MemoryRealm implements Realm { |
|
37 |
|
|
38 |
private String name; |
|
39 |
private Map users; |
|
40 |
private Map userRoles; |
|
41 |
|
|
42 |
public void setName(String name) { |
|
43 |
this.name = name; |
|
44 |
} |
|
45 |
|
|
46 |
public void addUser(String userId, String password) { |
|
47 |
try { |
|
48 |
User user = new User(); |
|
49 |
user.setName(userId); |
|
50 |
user.setPassword(password); |
|
51 |
byte[] hashBytes = encode(password); |
|
52 |
String hash = new String(hashBytes); |
|
53 |
user.setHash(hash); |
|
54 |
users.put(user.getName(), user); |
|
55 |
} catch (NoSuchAlgorithmException | InvalidKeySpecException ex) { |
|
56 |
Logger.getLogger(MemoryRealm.class.getName()).log(Level.SEVERE, null, ex); |
|
57 |
} |
|
58 |
} |
|
59 |
|
|
60 |
private byte[] encode(String password) throws NoSuchAlgorithmException, InvalidKeySpecException { |
|
61 |
SecureRandom random = new SecureRandom(); |
|
62 |
byte[] salt = new byte[16]; |
|
63 |
random.nextBytes(salt); |
|
64 |
KeySpec spec = new PBEKeySpec(password.toCharArray(), salt, 65536, 128); |
|
65 |
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1"); |
|
66 |
byte[] hash = factory.generateSecret(spec).getEncoded(); |
|
67 |
return hash; |
|
68 |
} |
|
69 |
|
|
70 |
@Override |
|
71 |
public boolean isValid(String nutzerId, String kennwort) { |
|
72 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
|
73 |
} |
|
74 |
|
|
75 |
@Override |
|
76 |
public boolean hasRole(String nutzerId, String rollenId) { |
|
77 |
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. |
|
78 |
} |
|
79 |
|
|
80 |
@Override |
|
81 |
public String getName() { |
|
82 |
return name; |
|
83 |
} |
|
84 |
|
|
85 |
} |