Change VideoQuota value at SAML login

Hi,

I am trying to create a plugin that modifies videoQuota value of a user when he logs in. I’m using the action:auth-user.information-loaded hook to intercept the user and modify its profile.

I was able to install it but it doesn’t affect the videoQuota value as expected. Here’s the code I made just to test if I’m able to affect it. In the end, the goal is to change VideoQuota value in the peertube-plugin-auth-saml2 according to some SAML data associated with each user.

function register ({ registerHook, peertubeHelpers })
{    
    registerHook({
      target: 'action:auth-user.information-loaded',
      handler: ({ user }) =>
          user.videoQuota = 0
    })
}

export {
  register
}

I was not that confident that it would work, as it probably needs some saving and/or refreshing. Maybe the hooks happens when it’s too late too, I don’t know. I’m totally new to Angular, nodeJS, TypeScript and PeerTube so please forgive me if my post makes no sense ^^

Any idea if it can work and if I’m on the right direction ?

Thanks

With this hook, you only get the current user data. These data are not saved afterwards.

I think that the proper way to include this in the peertube-plugin-auth-saml2 plugin, is to ask the Peertube core team to add an optional «videoQuota» parameter for store.userAuthenticated method:

This will initiate the quota according to your plugin on the user creation. And only on the creation.

The dirty way is to use PeerTubeHelpers.database.query to do the modification in database. But:

  • this is dirty. If the database model changes, your plugin will be broken
  • the action:auth-user.information-loaded hook is not the good one. You should to this on user creation (see the first solution)

Note: there is another solution. If quota may change, and you want to update them periodicaly: you can do a daily/hourly batch on the Peertube or SAML server, that uses the REST API to check and update every user quota.
If you want to include this batch directly in a plugin, I think you can’t use the REST API. In such case, you should ask Peertube core team for a new backend helper for changing a user quota.

Yeah that was my conclusion too (I first tried on peertube-plugin-auth-saml2). I was hoping to be wrong.
Thank you for your answer.

For the REST API, I need to compare some values associated with one user to set the appropriated VideoQuota value. For example :

  • Some-Custom-SAML-Attribute$SchoolA$User => 0 Mo
  • Some-Custom-SAML-Attribute$SchoolB$Teacher => 100 Mo

These custom attributes are not stored anywhere, only provided in the samlUser object retrieved in the plugin, and have to be re-evaluated at each connection…

I’m surprised that there isn’t a hook (or another way) to manipulate user or any other component from a plugin. I guess the only way is to rebuild PeerTube from source myself, so I can modify userAuthenticated…

I also noticed that the UserService provides a function like updateMyProfile, so maybe something similar would be doable from a plugin, with the user as a parameter ?

@Chocobozzz added some filters in the hello world plugin example not long time ago so I’m surprised that it’s not possible to affect every aspect of the user from a plugin, especially from an authentication plugin.

I’m not blaming anyone, PeerTube is an incredible project and I can’t imagine the amount of work needed to handle it. I’m just hoping for both of us to be wrong :smiley:

Thank you again.

Just ask for the feature on github: Issues · Chocobozzz/PeerTube · GitHub
Chocobozzz can’t spend hours to guess all plugin developper needs. It must be written somewhere in the plugin documentation: if a feature is missing for plugins, just ask.
Chocobozzz usualy add wanted features on the next release.

Found it in the documentation:

Don’t try to require parent PeerTube modules, only use peertubeHelpers. If you need another helper or a specific hook, please create an issue