Pass an async function the provider to obtain the auth token
Hey there, first of all thanks for the amazing work on hocuspocus! 🙏
The problem I am facing
We are using firebase authentication and therefore are passing in the firebase token to the HocuspocusProvider. Firebase tokens expire after 1 hour, but a fresh token can always be retrieved using the async function getIdToken on the user. Currently however, we can only supply the token to the provider as a string value once, when we instantiate the provider. The problem now is, when the token is already expired, but the provider tries to reconnect, the authentication will fail, as Hocuspocus keeps in memory the expired token. Therefore we need to handle this case on our side and completely recreate the provider with a fresh token.
The solution I would like We were thinking maybe it would be cool to pass a function to the provider that returns the string as a promise, and before the AuthenticationMessage gets sent, the provider could resolve this promise to obtain the token, therefore making sure that there's always a fresh token.
Alternatives I have considered As mentioned, the alternative for us is that we recreate the provider completely. We then need to distinguish between a real authentication failure (because of an altered token) and an expired token in the onAuthenticationFailed hook to know if we need to actually recreate the provider or show an authentication error message.
That function would be called only once though, right? When the token is expired the connection would close, the function would get executed again und new token would be sent with the next connection attempt, right?
Would it make sense to update the token before the connection has to close?
// Add a new method to update the token …
provider.updateToken('new-secret-token')
// … which sends a new Auth/Token message to the server.
Probably too complex, your suggested solution is probably easier.
Related: I plan to add a .reconnect() method to the provider, that would allow to force a reconnect and (optional) overwrite the configuration. I wanted to add this to make switching documents easier, but it would work for your use case, too:
provider.reconnect({ token: 'new-secret-token' })
Your solution is probably the best though. What do you think?
Thanks for the quick answer!
Yes, exactly, the way I had it in mind was that the function could be called and the promise resolved right before sending the AuthenticationMessage, so something like this:
if (this.isAuthenticationRequired) {
this.options.getToken().then((token) => {
this.send(AuthenticationMessage, { token })
})
return
}
The drawback that I see here is regarding the API, because right now you can pass in the token as a string, but such a change would break this API (assuming that you don't want to support both ways for simplicity).
I guess with your solution - updating the token directly in the provider - this would be prevented.
Maybe the updateToken could be implemented without the side-effect of reconnecting? Because right now, the reconnection logic is happening within the Hocuspocus provider anyways, we just need to make sure that the provider has the freshest token from firebase the next time it tries to authenticate again.
The issue with this is that we need to make sure somehow that the updateToken runs before the next time the provider sends the AuthenticationMessage, because we'd need to set this asynchronously, as we need to call the async function from firebase first. :( With the token function passed to the options, this would not be a problem, but it would require a breaking change.
What do you think about this? Would a breaking change here be justified?
On second thought, we just had the idea that the token option could be either string (so static) or it could be a function which returns promise, and this could be checked and if needed resolved before sending the AuthenticationMessage. That way, we could keep the existing behaviour.
Just a question, given your token is valid for 1 hour but Authentication happens only on connect, how do you handle this? Do you close connection and connect again with new token after 1 hour?
I think provider.reconnect({ token: 'new-secret-token' }) mentioned by Hans would be good for this.
Yes, absolutely, this would definitely make sense.
Right now, we don't handle this case specifically on the client side, but we can close connections from users serverside if needed (e.g. if they delete the account) and therefore forcing them to reauthenticate. It would definitely make sense to have the reconnect feature.
Just a minor detail, but with the new PR (which I’ll merge and release today) and the proposed reconnect() method it would be possible to do just that to force a reconnect:
const provider = new HocuspocusProvider({
token: () => {},
// …
})
// no parameter needed then, a new token will be generated from the function
provider.reconnect()
Closing this, as it is solved :)