Extract window opener

We're going to use this in a Phonegap environment, and it was a bit hard to override the part where a window is opened without copy/pasting the whole `authorize` function. This PR should make that easier.