all basic tests pass!

This commit is contained in:
Dave
2015-07-04 14:46:22 +02:00
parent 1c1a6012bf
commit cd1c7bdc69
9 changed files with 248 additions and 101 deletions

View File

@@ -4,6 +4,7 @@ import java.security.PublicKey;
import java.util.Collection;
import nodash.exceptions.NoAdapterException;
import nodash.exceptions.NoSessionExpiredException;
import nodash.exceptions.NoUserAlreadyOnlineException;
import nodash.exceptions.NoUserNotValidException;
import nodash.models.NoByteSet;
@@ -19,11 +20,11 @@ public interface NoAdapter {
public byte[][] exportHashes() throws NoAdapterException;
public long hashCount() throws NoAdapterException;
public void goOnline(byte[] hash) throws NoAdapterException, NoUserAlreadyOnlineException;
public boolean isOnline(byte[] hash) throws NoAdapterException;
public void goOffline(byte[] hash) throws NoAdapterException;
public void addNoSession(NoSession session) throws NoAdapterException;
@@ -32,12 +33,13 @@ public interface NoAdapter {
public void shredNoSession(byte[] encryptedUuid) throws NoAdapterException;
public NoSession getNoSession(byte[] encryptedUuid) throws NoAdapterException;
public NoSession getNoSession(byte[] encryptedUuid) throws NoAdapterException,
NoSessionExpiredException;
public Collection<NoByteSet> pollNoByteSets(PublicKey address) throws NoAdapterException;
public void addNoByteSet(NoByteSet byteSet, PublicKey address) throws NoAdapterException;
public void addNoByteSets(Collection<NoByteSet> byteSets, PublicKey address)
throws NoAdapterException;
}

View File

@@ -17,6 +17,8 @@
package nodash.core;
import org.apache.commons.codec.binary.Base64;
import nodash.exceptions.NoAdapterException;
import nodash.exceptions.NoByteSetBadDecryptionException;
import nodash.exceptions.NoDashFatalException;
@@ -59,7 +61,7 @@ public final class NoCore {
public byte[] login(byte[] data, char[] password) throws NoUserNotValidException,
NoUserAlreadyOnlineException, NoSessionExpiredException {
NoSession session = new NoSession(adapter, data, password);
NoSession session = new NoSession(data, password);
/* 1. Check that user is a valid user of the system based on their hash. */
try {
@@ -129,6 +131,7 @@ public final class NoCore {
byte[] userFile;
try {
userFile = save(cookie, password);
adapter.goOnline(user.createHash());
} catch (NoSessionExpiredException e) {
throw new NoDashFatalException("Session expired despite just being created.");
} catch (NoSessionConfirmedException e) {
@@ -139,6 +142,10 @@ public final class NoCore {
} catch (NoSessionAlreadyAwaitingConfirmationException e) {
throw new NoDashFatalException(
"Session already waiting confirmation despite just being created.");
} catch (NoAdapterException e) {
throw new NoDashFatalException("Could not go online.", e);
} catch (NoUserAlreadyOnlineException e) {
throw new NoDashFatalException("User with same hash is already online.");
}
return new NoRegister(cookie, userFile);
@@ -196,29 +203,44 @@ public final class NoCore {
}
try {
adapter.goOffline(newHash);
adapter.shredNoSession(cookie);
} catch (NoAdapterException e) {
throw new NoDashFatalException("Could not shred session.", e);
}
try {
adapter.goOffline(session.getNoUserSafe().createHash());
} catch (NoAdapterException e) {
throw new NoDashFatalException("Could not go offline.", e);
}
if (!session.isNewUser()) {
try {
adapter.removeHash(oldHash);
} catch (NoAdapterException e) {
throw new NoDashFatalException("Could not remove old hash.", e);
}
}
}
public void shred(byte[] cookie) throws NoSessionExpiredException {
NoSession session = getNoSession(cookie);
try {
adapter.shredNoSession(cookie);
} catch (NoAdapterException e) {
throw new NoDashFatalException("Could not shred session.", e);
}
try {
adapter.removeHash(oldHash);
adapter.addNoByteSets(session.getIncomingSafe(), session.getNoUserSafe().getRsaPublicKey());
} catch (NoAdapterException e) {
throw new NoDashFatalException("Could not remove old hash.", e);
throw new NoDashFatalException("Could not add bytesets back into pool.");
}
}
public void shred(byte[] cookie) {
try {
adapter.shredNoSession(cookie);
adapter.goOffline(session.getNoUserSafe().createHash());
} catch (NoAdapterException e) {
throw new NoDashFatalException("Could not shred session.", e);
throw new NoDashFatalException("Could not go offline.", e);
}
}

View File

@@ -44,19 +44,21 @@ public class NoDefaultAdapter implements NoAdapter {
private static synchronized byte[] alterFile(byte[] hashToAdd, byte[] hashToRemove)
throws IOException {
if (!(hashToAdd == null ^ hashToRemove == null) && Arrays.equals(hashToAdd, hashToRemove)) {
throw new IllegalArgumentException("Hashes to add and remove cannot be the same.");
}
File file = new File(HASH_FILE);
if (!file.exists()) {
Files.createFile(file.toPath());
}
byte[] originalFileBytes = Files.readAllBytes(file.toPath());
if (hashToAdd == null && hashToRemove == null) {
return originalFileBytes;
}
if (!(hashToAdd == null ^ hashToRemove == null) && Arrays.equals(hashToAdd, hashToRemove)) {
throw new IllegalArgumentException("Hashes to add and remove cannot be the same.");
}
int hashes = originalFileBytes.length / 64;
List<Byte> newFile = new ArrayList<Byte>();
for (int x = 0; x < hashes; x++) {
byte[] hash = Arrays.copyOfRange(originalFileBytes, x * 64, x * 64 + 64);
@@ -66,7 +68,7 @@ public class NoDefaultAdapter implements NoAdapter {
}
}
if (hashToAdd != null || Arrays.equals(hash, hashToAdd)) {
if (hashToAdd != null && Arrays.equals(hash, hashToAdd)) {
hashToAdd = null;
}
}
@@ -152,7 +154,7 @@ public class NoDefaultAdapter implements NoAdapter {
public boolean containsNoSession(byte[] encryptedUuid) {
String uuid;
try {
uuid = Base64.encodeBase64String(NoUtil.decrypt(encryptedUuid));
uuid = Base64.encodeBase64URLSafeString(NoUtil.decrypt(encryptedUuid));
} catch (IllegalBlockSizeException | BadPaddingException e) {
throw new NoDashFatalException("Could not decrypt given UUID.", e);
}
@@ -163,7 +165,6 @@ public class NoDefaultAdapter implements NoAdapter {
@Override
public void shredNoSession(byte[] encryptedUuid) {
NoSession session = getNoSession(encryptedUuid);
addNoByteSets(session.getIncomingSafe(), session.getNoUserSafe().getRsaPublicKey());
sessions.remove(session.getUuid());
}
@@ -172,7 +173,7 @@ public class NoDefaultAdapter implements NoAdapter {
if (containsNoSession(encryptedUuid)) {
String uuid;
try {
uuid = Base64.encodeBase64String(NoUtil.decrypt(encryptedUuid));
uuid = Base64.encodeBase64URLSafeString(NoUtil.decrypt(encryptedUuid));
} catch (IllegalBlockSizeException | BadPaddingException e) {
throw new NoDashFatalException("Could not decrypt given UUID.", e);
}
@@ -206,6 +207,14 @@ public class NoDefaultAdapter implements NoAdapter {
@Override
public void addNoByteSets(Collection<NoByteSet> addedByteSets, PublicKey address) {
if (addedByteSets == null) {
return;
}
if (address == null) {
throw new NullPointerException("Address cannot be null.");
}
if (byteSets.containsKey(address)) {
byteSets.get(address).addAll(addedByteSets);
} else {
@@ -215,7 +224,7 @@ public class NoDefaultAdapter implements NoAdapter {
@Override
public void goOnline(byte[] hash) throws NoUserAlreadyOnlineException {
String hashString = Base64.encodeBase64String(hash);
String hashString = Base64.encodeBase64URLSafeString(hash);
if (online.contains(hashString)) {
throw new NoUserAlreadyOnlineException();
}
@@ -224,13 +233,13 @@ public class NoDefaultAdapter implements NoAdapter {
@Override
public boolean isOnline(byte[] hash) {
String hashString = Base64.encodeBase64String(hash);
String hashString = Base64.encodeBase64URLSafeString(hash);
return online.contains(hashString);
}
@Override
public void goOffline(byte[] hash) {
String hashString = Base64.encodeBase64String(hash);
String hashString = Base64.encodeBase64URLSafeString(hash);
online.remove(hashString);
}