Restructure authentication to be more abstract, added unit tests to authentication