Horizon: authType: token fails with Error: Attempting to authenticate with a token, but no token is present(…)

Created on 21 Jul 2016  Â·  12Comments  Â·  Source: rethinkdb/horizon

Server version: 2.0.0 fa365093912cfb4047b393f6ad8065f59798c470
Client version: 2.0.0 fa365093912cfb4047b393f6ad8065f59798c470

This issue surfaced a few days ago but I haven't been able to find what/when changed.

Previously I could do the following:

var horizon = Horizon({ host: 'localhost:8181', authType: 'token', secure: true });
if (!horizon.hasAuthToken()) {
    horizon.authEndpoint('twitter').subscribe((endpoint) => {
        window.location.assign(endpoint);
});
} else {
    // We have a token already, do authenticated Horizon stuff here
    horizon.connect();
}
horizon.onReady(function () {
    document.querySelector('h1').innerHTML = 'test001 works!';
    horizon.currentUser().fetch().subscribe(function (u) {
        console.log(JSON.stringify(u))
    });
});

Now doing so results in:

Error: Attempting to authenticate with a token, but no token is present(…)

so the app is not able to open up Twitter for authorization to get a new token. This still works with Horizon 1.1.3.

@deontologician this is per our discussion on the Horizon slack channel.

ready for review bug client

All 12 comments

Thanks, looking into this now

Ok, I have a fix up in #667

@michaelwills any chance you could check if this works for you? (there may be further bugs lurking)

@deontologician Will do and let you know. Thanks much!

Unfortunately @deontologician I still get the same error. I'll have to see if I can get into the source to see the cause some time.

@michaelwills Sorry about that. I'm going to try fully replicating today. I had partially replicated it yesterday when I ran into a bug that seemed to cause the same error message.

I merged the previous fix, but I've not confirmed this issue is fixed, so opened

By adding authType: 'token' when initialising Horizon I get the error:

Attempting to authenticate with a token, but no token is present

Is this part of this same bug or am I doing something wrong? Shouldn't by app at least initialise in order for the client to get a token?

You have to check for an authKey, redirect to oauth if there isn't one, and
connect if you have one. This bug is where you check explicitly for
.hasAuthKey, it says yes, so you connect and the server says no actually you dont
have an authKey

On Wed, Jul 27, 2016, 13:42 Luandro [email protected] wrote:

By adding authType: 'token' when initialising Horizon I get the error:

Attempting to authenticate with a token, but no token is present

Is this part of this same bug or am I doing something wrong? Shouldn't by
app at least initialise in order for the client to get a token?

—
You are receiving this because you modified the open/close state.

Reply to this email directly, view it on GitHub
https://github.com/rethinkdb/horizon/issues/663#issuecomment-235714160,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AAAFVtpisDXY_UJILMU9AT-YO_LPzD6oks5qZ8KagaJpZM4JSEKH
.

Missed notifications on this @deontologician. I hadn't seen the updates but came to the same conclusion you noted.

I think it's a bit counterintuitive in that the check for hasAuthToken is on the _instance_ of horizon as opposed to the main Horizon object. This means we have to attempt instantiating the token invocation, and if that fails with a try/catch, instantiate the anonymous version, do the horizon.hasAuthToken() check, and then do the redirect.

Horizon.clearAuthTokens(); // make sure the tokens are cleared for testing
var horizon; // we don't know which one to instantiate yet
try {
    horizon = Horizon({ host: 'localhost:8181', authType: 'token', secure: true });
}
catch(err) {
    console.log(err);
    // OK we had an error. It's most likely the auth token is missing so lets try an anonyous one
    horizon = Horizon({ host: 'localhost:8181', secure: true });
    if (!horizon.hasAuthToken()) {
        // ok now we know so lets get authorized
        horizon.authEndpoint('twitter').subscribe((endpoint) => {
            window.location.assign(endpoint);
        });
    }
}
// We have a token already, do authenticated Horizon stuff here. We will bounce to auth before this if 
// we don't have a token available.
horizon.onReady(onReady);
horizon.connect();

function onReady() {
    document.querySelector('h1').innerHTML = 'test001 works!';
    horizon.currentUser().fetch().subscribe(function (u) {
        console.log(JSON.stringify(u))
    });
    horizon.utensils.sendRequest('jt_test', {}).subscribe(res => console.log(res));
}

Is there a way to "upgrade" an anonymous horizon instance if we find auth tokens _are_ present?

@deontologician so we can do the following now

horizon = Horizon({ host: 'localhost:8181', secure: true });
if (!horizon.hasAuthToken()) {
    // ok now we know so lets get authorized
    horizon.authEndpoint('twitter').subscribe((endpoint) => {
        window.location.assign(endpoint);
    });
}

If a token is not present we can get one via the Oauth sequence, otherwise we can connect as usual. The docs example:

horizon = Horizon({ host: 'localhost:8181', authType: 'token', secure: true });
if (!horizon.hasAuthToken()) {
    // ok now we know so lets get authorized
    horizon.authEndpoint('twitter').subscribe((endpoint) => {
        window.location.assign(endpoint);
    });
}

fails with an uncaught error which prompted the try/catch implementation above. Am I correct in saying we can't and shouldn't use this authType: "token" methodology then?

Alright, I finally replicated and got to the bottom of this in #752 . So the docs need to be updated to use window.location.replace to go along with this.

Thanks much @deontologician. I'll be able to continue again shortly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

arthurvi picture arthurvi  Â·  4Comments

josephg picture josephg  Â·  6Comments

arthurvi picture arthurvi  Â·  7Comments

deontologician picture deontologician  Â·  3Comments

marshall007 picture marshall007  Â·  8Comments