Retrieve authorization token from external auth method

Hello everyone, I’ve managed to create simple auth plugin, but I want to make it available via API. Hoe can I retrieve authorisation token to return it as json(without instance’s code modiication)?

At this point, as far as I understand, successful request is getting redirected. Peertube source code:

My plugin code is simple and looks like this:

Thanks for your attention.

Hi,

Why do you want to send the token to the client?

You can do such thing using a special route in the plugin that returns this token, or you could use a cookie.

1 « J'aime »

Yes, exactly, I want to send token back to client for further API interactions (because I use an external website to upload videos to peertube etc.). But how can I get this token from peertube as a variable? Maybe it mentioned in the docs somewhere

Related issue on github: https://github.com/Chocobozzz/PeerTube/issues/3959

As I said, you can use a cookie or store the token in the plugin storage and then send it using the router.

1 « J'aime »

Thank you, that is clear. But how can I access token value? Because all I have after calling result.userAuthenticated hook in my response is an html page with two query params - externalAuthToken and username. That is fine, but I want to get raw token value string to immediately write something like res.json({token: externalAuthToken}), send it back to the client side for further peertube authorization, and later store it, handle it etc.

I could have changed instance source code, but it would be way less optimal in my opinion.

Peertube instance code:

Could you explain your use case? I’m not sure to understand what you want to do (why do you need the same token than the client instead of generating another one for your external website?)

1 « J'aime »

I’m working with blockchain-based social network to which I want to embed peertube videos (users can upload them directly from my website etc.). So I want to add authorisation method similar to that network (which checks crypto signature). After successful authorisation I want to send back external auth token, that can be used to get the Bearer: Token for further interactions with peertube API (as far as I understand, it can be retrieved with externalAuthToken option in /token server call).

So my final goal is to get bearer token using external auth to work with peertube API.

Then use action:auth-user.information-loaded (available peertube >= 3.2) https://docs.joinpeertube.org/api-plugins?id=client-hooks to send the access token to your application fetched from the local storage.

But honestly I don’t think it will work. If you want that your application acts as a peertube client (to upload videos etc), then use the peertube REST API to login your users directly from your application.

1 « J'aime »

Yes, this is the way I want users interact with my app. I want to log them in automatically with the same « credentials » they use in my website. And I make a call from the code to the route I’ve registered to my auth method.

Maybe I had better use registerIdAndPassAuth? I decided to use registerExternalAuth because I need to pass an object to login function such as {signature: 'abc', address: 'abc', nonce: 'asd', pubKey: 'asd'}. Can I somehow pass additional data to the login function (other than id and password)? I could have used JSON.parse option, but that seemed to be an overkill.

If I could somehow retrieve request parameters in the login function, that would have solved all my problems.

And one more important question: could I access res and req objects in the registerIdAndPassAuth to send custom errors to the client?

I kind of solved this using method overloading:

Not sure, if it is the most clear way to do so, but it works, and I am retrieving a string, that contains both username and token.

By the way, for how long external auth token is valid? Can I alter this value?