Refactoring and unit tests for libraries/plugin/auth/*