Changed from static system var to explicit constructor
(consider changing to a generic NoCore/NoSession type in future)
This commit is contained in:
@@ -43,6 +43,7 @@ import nodash.models.NoSession.NoState;
|
||||
*/
|
||||
public final class NoCore {
|
||||
private NoAdapter adapter;
|
||||
private Class<? extends NoUser> userClass;
|
||||
|
||||
/**
|
||||
* Instantiates an instance of the NoCore, using the given adapter to interact with saved hashes,
|
||||
@@ -50,8 +51,9 @@ public final class NoCore {
|
||||
*
|
||||
* @param adapter an object implementing the NoAdapter interface.
|
||||
*/
|
||||
public NoCore(NoAdapter adapter) {
|
||||
public NoCore(NoAdapter adapter, Class<? extends NoUser> userClass) {
|
||||
this.adapter = adapter;
|
||||
this.userClass = userClass;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -92,7 +94,7 @@ public final class NoCore {
|
||||
*/
|
||||
public byte[] login(byte[] data, char[] password) throws NoUserNotValidException,
|
||||
NoUserAlreadyOnlineException {
|
||||
NoSession session = new NoSession(data, password);
|
||||
NoSession session = new NoSession(data, password, userClass);
|
||||
|
||||
/* 1. Check that user is a valid user of the system based on their hash. */
|
||||
try {
|
||||
@@ -286,7 +288,7 @@ public final class NoCore {
|
||||
byte[] newHash = session.getNoUserSafe().createHash();
|
||||
|
||||
try {
|
||||
session.confirmSave(adapter, data, password);
|
||||
session.confirmSave(adapter, data, password, userClass);
|
||||
|
||||
try {
|
||||
adapter.insertHash(newHash);
|
||||
|
||||
@@ -38,7 +38,6 @@ import javax.crypto.spec.SecretKeySpec;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
|
||||
import nodash.exceptions.NoDashFatalException;
|
||||
import nodash.models.NoUser;
|
||||
|
||||
public final class NoUtil {
|
||||
public static final SecretKey SECRET_KEY = setupSecretKey();
|
||||
@@ -53,18 +52,6 @@ public final class NoUtil {
|
||||
public static final int RSA_STRENGTH = setupRsaStrength();
|
||||
public static final int AES_STRENGTH = 256;
|
||||
public static final byte BLANK_BYTE = 'A';
|
||||
public static final Class<? extends NoUser> NO_USER_CLASS = setupNoUserClass();
|
||||
|
||||
private static Class<? extends NoUser> setupNoUserClass() {
|
||||
String secretEnv = System.getenv("NODASH_USER_CLASS");
|
||||
try {
|
||||
@SuppressWarnings("unchecked")
|
||||
Class<? extends NoUser> clazz = (Class<? extends NoUser>) Class.forName(secretEnv);
|
||||
return clazz;
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new RuntimeException("Can't find NODASH_USER_CLASS", e);
|
||||
}
|
||||
}
|
||||
|
||||
private static SecretKey setupSecretKey() {
|
||||
String secretEnv = System.getenv("NODASH_SECRET");
|
||||
|
||||
@@ -50,14 +50,15 @@ public final class NoSession implements Serializable {
|
||||
this.current = newUser;
|
||||
}
|
||||
|
||||
public NoSession(byte[] data, char[] password) throws NoUserNotValidException {
|
||||
public NoSession(byte[] data, char[] password, Class<? extends NoUser> userClass)
|
||||
throws NoUserNotValidException {
|
||||
this();
|
||||
this.state = NoState.IDLE;
|
||||
byte[] originalData = Arrays.copyOf(data, data.length);
|
||||
char[] originalPassword = Arrays.copyOf(password, password.length);
|
||||
this.original =
|
||||
NoUser.createUserFromFile(originalData, originalPassword, NoUtil.NO_USER_CLASS);
|
||||
this.current = NoUser.createUserFromFile(data, password, NoUtil.NO_USER_CLASS);
|
||||
NoUser.createUserFromFile(originalData, originalPassword, userClass);
|
||||
this.current = NoUser.createUserFromFile(data, password, userClass);
|
||||
NoUtil.wipeBytes(data);
|
||||
NoUtil.wipeChars(password);
|
||||
this.uuid = UUID.randomUUID().toString();
|
||||
@@ -103,7 +104,7 @@ public final class NoSession implements Serializable {
|
||||
return file;
|
||||
}
|
||||
|
||||
public void confirmSave(NoAdapter adapter, byte[] confirmData, char[] password)
|
||||
public void confirmSave(NoAdapter adapter, byte[] confirmData, char[] password, Class<? extends NoUser> userClass)
|
||||
throws NoSessionConfirmedException, NoSessionExpiredException,
|
||||
NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
|
||||
check();
|
||||
@@ -111,7 +112,7 @@ public final class NoSession implements Serializable {
|
||||
throw new NoSessionNotAwaitingConfirmationException();
|
||||
}
|
||||
|
||||
NoUser confirmed = NoUser.createUserFromFile(confirmData, password, NoUtil.NO_USER_CLASS);
|
||||
NoUser confirmed = NoUser.createUserFromFile(confirmData, password, userClass);
|
||||
|
||||
NoUtil.wipeBytes(confirmData);
|
||||
NoUtil.wipeChars(password);
|
||||
|
||||
@@ -47,7 +47,7 @@ public class NoCoreTest {
|
||||
|
||||
@Test
|
||||
public void testRegister() {
|
||||
NoCore core = new NoCore(new NoDefaultAdapter());
|
||||
NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
|
||||
|
||||
NoUser user1 = new TestNoUser("Test");
|
||||
NoRegister registration1 = core.register(user1, "password".toCharArray());
|
||||
@@ -90,7 +90,7 @@ public class NoCoreTest {
|
||||
NoSessionAlreadyAwaitingConfirmationException, NoAdapterException, NoSuchMethodException,
|
||||
SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
|
||||
NoAdapter adapter = new NoDefaultAdapter();
|
||||
NoCore core = new NoCore(adapter);
|
||||
NoCore core = new NoCore(adapter, TestNoUser.class);
|
||||
|
||||
NoUser newUser = new TestNoUser("Test");
|
||||
NoRegister registration = core.register(newUser, "password".toCharArray());
|
||||
@@ -162,7 +162,7 @@ public class NoCoreTest {
|
||||
public void testGetUser() throws NoSessionExpiredException, NoSessionConfirmedException,
|
||||
NoSessionNotAwaitingConfirmationException, NoUserNotValidException,
|
||||
NoUserAlreadyOnlineException {
|
||||
NoCore core = new NoCore(new NoDefaultAdapter());
|
||||
NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
|
||||
NoUser user = new TestNoUser("Test");
|
||||
NoRegister registration = core.register(user, "password".toCharArray());
|
||||
byte[] file = Arrays.copyOf(registration.data, registration.data.length);
|
||||
@@ -188,7 +188,7 @@ public class NoCoreTest {
|
||||
public void testGetSessionState() throws NoSessionExpiredException, NoSessionConfirmedException,
|
||||
NoSessionNotAwaitingConfirmationException, NoUserNotValidException,
|
||||
NoUserAlreadyOnlineException, NoSessionNotChangedException, NoSessionAlreadyAwaitingConfirmationException {
|
||||
NoCore core = new NoCore(new NoDefaultAdapter());
|
||||
NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
|
||||
NoUser user = new TestNoUser("Test");
|
||||
NoRegister registration = core.register(user, "password".toCharArray());
|
||||
assertEquals(core.getSessionState(registration.cookie), NoState.AWAITING_CONFIRMATION);
|
||||
@@ -217,7 +217,7 @@ public class NoCoreTest {
|
||||
@Test
|
||||
public void testShred() throws NoAdapterException, NoSessionConfirmedException, NoSessionExpiredException {
|
||||
NoAdapter adapter = new NoDefaultAdapter();
|
||||
NoCore core = new NoCore(adapter);
|
||||
NoCore core = new NoCore(adapter, TestNoUser.class);
|
||||
NoUser user = new TestNoUser("Test");
|
||||
NoRegister registration = core.register(user, "password".toCharArray());
|
||||
assertTrue(adapter.isOnline(user.createHash()));
|
||||
|
||||
@@ -52,14 +52,15 @@ public class NoSessionTest {
|
||||
// Do nothing, correct
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNoSessionByteArrayCharArray() throws NoUserNotValidException,
|
||||
NoSessionExpiredException, NoSessionConfirmedException {
|
||||
public void testNoSessionByteArrayCharArray()
|
||||
throws NoUserNotValidException, NoSessionExpiredException, NoSessionConfirmedException {
|
||||
NoUser user = new TestNoUser("Test");
|
||||
final byte[] userFile1 = user.createFile("password".toCharArray());
|
||||
byte[] userFile2 = Arrays.copyOf(userFile1, userFile1.length);
|
||||
char[] userPassword = "password".toCharArray();
|
||||
NoSession session = new NoSession(userFile2, userPassword);
|
||||
NoSession session = new NoSession(userFile2, userPassword, TestNoUser.class);
|
||||
assertFalse(Arrays.equals(userFile1, userFile2));
|
||||
assertFalse(Arrays.equals("password".toCharArray(), userPassword));
|
||||
assertNotNull(session.getNoUser());
|
||||
@@ -72,14 +73,15 @@ public class NoSessionTest {
|
||||
byte[] badUserFile = Arrays.copyOf(userFile1, userFile1.length);
|
||||
badUserFile[0] = (byte) (badUserFile[0] == 'A' ? 'B' : 'A');
|
||||
try {
|
||||
new NoSession(badUserFile, "password".toCharArray());
|
||||
new NoSession(badUserFile, "password".toCharArray(), TestNoUser.class);
|
||||
fail("Did not throw NoUserNotValidException when given bad file.");
|
||||
} catch (NoUserNotValidException e) {
|
||||
// Do nothing, correct
|
||||
}
|
||||
|
||||
try {
|
||||
new NoSession(Arrays.copyOf(userFile2, userFile2.length), "badpassword".toCharArray());
|
||||
new NoSession(Arrays.copyOf(userFile2, userFile2.length), "badpassword".toCharArray(),
|
||||
TestNoUser.class);
|
||||
fail("Did not throw NoUserNotValidException when given bad password.");
|
||||
} catch (NoUserNotValidException e) {
|
||||
// Do nothing, correct
|
||||
|
||||
@@ -93,34 +93,34 @@ public class NoUserTest {
|
||||
user = null;
|
||||
|
||||
try {
|
||||
user = NoUser.createUserFromFile(file, "wrongpassword".toCharArray(), NoUtil.NO_USER_CLASS);
|
||||
user = NoUser.createUserFromFile(file, "wrongpassword".toCharArray(), TestNoUser.class);
|
||||
fail("Should have thrown an error when given wrong password.");
|
||||
} catch (NoUserNotValidException e) {
|
||||
// Do nothing, correct
|
||||
}
|
||||
|
||||
file = Arrays.copyOf(originalFile, originalFile.length);
|
||||
user = NoUser.createUserFromFile(file, "password".toCharArray(), NoUtil.NO_USER_CLASS);
|
||||
user = NoUser.createUserFromFile(file, "password".toCharArray(), TestNoUser.class);
|
||||
assertTrue(Arrays.equals(hash, user.createHash()));
|
||||
assertEquals(hashString, user.createHashString());
|
||||
|
||||
file = Arrays.copyOf(originalFile, originalFile.length);
|
||||
try {
|
||||
NoUser.createUserFromFile(file, null, NoUtil.NO_USER_CLASS);
|
||||
NoUser.createUserFromFile(file, null, TestNoUser.class);
|
||||
fail("Should have thrown a NullPointerException.");
|
||||
} catch (NullPointerException e) {
|
||||
// Do nothing, correct
|
||||
}
|
||||
|
||||
try {
|
||||
NoUser.createUserFromFile(null, "password".toCharArray(), NoUtil.NO_USER_CLASS);
|
||||
NoUser.createUserFromFile(null, "password".toCharArray(), TestNoUser.class);
|
||||
fail("Should have thrown a IllegalArgumentException.");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// Do nothing, correct
|
||||
}
|
||||
|
||||
try {
|
||||
NoUser.createUserFromFile(null, null, NoUtil.NO_USER_CLASS);
|
||||
NoUser.createUserFromFile(null, null, TestNoUser.class);
|
||||
fail("Should have thrown a IllegalArgumentException.");
|
||||
} catch (NullPointerException e) {
|
||||
// Do nothing, correct
|
||||
|
||||
@@ -36,7 +36,7 @@ public class NoRoutineTest {
|
||||
private byte[] registerAndConfirm(TestNoUser user, String password)
|
||||
throws NoSessionExpiredException, NoSessionConfirmedException,
|
||||
NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
|
||||
NoCore core = new NoCore(new NoDefaultAdapter());
|
||||
NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
|
||||
NoRegister registration = core.register(user, password.toCharArray());
|
||||
core.confirm(getCopy(registration.cookie), password.toCharArray(), getCopy(registration.data));
|
||||
return getCopy(registration.data);
|
||||
@@ -50,7 +50,7 @@ public class NoRoutineTest {
|
||||
final byte[] userFile = registerAndConfirm(new TestNoUser("username"), "password");
|
||||
|
||||
NoAdapter adapter = new NoDefaultAdapter();
|
||||
NoCore core = new NoCore(adapter);
|
||||
NoCore core = new NoCore(adapter, TestNoUser.class);
|
||||
|
||||
byte[] cookie = core.login(getCopy(userFile), "password".toCharArray());
|
||||
TestNoUser userRegistered = (TestNoUser) core.getNoUser(Arrays.copyOf(cookie, cookie.length));
|
||||
@@ -84,7 +84,7 @@ public class NoRoutineTest {
|
||||
NoUserAlreadyOnlineException, NoSessionNotChangedException,
|
||||
NoSessionAlreadyAwaitingConfirmationException { // testing TestJustTouchStaticField
|
||||
byte[] userFile = registerAndConfirm(new TestNoUser("username"), "password");
|
||||
NoCore core = new NoCore(new NoDefaultAdapter());
|
||||
NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
|
||||
|
||||
int touchCount = TestJustTouchStaticField.touchMe;
|
||||
|
||||
@@ -118,7 +118,7 @@ public class NoRoutineTest {
|
||||
NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionNotChangedException,
|
||||
NoSessionAlreadyAwaitingConfirmationException { // testing TestRequestFunds
|
||||
byte[] requesterFile = registerAndConfirm(new TestNoUser("requester"), "password");
|
||||
NoCore core = new NoCore(new NoDefaultAdapter());
|
||||
NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
|
||||
|
||||
byte[] requesterCookie = core.login(getCopy(requesterFile), "password".toCharArray());
|
||||
TestNoUser requester = (TestNoUser) core.getNoUser(getCopy(requesterCookie));
|
||||
@@ -137,7 +137,7 @@ public class NoRoutineTest {
|
||||
NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionNotChangedException,
|
||||
NoSessionAlreadyAwaitingConfirmationException { // testing TestRequestFunds
|
||||
byte[] requesterFile = registerAndConfirm(new TestNoUser("requester"), "password");
|
||||
NoCore core = new NoCore(new NoDefaultAdapter());
|
||||
NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
|
||||
|
||||
byte[] requesterCookie = core.login(getCopy(requesterFile), "password".toCharArray());
|
||||
TestNoUser requester = (TestNoUser) core.getNoUser(getCopy(requesterCookie));
|
||||
@@ -161,7 +161,7 @@ public class NoRoutineTest {
|
||||
NoSessionAlreadyAwaitingConfirmationException { // testing TestSendFunds
|
||||
byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1");
|
||||
byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2");
|
||||
NoCore core = new NoCore(new NoDefaultAdapter());
|
||||
NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
|
||||
|
||||
byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray());
|
||||
TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie);
|
||||
@@ -211,7 +211,7 @@ public class NoRoutineTest {
|
||||
NoSessionAlreadyAwaitingConfirmationException { // testing TestSendFundsReceipted
|
||||
byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1");
|
||||
byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2");
|
||||
NoCore core = new NoCore(new NoDefaultAdapter());
|
||||
NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
|
||||
|
||||
byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray());
|
||||
TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie);
|
||||
@@ -268,7 +268,7 @@ public class NoRoutineTest {
|
||||
NoUserNotValidException, NoUserAlreadyOnlineException { // testing TestSendFundsSourced
|
||||
byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1");
|
||||
byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2");
|
||||
NoCore core = new NoCore(new NoDefaultAdapter());
|
||||
NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
|
||||
|
||||
byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray());
|
||||
TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie);
|
||||
|
||||
Reference in New Issue
Block a user