Expose the authorisation mechanism, allow queries to be saved.

Also fix a few bugs, including fetchTemplates.