Refactor authentication code

Refactor auth out into a separate package to make it easier to add more auth mechanisms in future.

This is in preparation for fixing issue #71.