Changed from static system var to explicit constructor

(consider changing to a generic NoCore/NoSession type in future)
This commit is contained in:
Dave
2015-11-29 15:50:26 +02:00
parent d230b610da
commit d0e2b2d69a
7 changed files with 40 additions and 48 deletions

View File

@@ -43,6 +43,7 @@ import nodash.models.NoSession.NoState;
*/ */
public final class NoCore { public final class NoCore {
private NoAdapter adapter; private NoAdapter adapter;
private Class<? extends NoUser> userClass;
/** /**
* Instantiates an instance of the NoCore, using the given adapter to interact with saved hashes, * 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. * @param adapter an object implementing the NoAdapter interface.
*/ */
public NoCore(NoAdapter adapter) { public NoCore(NoAdapter adapter, Class<? extends NoUser> userClass) {
this.adapter = adapter; this.adapter = adapter;
this.userClass = userClass;
} }
/** /**
@@ -92,7 +94,7 @@ public final class NoCore {
*/ */
public byte[] login(byte[] data, char[] password) throws NoUserNotValidException, public byte[] login(byte[] data, char[] password) throws NoUserNotValidException,
NoUserAlreadyOnlineException { 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. */ /* 1. Check that user is a valid user of the system based on their hash. */
try { try {
@@ -286,7 +288,7 @@ public final class NoCore {
byte[] newHash = session.getNoUserSafe().createHash(); byte[] newHash = session.getNoUserSafe().createHash();
try { try {
session.confirmSave(adapter, data, password); session.confirmSave(adapter, data, password, userClass);
try { try {
adapter.insertHash(newHash); adapter.insertHash(newHash);

View File

@@ -38,7 +38,6 @@ import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
import nodash.exceptions.NoDashFatalException; import nodash.exceptions.NoDashFatalException;
import nodash.models.NoUser;
public final class NoUtil { public final class NoUtil {
public static final SecretKey SECRET_KEY = setupSecretKey(); public static final SecretKey SECRET_KEY = setupSecretKey();
@@ -53,19 +52,7 @@ public final class NoUtil {
public static final int RSA_STRENGTH = setupRsaStrength(); public static final int RSA_STRENGTH = setupRsaStrength();
public static final int AES_STRENGTH = 256; public static final int AES_STRENGTH = 256;
public static final byte BLANK_BYTE = 'A'; 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() { private static SecretKey setupSecretKey() {
String secretEnv = System.getenv("NODASH_SECRET"); String secretEnv = System.getenv("NODASH_SECRET");
if (secretEnv == null) { if (secretEnv == null) {

View File

@@ -50,14 +50,15 @@ public final class NoSession implements Serializable {
this.current = newUser; 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();
this.state = NoState.IDLE; this.state = NoState.IDLE;
byte[] originalData = Arrays.copyOf(data, data.length); byte[] originalData = Arrays.copyOf(data, data.length);
char[] originalPassword = Arrays.copyOf(password, password.length); char[] originalPassword = Arrays.copyOf(password, password.length);
this.original = this.original =
NoUser.createUserFromFile(originalData, originalPassword, NoUtil.NO_USER_CLASS); NoUser.createUserFromFile(originalData, originalPassword, userClass);
this.current = NoUser.createUserFromFile(data, password, NoUtil.NO_USER_CLASS); this.current = NoUser.createUserFromFile(data, password, userClass);
NoUtil.wipeBytes(data); NoUtil.wipeBytes(data);
NoUtil.wipeChars(password); NoUtil.wipeChars(password);
this.uuid = UUID.randomUUID().toString(); this.uuid = UUID.randomUUID().toString();
@@ -103,7 +104,7 @@ public final class NoSession implements Serializable {
return file; 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, throws NoSessionConfirmedException, NoSessionExpiredException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException { NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
check(); check();
@@ -111,7 +112,7 @@ public final class NoSession implements Serializable {
throw new NoSessionNotAwaitingConfirmationException(); throw new NoSessionNotAwaitingConfirmationException();
} }
NoUser confirmed = NoUser.createUserFromFile(confirmData, password, NoUtil.NO_USER_CLASS); NoUser confirmed = NoUser.createUserFromFile(confirmData, password, userClass);
NoUtil.wipeBytes(confirmData); NoUtil.wipeBytes(confirmData);
NoUtil.wipeChars(password); NoUtil.wipeChars(password);

View File

@@ -47,7 +47,7 @@ public class NoCoreTest {
@Test @Test
public void testRegister() { public void testRegister() {
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
NoUser user1 = new TestNoUser("Test"); NoUser user1 = new TestNoUser("Test");
NoRegister registration1 = core.register(user1, "password".toCharArray()); NoRegister registration1 = core.register(user1, "password".toCharArray());
@@ -90,7 +90,7 @@ public class NoCoreTest {
NoSessionAlreadyAwaitingConfirmationException, NoAdapterException, NoSuchMethodException, NoSessionAlreadyAwaitingConfirmationException, NoAdapterException, NoSuchMethodException,
SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
NoAdapter adapter = new NoDefaultAdapter(); NoAdapter adapter = new NoDefaultAdapter();
NoCore core = new NoCore(adapter); NoCore core = new NoCore(adapter, TestNoUser.class);
NoUser newUser = new TestNoUser("Test"); NoUser newUser = new TestNoUser("Test");
NoRegister registration = core.register(newUser, "password".toCharArray()); NoRegister registration = core.register(newUser, "password".toCharArray());
@@ -162,7 +162,7 @@ public class NoCoreTest {
public void testGetUser() throws NoSessionExpiredException, NoSessionConfirmedException, public void testGetUser() throws NoSessionExpiredException, NoSessionConfirmedException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException, NoSessionNotAwaitingConfirmationException, NoUserNotValidException,
NoUserAlreadyOnlineException { NoUserAlreadyOnlineException {
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
NoUser user = new TestNoUser("Test"); NoUser user = new TestNoUser("Test");
NoRegister registration = core.register(user, "password".toCharArray()); NoRegister registration = core.register(user, "password".toCharArray());
byte[] file = Arrays.copyOf(registration.data, registration.data.length); byte[] file = Arrays.copyOf(registration.data, registration.data.length);
@@ -188,7 +188,7 @@ public class NoCoreTest {
public void testGetSessionState() throws NoSessionExpiredException, NoSessionConfirmedException, public void testGetSessionState() throws NoSessionExpiredException, NoSessionConfirmedException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException, NoSessionNotAwaitingConfirmationException, NoUserNotValidException,
NoUserAlreadyOnlineException, NoSessionNotChangedException, NoSessionAlreadyAwaitingConfirmationException { NoUserAlreadyOnlineException, NoSessionNotChangedException, NoSessionAlreadyAwaitingConfirmationException {
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
NoUser user = new TestNoUser("Test"); NoUser user = new TestNoUser("Test");
NoRegister registration = core.register(user, "password".toCharArray()); NoRegister registration = core.register(user, "password".toCharArray());
assertEquals(core.getSessionState(registration.cookie), NoState.AWAITING_CONFIRMATION); assertEquals(core.getSessionState(registration.cookie), NoState.AWAITING_CONFIRMATION);
@@ -217,7 +217,7 @@ public class NoCoreTest {
@Test @Test
public void testShred() throws NoAdapterException, NoSessionConfirmedException, NoSessionExpiredException { public void testShred() throws NoAdapterException, NoSessionConfirmedException, NoSessionExpiredException {
NoAdapter adapter = new NoDefaultAdapter(); NoAdapter adapter = new NoDefaultAdapter();
NoCore core = new NoCore(adapter); NoCore core = new NoCore(adapter, TestNoUser.class);
NoUser user = new TestNoUser("Test"); NoUser user = new TestNoUser("Test");
NoRegister registration = core.register(user, "password".toCharArray()); NoRegister registration = core.register(user, "password".toCharArray());
assertTrue(adapter.isOnline(user.createHash())); assertTrue(adapter.isOnline(user.createHash()));

View File

@@ -44,7 +44,7 @@ public class NoSessionTest {
assertNull(session.getOriginalHash()); assertNull(session.getOriginalHash());
assertEquals(session.getNoUser(), user); assertEquals(session.getNoUser(), user);
assertEquals(session.getNoState(), NoState.MODIFIED); assertEquals(session.getNoState(), NoState.MODIFIED);
try { try {
new NoSession(null); new NoSession(null);
fail("Did not throw NullPointerException when given a null user."); fail("Did not throw NullPointerException when given a null user.");
@@ -52,14 +52,15 @@ public class NoSessionTest {
// Do nothing, correct // Do nothing, correct
} }
} }
@Test @Test
public void testNoSessionByteArrayCharArray() throws NoUserNotValidException, public void testNoSessionByteArrayCharArray()
NoSessionExpiredException, NoSessionConfirmedException { throws NoUserNotValidException, NoSessionExpiredException, NoSessionConfirmedException {
NoUser user = new TestNoUser("Test"); NoUser user = new TestNoUser("Test");
final byte[] userFile1 = user.createFile("password".toCharArray()); final byte[] userFile1 = user.createFile("password".toCharArray());
byte[] userFile2 = Arrays.copyOf(userFile1, userFile1.length); byte[] userFile2 = Arrays.copyOf(userFile1, userFile1.length);
char[] userPassword = "password".toCharArray(); 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(userFile1, userFile2));
assertFalse(Arrays.equals("password".toCharArray(), userPassword)); assertFalse(Arrays.equals("password".toCharArray(), userPassword));
assertNotNull(session.getNoUser()); assertNotNull(session.getNoUser());
@@ -68,18 +69,19 @@ public class NoSessionTest {
assertNull(session.getIncoming()); assertNull(session.getIncoming());
assertEquals(session.getNoUser(), user); assertEquals(session.getNoUser(), user);
assertEquals(session.getNoState(), NoState.IDLE); assertEquals(session.getNoState(), NoState.IDLE);
byte[] badUserFile = Arrays.copyOf(userFile1, userFile1.length); byte[] badUserFile = Arrays.copyOf(userFile1, userFile1.length);
badUserFile[0] = (byte) (badUserFile[0] == 'A' ? 'B' : 'A'); badUserFile[0] = (byte) (badUserFile[0] == 'A' ? 'B' : 'A');
try { try {
new NoSession(badUserFile, "password".toCharArray()); new NoSession(badUserFile, "password".toCharArray(), TestNoUser.class);
fail("Did not throw NoUserNotValidException when given bad file."); fail("Did not throw NoUserNotValidException when given bad file.");
} catch (NoUserNotValidException e) { } catch (NoUserNotValidException e) {
// Do nothing, correct // Do nothing, correct
} }
try { 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."); fail("Did not throw NoUserNotValidException when given bad password.");
} catch (NoUserNotValidException e) { } catch (NoUserNotValidException e) {
// Do nothing, correct // Do nothing, correct

View File

@@ -93,34 +93,34 @@ public class NoUserTest {
user = null; user = null;
try { 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."); fail("Should have thrown an error when given wrong password.");
} catch (NoUserNotValidException e) { } catch (NoUserNotValidException e) {
// Do nothing, correct // Do nothing, correct
} }
file = Arrays.copyOf(originalFile, originalFile.length); 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())); assertTrue(Arrays.equals(hash, user.createHash()));
assertEquals(hashString, user.createHashString()); assertEquals(hashString, user.createHashString());
file = Arrays.copyOf(originalFile, originalFile.length); file = Arrays.copyOf(originalFile, originalFile.length);
try { try {
NoUser.createUserFromFile(file, null, NoUtil.NO_USER_CLASS); NoUser.createUserFromFile(file, null, TestNoUser.class);
fail("Should have thrown a NullPointerException."); fail("Should have thrown a NullPointerException.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Do nothing, correct // Do nothing, correct
} }
try { try {
NoUser.createUserFromFile(null, "password".toCharArray(), NoUtil.NO_USER_CLASS); NoUser.createUserFromFile(null, "password".toCharArray(), TestNoUser.class);
fail("Should have thrown a IllegalArgumentException."); fail("Should have thrown a IllegalArgumentException.");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// Do nothing, correct // Do nothing, correct
} }
try { try {
NoUser.createUserFromFile(null, null, NoUtil.NO_USER_CLASS); NoUser.createUserFromFile(null, null, TestNoUser.class);
fail("Should have thrown a IllegalArgumentException."); fail("Should have thrown a IllegalArgumentException.");
} catch (NullPointerException e) { } catch (NullPointerException e) {
// Do nothing, correct // Do nothing, correct

View File

@@ -36,7 +36,7 @@ public class NoRoutineTest {
private byte[] registerAndConfirm(TestNoUser user, String password) private byte[] registerAndConfirm(TestNoUser user, String password)
throws NoSessionExpiredException, NoSessionConfirmedException, throws NoSessionExpiredException, NoSessionConfirmedException,
NoSessionNotAwaitingConfirmationException, NoUserNotValidException { NoSessionNotAwaitingConfirmationException, NoUserNotValidException {
NoCore core = new NoCore(new NoDefaultAdapter()); NoCore core = new NoCore(new NoDefaultAdapter(), TestNoUser.class);
NoRegister registration = core.register(user, password.toCharArray()); NoRegister registration = core.register(user, password.toCharArray());
core.confirm(getCopy(registration.cookie), password.toCharArray(), getCopy(registration.data)); core.confirm(getCopy(registration.cookie), password.toCharArray(), getCopy(registration.data));
return getCopy(registration.data); return getCopy(registration.data);
@@ -50,7 +50,7 @@ public class NoRoutineTest {
final byte[] userFile = registerAndConfirm(new TestNoUser("username"), "password"); final byte[] userFile = registerAndConfirm(new TestNoUser("username"), "password");
NoAdapter adapter = new NoDefaultAdapter(); NoAdapter adapter = new NoDefaultAdapter();
NoCore core = new NoCore(adapter); NoCore core = new NoCore(adapter, TestNoUser.class);
byte[] cookie = core.login(getCopy(userFile), "password".toCharArray()); byte[] cookie = core.login(getCopy(userFile), "password".toCharArray());
TestNoUser userRegistered = (TestNoUser) core.getNoUser(Arrays.copyOf(cookie, cookie.length)); TestNoUser userRegistered = (TestNoUser) core.getNoUser(Arrays.copyOf(cookie, cookie.length));
@@ -84,7 +84,7 @@ public class NoRoutineTest {
NoUserAlreadyOnlineException, NoSessionNotChangedException, NoUserAlreadyOnlineException, NoSessionNotChangedException,
NoSessionAlreadyAwaitingConfirmationException { // testing TestJustTouchStaticField NoSessionAlreadyAwaitingConfirmationException { // testing TestJustTouchStaticField
byte[] userFile = registerAndConfirm(new TestNoUser("username"), "password"); 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; int touchCount = TestJustTouchStaticField.touchMe;
@@ -118,7 +118,7 @@ public class NoRoutineTest {
NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionNotChangedException, NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionNotChangedException,
NoSessionAlreadyAwaitingConfirmationException { // testing TestRequestFunds NoSessionAlreadyAwaitingConfirmationException { // testing TestRequestFunds
byte[] requesterFile = registerAndConfirm(new TestNoUser("requester"), "password"); 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()); byte[] requesterCookie = core.login(getCopy(requesterFile), "password".toCharArray());
TestNoUser requester = (TestNoUser) core.getNoUser(getCopy(requesterCookie)); TestNoUser requester = (TestNoUser) core.getNoUser(getCopy(requesterCookie));
@@ -137,7 +137,7 @@ public class NoRoutineTest {
NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionNotChangedException, NoUserNotValidException, NoUserAlreadyOnlineException, NoSessionNotChangedException,
NoSessionAlreadyAwaitingConfirmationException { // testing TestRequestFunds NoSessionAlreadyAwaitingConfirmationException { // testing TestRequestFunds
byte[] requesterFile = registerAndConfirm(new TestNoUser("requester"), "password"); 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()); byte[] requesterCookie = core.login(getCopy(requesterFile), "password".toCharArray());
TestNoUser requester = (TestNoUser) core.getNoUser(getCopy(requesterCookie)); TestNoUser requester = (TestNoUser) core.getNoUser(getCopy(requesterCookie));
@@ -161,7 +161,7 @@ public class NoRoutineTest {
NoSessionAlreadyAwaitingConfirmationException { // testing TestSendFunds NoSessionAlreadyAwaitingConfirmationException { // testing TestSendFunds
byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1"); byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1");
byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2"); 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()); byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray());
TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie); TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie);
@@ -211,7 +211,7 @@ public class NoRoutineTest {
NoSessionAlreadyAwaitingConfirmationException { // testing TestSendFundsReceipted NoSessionAlreadyAwaitingConfirmationException { // testing TestSendFundsReceipted
byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1"); byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1");
byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2"); 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()); byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray());
TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie); TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie);
@@ -268,7 +268,7 @@ public class NoRoutineTest {
NoUserNotValidException, NoUserAlreadyOnlineException { // testing TestSendFundsSourced NoUserNotValidException, NoUserAlreadyOnlineException { // testing TestSendFundsSourced
byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1"); byte[] fundSenderFile = registerAndConfirm(new TestNoUser("fund-sender"), "password1");
byte[] fundGetterFile = registerAndConfirm(new TestNoUser("fund-getter"), "password2"); 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()); byte[] senderCookie = core.login(getCopy(fundSenderFile), "password1".toCharArray());
TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie); TestNoUser sender = (TestNoUser) core.getNoUser(senderCookie);