[WIP] Refactor SSO Authentication

I'm looking at this again.