first working version
Signed-off-by: Stephan Richter <s.richter@srsoftware.de>
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
/* © SRSoftware 2024 */
|
||||
package de.srsoftware.oidc.datastore.file; /* © SRSoftware 2024 */
|
||||
import static de.srsoftware.oidc.api.User.*;
|
||||
import static de.srsoftware.utils.Optionals.optional;
|
||||
import static de.srsoftware.utils.Strings.uuid;
|
||||
|
||||
import de.srsoftware.oidc.api.*;
|
||||
import java.io.File;
|
||||
@@ -12,6 +14,8 @@ import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.*;
|
||||
|
||||
import de.srsoftware.utils.Optionals;
|
||||
import org.json.JSONObject;
|
||||
|
||||
public class FileStore implements AuthorizationService, ClientService, SessionService, UserService {
|
||||
@@ -30,6 +34,8 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
|
||||
private final JSONObject json;
|
||||
private final PasswordHasher<String> passwordHasher;
|
||||
private Duration sessionDuration = Duration.of(10, ChronoUnit.MINUTES);
|
||||
private Map<String, Client> clients = new HashMap<>();
|
||||
private Map<String, User> accessTokens = new HashMap<>();
|
||||
|
||||
public FileStore(File storage, PasswordHasher<String> passwordHasher) throws IOException {
|
||||
this.storageFile = storage.toPath();
|
||||
@@ -52,14 +58,26 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*** User Service Methods ***/
|
||||
|
||||
@Override
|
||||
public String accessToken(User user) {
|
||||
var token = uuid();
|
||||
accessTokens.put(token, Objects.requireNonNull(user));
|
||||
return token;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public UserService delete(User user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<User> forToken(String accessToken) {
|
||||
return optional(accessTokens.get(accessToken));
|
||||
}
|
||||
|
||||
@Override
|
||||
public FileStore init(User defaultUser) {
|
||||
@@ -203,8 +221,14 @@ public class FileStore implements AuthorizationService, ClientService, SessionSe
|
||||
|
||||
@Override
|
||||
public Optional<Client> getClient(String clientId) {
|
||||
var clients = json.getJSONObject(CLIENTS);
|
||||
if (clients.has(clientId)) return Optional.of(toClient(clientId, clients.getJSONObject(clientId)));
|
||||
var client = clients.get(clientId);
|
||||
if (client != null) return Optional.of(client);
|
||||
var clientsJson = json.getJSONObject(CLIENTS);
|
||||
if (clientsJson.has(clientId)) {
|
||||
client = toClient(clientId, clientsJson.getJSONObject(clientId));
|
||||
clients.put(clientId, client);
|
||||
return Optional.of(client);
|
||||
}
|
||||
return Optional.empty();
|
||||
}
|
||||
|
||||
|
||||
@@ -2,12 +2,14 @@
|
||||
package de.srsoftware.oidc.datastore.file;
|
||||
|
||||
import static java.lang.System.Logger.Level.ERROR;
|
||||
import static org.jose4j.jwk.JsonWebKey.OutputControlLevel.INCLUDE_PRIVATE;
|
||||
|
||||
import de.srsoftware.oidc.api.KeyManager;
|
||||
import de.srsoftware.oidc.api.KeyStorage;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import org.jose4j.jwk.PublicJsonWebKey;
|
||||
import org.jose4j.lang.JoseException;
|
||||
@@ -15,7 +17,8 @@ import org.jose4j.lang.JoseException;
|
||||
public class PlaintextKeyStore implements KeyStorage {
|
||||
public static System.Logger LOG = System.getLogger(PlaintextKeyStore.class.getSimpleName());
|
||||
|
||||
private final Path dir;
|
||||
private final Path dir;
|
||||
private HashMap<String, PublicJsonWebKey> loaded = new HashMap<>();
|
||||
|
||||
public PlaintextKeyStore(Path storageDir) {
|
||||
this.dir = storageDir;
|
||||
@@ -38,9 +41,13 @@ public class PlaintextKeyStore implements KeyStorage {
|
||||
|
||||
@Override
|
||||
public PublicJsonWebKey load(String keyId) throws IOException, KeyManager.KeyCreationException {
|
||||
var key = loaded.get(keyId);
|
||||
if (key != null) return key;
|
||||
var json = Files.readString(filename(keyId));
|
||||
try {
|
||||
return PublicJsonWebKey.Factory.newPublicJwk(json);
|
||||
key = PublicJsonWebKey.Factory.newPublicJwk(json);
|
||||
loaded.put(keyId, key);
|
||||
return key;
|
||||
} catch (JoseException e) {
|
||||
throw new KeyManager.KeyCreationException(e);
|
||||
}
|
||||
@@ -48,7 +55,7 @@ public class PlaintextKeyStore implements KeyStorage {
|
||||
|
||||
@Override
|
||||
public KeyStorage store(PublicJsonWebKey jsonWebKey) throws IOException {
|
||||
Files.writeString(filename(jsonWebKey.getKeyId()), jsonWebKey.toJson());
|
||||
Files.writeString(filename(jsonWebKey.getKeyId()), jsonWebKey.toJson(INCLUDE_PRIVATE));
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user