diff --git a/src/nodash/core/NoCore.java b/src/nodash/core/NoCore.java index 3ce29a5..fd6a87b 100644 --- a/src/nodash/core/NoCore.java +++ b/src/nodash/core/NoCore.java @@ -43,6 +43,7 @@ import nodash.models.NoSession.NoState; */ public final class NoCore { private NoAdapter adapter; + private Class 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 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); diff --git a/src/nodash/core/NoUtil.java b/src/nodash/core/NoUtil.java index 293676d..71fa52d 100644 --- a/src/nodash/core/NoUtil.java +++ b/src/nodash/core/NoUtil.java @@ -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,19 +52,7 @@ 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 NO_USER_CLASS = setupNoUserClass(); - - private static Class setupNoUserClass() { - String secretEnv = System.getenv("NODASH_USER_CLASS"); - try { - @SuppressWarnings("unchecked") - Class clazz = (Class) 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"); if (secretEnv == null) { diff --git a/src/nodash/models/NoSession.java b/src/nodash/models/NoSession.java index d804ce4..75f8abc 100644 --- a/src/nodash/models/NoSession.java +++ b/src/nodash/models/NoSession.java @@ -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 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 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); diff --git a/src/nodash/test/NoCoreTest.java b/src/nodash/test/NoCoreTest.java index b7ddd94..2c8d872 100644 --- a/src/nodash/test/NoCoreTest.java +++ b/src/nodash/test/NoCoreTest.java @@ -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())); diff --git a/src/nodash/test/NoSessionTest.java b/src/nodash/test/NoSessionTest.java index c427665..698751f 100644 --- a/src/nodash/test/NoSessionTest.java +++ b/src/nodash/test/NoSessionTest.java @@ -44,7 +44,7 @@ public class NoSessionTest { assertNull(session.getOriginalHash()); assertEquals(session.getNoUser(), user); assertEquals(session.getNoState(), NoState.MODIFIED); - + try { new NoSession(null); fail("Did not throw NullPointerException when given a null user."); @@ -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()); @@ -68,18 +69,19 @@ public class NoSessionTest { assertNull(session.getIncoming()); assertEquals(session.getNoUser(), user); assertEquals(session.getNoState(), NoState.IDLE); - + 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 diff --git a/src/nodash/test/NoUserTest.java b/src/nodash/test/NoUserTest.java index a335b1a..84084f9 100644 --- a/src/nodash/test/NoUserTest.java +++ b/src/nodash/test/NoUserTest.java @@ -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 diff --git a/src/nodash/test/functional/NoRoutineTest.java b/src/nodash/test/functional/NoRoutineTest.java index 357813e..d3e5cce 100644 --- a/src/nodash/test/functional/NoRoutineTest.java +++ b/src/nodash/test/functional/NoRoutineTest.java @@ -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);