Initial commit
This commit is contained in:
38
src/nodash/core/spheres/NoByteSetSphere.java
Normal file
38
src/nodash/core/spheres/NoByteSetSphere.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package nodash.core.spheres;
|
||||
|
||||
import java.security.PublicKey;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import nodash.models.NoByteSet;
|
||||
import nodash.models.NoUser;
|
||||
|
||||
public final class NoByteSetSphere {
|
||||
private static final ArrayList<NoByteSet> EMPTY_BYTESET_LIST = new ArrayList<NoByteSet>(0);
|
||||
|
||||
private static ConcurrentHashMap<PublicKey, ArrayList<NoByteSet>> byteSets = new ConcurrentHashMap<PublicKey, ArrayList<NoByteSet>>();
|
||||
|
||||
public static void add(NoByteSet byteSet, PublicKey publicKey) {
|
||||
if (!NoByteSetSphere.byteSets.containsKey(publicKey)) {
|
||||
NoByteSetSphere.byteSets.put(publicKey, new ArrayList<NoByteSet>());
|
||||
}
|
||||
NoByteSetSphere.byteSets.get(publicKey).add(byteSet);
|
||||
}
|
||||
|
||||
public static void addList(ArrayList<NoByteSet> byteSetList, PublicKey publicKey) {
|
||||
if (!NoByteSetSphere.byteSets.containsKey(publicKey)) {
|
||||
NoByteSetSphere.byteSets.put(publicKey, new ArrayList<NoByteSet>());
|
||||
}
|
||||
NoByteSetSphere.byteSets.get(publicKey).addAll(byteSetList);
|
||||
}
|
||||
|
||||
public static ArrayList<NoByteSet> consume(NoUser user) {
|
||||
if (NoByteSetSphere.byteSets.containsKey(user.getRSAPublicKey())) {
|
||||
ArrayList<NoByteSet> result = NoByteSetSphere.byteSets.get(user.getRSAPublicKey());
|
||||
NoByteSetSphere.byteSets.remove(user.getRSAPublicKey());
|
||||
return result;
|
||||
} else {
|
||||
return NoByteSetSphere.EMPTY_BYTESET_LIST;
|
||||
}
|
||||
}
|
||||
}
|
||||
77
src/nodash/core/spheres/NoHashSphere.java
Normal file
77
src/nodash/core/spheres/NoHashSphere.java
Normal file
@@ -0,0 +1,77 @@
|
||||
package nodash.core.spheres;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import nodash.core.NoCore;
|
||||
import nodash.exceptions.NoDashFatalException;
|
||||
import nodash.models.NoUser;
|
||||
|
||||
public final class NoHashSphere {
|
||||
private static Set<String> database = Collections.newSetFromMap(new ConcurrentHashMap<String, Boolean>());
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static void setup() {
|
||||
if (NoCore.config.saveDatabase) {
|
||||
File file = new File(NoCore.config.databaseFilename);
|
||||
if (file.exists()) {
|
||||
try {
|
||||
byte[] data = Files.readAllBytes(file.toPath());
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(data);
|
||||
ObjectInputStream ois = new ObjectInputStream(bais);
|
||||
NoHashSphere.database = (Set<String>) ois.readObject();
|
||||
ois.close();
|
||||
bais.close();
|
||||
} catch (IOException e){
|
||||
throw new NoDashFatalException("Unable to load up given database file.");
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new NoDashFatalException("Database file not in a verifiable format.");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void saveToFile() throws IOException {
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
ObjectOutputStream oos = new ObjectOutputStream(baos);
|
||||
oos.writeObject(NoHashSphere.database);
|
||||
byte[] data = baos.toByteArray();
|
||||
oos.close();
|
||||
baos.close();
|
||||
File file = new File(NoCore.config.databaseFilename);
|
||||
Files.write(file.toPath(), data, StandardOpenOption.CREATE);
|
||||
}
|
||||
|
||||
public static synchronized void addNewNoUser(NoUser user) throws IOException {
|
||||
String hash = user.createHashString();
|
||||
NoHashSphere.database.add(hash);
|
||||
NoHashSphere.saveToFile();
|
||||
}
|
||||
|
||||
public static synchronized void insertHash(String hash) throws IOException {
|
||||
NoHashSphere.database.add(hash);
|
||||
NoHashSphere.saveToFile();
|
||||
}
|
||||
|
||||
public static synchronized void removeHash(String hash) throws IOException {
|
||||
NoHashSphere.database.remove(hash);
|
||||
NoHashSphere.saveToFile();
|
||||
}
|
||||
|
||||
public static synchronized boolean checkHash(String hash) {
|
||||
return NoHashSphere.database.contains(hash);
|
||||
}
|
||||
|
||||
public static synchronized int size() {
|
||||
return NoHashSphere.database.size();
|
||||
}
|
||||
}
|
||||
165
src/nodash/core/spheres/NoSessionSphere.java
Normal file
165
src/nodash/core/spheres/NoSessionSphere.java
Normal file
@@ -0,0 +1,165 @@
|
||||
package nodash.core.spheres;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import nodash.core.NoRegister;
|
||||
import nodash.exceptions.NoByteSetBadDecryptionException;
|
||||
import nodash.exceptions.NoDashFatalException;
|
||||
import nodash.exceptions.NoDashSessionBadUUID;
|
||||
import nodash.exceptions.NoSessionAlreadyAwaitingConfirmationException;
|
||||
import nodash.exceptions.NoSessionConfirmedException;
|
||||
import nodash.exceptions.NoSessionExpiredException;
|
||||
import nodash.exceptions.NoSessionNotAwaitingConfirmationException;
|
||||
import nodash.exceptions.NoSessionNotChangedException;
|
||||
import nodash.exceptions.NoUserAlreadyOnlineException;
|
||||
import nodash.exceptions.NoUserNotValidException;
|
||||
import nodash.models.NoByteSet;
|
||||
import nodash.models.NoSession;
|
||||
import nodash.models.NoUser;
|
||||
import nodash.models.NoSession.NoState;
|
||||
|
||||
public final class NoSessionSphere {
|
||||
private static ConcurrentHashMap<UUID, NoSession> sessions = new ConcurrentHashMap<UUID, NoSession>();
|
||||
private static Set<byte[]> originalHashesOnline = Collections.newSetFromMap(new ConcurrentHashMap<byte[], Boolean>());
|
||||
|
||||
public static synchronized void prune() {
|
||||
for (UUID uuid : NoSessionSphere.sessions.keySet()) {
|
||||
pruneSingle(uuid);
|
||||
}
|
||||
}
|
||||
|
||||
public static void shred(byte[] encryptedUUID) {
|
||||
try {
|
||||
UUID uuid = NoSession.decryptUUID(encryptedUUID);
|
||||
if (NoSessionSphere.sessions.containsKey(uuid)) {
|
||||
NoSession session = NoSessionSphere.sessions.get(uuid);
|
||||
NoByteSetSphere.addList(session.incoming, session.current.getRSAPublicKey());
|
||||
NoSessionSphere.originalHashesOnline.remove(session.getOriginalHash());
|
||||
NoSessionSphere.sessions.remove(uuid);
|
||||
session = null;
|
||||
}
|
||||
} catch (NoDashSessionBadUUID e) {
|
||||
// Suppress, doesn't matter
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized void pruneSingle(UUID uuid) {
|
||||
NoSession session = NoSessionSphere.sessions.get(uuid);
|
||||
try {
|
||||
session.check();
|
||||
} catch (NoSessionExpiredException e) {
|
||||
/* Resultant from 3.1 and 3.2 */
|
||||
NoByteSetSphere.addList(session.incoming, session.current.getRSAPublicKey());
|
||||
NoSessionSphere.originalHashesOnline.remove(session.getOriginalHash());
|
||||
NoSessionSphere.sessions.remove(uuid);
|
||||
session = null;
|
||||
} catch (NoSessionConfirmedException e) {
|
||||
/* Should be cleaned up at 5.2 */
|
||||
}
|
||||
}
|
||||
|
||||
public static synchronized byte[] login(byte[] data, char[] password) throws NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionExpiredException {
|
||||
/* 1. Login with byte[] data and byte[] password */
|
||||
NoSession session = new NoSession(data, password);
|
||||
/* 1.1. User currently has an online session, must wait for it to expire. */
|
||||
if (originalHashesOnline.contains(session.getOriginalHash())) {
|
||||
throw new NoUserAlreadyOnlineException();
|
||||
}
|
||||
/* 1.2. User successfully logged in: set up session records. */
|
||||
NoSessionSphere.originalHashesOnline.add(session.getOriginalHash());
|
||||
NoSessionSphere.sessions.put(session.uuid, session);
|
||||
|
||||
/* 2. Check NoByteSetSphere for incoming Influences */
|
||||
session.incoming = NoByteSetSphere.consume(session.current);
|
||||
for (NoByteSet nbs : session.incoming) {
|
||||
/* 2.1 Decrypt NoInfluence from NoByteSet, let the current user consume them */
|
||||
try {
|
||||
session.consume(nbs);
|
||||
} catch (NoByteSetBadDecryptionException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} /* 2.2 Alternatively, no NoByteSets to consume */
|
||||
|
||||
try {
|
||||
session.check();
|
||||
} catch (NoSessionConfirmedException e) {
|
||||
/* Should be impossible to reach */
|
||||
throw new NoDashFatalException(e);
|
||||
}
|
||||
|
||||
/* Will set to 2.1[MODIFIED] or 2.2[IDLE] */
|
||||
|
||||
/* Precursor to 3.; allow website to associate user session with a cookie. */
|
||||
return session.getEncryptedUUID();
|
||||
}
|
||||
|
||||
public static NoUser getUser(byte[] encryptedUUID) throws NoDashSessionBadUUID, NoSessionExpiredException, NoSessionConfirmedException {
|
||||
UUID uuid = NoSession.decryptUUID(encryptedUUID);
|
||||
if (NoSessionSphere.sessions.containsKey(uuid)) {
|
||||
NoSessionSphere.pruneSingle(uuid);
|
||||
return NoSessionSphere.sessions.get(uuid).getNoUser();
|
||||
}
|
||||
throw new NoSessionExpiredException();
|
||||
}
|
||||
|
||||
public static NoState getState(byte[] encryptedUUID) throws NoDashSessionBadUUID, NoSessionExpiredException, NoSessionConfirmedException {
|
||||
UUID uuid = NoSession.decryptUUID(encryptedUUID);
|
||||
if (NoSessionSphere.sessions.containsKey(uuid)) {
|
||||
NoSessionSphere.pruneSingle(uuid);
|
||||
NoSession session = NoSessionSphere.sessions.get(uuid);
|
||||
return session.getNoState();
|
||||
}
|
||||
throw new NoSessionExpiredException();
|
||||
}
|
||||
|
||||
public static synchronized byte[] save(byte[] encryptedUUID, char[] password) throws NoDashSessionBadUUID, NoSessionExpiredException, NoSessionConfirmedException, NoSessionNotChangedException, NoSessionAlreadyAwaitingConfirmationException {
|
||||
UUID uuid = NoSession.decryptUUID(encryptedUUID);
|
||||
if (NoSessionSphere.sessions.containsKey(uuid)) {
|
||||
NoSessionSphere.pruneSingle(uuid);
|
||||
NoSession session = NoSessionSphere.sessions.get(uuid);
|
||||
|
||||
if (session.getNoState().equals(NoState.IDLE)) {
|
||||
throw new NoSessionNotChangedException();
|
||||
} else if (session.getNoState().equals(NoState.AWAITING_CONFIRMATION)) {
|
||||
throw new NoSessionAlreadyAwaitingConfirmationException();
|
||||
}
|
||||
return session.initiateSaveAttempt(password);
|
||||
}
|
||||
throw new NoSessionExpiredException();
|
||||
}
|
||||
|
||||
public static synchronized void confirm(byte[] encryptedUUID, char[] password, byte[] data) throws NoDashSessionBadUUID, NoSessionExpiredException, NoSessionConfirmedException, NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
|
||||
UUID uuid = NoSession.decryptUUID(encryptedUUID);
|
||||
if (NoSessionSphere.sessions.containsKey(uuid)) {
|
||||
NoSessionSphere.pruneSingle(uuid);
|
||||
NoSession session = NoSessionSphere.sessions.get(uuid);
|
||||
session.confirmSave(data, password);
|
||||
return;
|
||||
}
|
||||
throw new NoSessionExpiredException();
|
||||
}
|
||||
|
||||
public static synchronized NoRegister registerUser(NoUser user, char[] password) {
|
||||
NoRegister result = new NoRegister();
|
||||
NoSession session = new NoSession(user);
|
||||
NoSessionSphere.sessions.put(session.uuid, session);
|
||||
result.cookie = session.getEncryptedUUID();
|
||||
try {
|
||||
result.data = NoSessionSphere.save(result.cookie, password);
|
||||
} catch (NoDashSessionBadUUID e) {
|
||||
throw new NoDashFatalException("Immediately generated cookie throwing bad cookie error.");
|
||||
} catch (NoSessionExpiredException e) {
|
||||
throw new NoDashFatalException("Session expired before it was even returned to client.");
|
||||
} catch (NoSessionConfirmedException e) {
|
||||
throw new NoDashFatalException("Session is in confirmed state before it was returned to client.");
|
||||
} catch (NoSessionNotChangedException e) {
|
||||
throw new NoDashFatalException("Session claims to be unchanged but user is newly registered.");
|
||||
} catch (NoSessionAlreadyAwaitingConfirmationException e) {
|
||||
throw new NoDashFatalException("Session claims to be awaiting confirmation before returning data to the user.");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user