Add ensureauthenticated function. Fixes #9