Registering custom \OCP\IUserSession implementation


I am trying to integrate a custom auth provider with nextcloud. I have an external server that has an api which will check a token to verify if the user is looged in to the auth provider. My thought on how to do this was to override \OCP\IUserSession to check the external API instead of the server session. The issue I am having is I can’t figure out how to register the new IUserSession. I have created an app, and in the registration method of lib/AppInfo/Application.php have tried both:

$context->registerService(\OCP\IUserSession::class, function(){
            return new NTSession();


OC::$server->registerAlias(\OCP\IUserSession::class, NTSession::class);

The first appears to do nothing, and the second errors out with a message “Fatal error: Cannot declare class OCA\NewtechServices\Auth\NTSesssion, because the name is already in use in /var/www/html/custom_apps/newtechservices/lib/Auth/NTSession.php

Ami doing this completely wrong? How are you ssupposed to register new implementations of the User Session?

You shouldn’t do this. The CI system is not mean to be used to overwrite something. Any existing object built prior to your call would still have the old user session implementation.

Is there a correct way to change the auth behavior from an application? I was using the sso application(GitHub - nextcloud/user_saml: App for authenticating Nextcloud users using SAML) as kind of a guide, but from what I can tell that only adds another provider to authenticate username and password against, not a way to bypass the login screen, and a lot of the methods it was using were marked as legacy, so I’m not sure I should use them.

Take a look at user_oidc/Backend.php at master · nextcloud/user_oidc · GitHub. I think that is a solid base to start with and an app that wasn’t developed too long ago.

Of course let us please know if things are not clear. I’m afraid that API isn’t much documented but I have the feeling you know your way around to explore it :slightly_smiling_face: