Authenticate with token

Hi.

Iā€™m trying to develop an app that a third party service will send a request to. Iā€™m able to do this, but I have some problems with the authentication process.

Right now when the request is sent the only response that the third party service get is ā€œUser is not currently logged in.ā€ This must be because the third party service havenā€™t authenticated itself yet. Manually going to the same route while logged in to nextcloud through an url shows me the response.

Now Iā€™ve tried to send a token (which I get from nc_token cookie) to the third party service, and it uses it as part of its request both in its body (form) and as a authentication header bearer token. This used to work when I was working on this app on nc16, but now on a new server with NC18 it doesnā€™t work, giving the same error message.

How should I authenticate such a request to nextcloud? Any other apps that does this?

I guess you need the @PublicPage annotation for the controller method.

2 Likes

I think your idea lets me access the method, but there is a problem. I donā€™t want just anyone to access the page, and even if I did, it uses member functions such as getUID, which means making it a public page only returns an error that ā€œMessage: Call to a member function getUID() on nullā€.

Any idea how to get around this, or another way to solve this?

When you want to do your own authentication, my understanding is that you need to use a @PublicPage and do authentication yourself. The session manager letā€™s you set a user, for example. So you could verify a token provided as part of the request and set the corresponding user to the session.

2 Likes

Makes sense. I can see that you can set an user with the user session manager, but that requires IUser interface as a parameter and maybe even a login attempt afterward, which I would imagine is hard to do without a password unless token works for passwords. Could you eleborate on how you could do this?

Hi!
Did you get any further in your project? I would like to understand the login-procedure by myself since i want to develop a custom login-service by myself (Custom Login Controller)
in my case the $userSession->login(ā€œusernameā€,ā€œpassword") returns true, but nothing happens afterwards. do you know how to actually login (get session-cookies etc.?)

Hi.

For me it works in a @PublicPage controller with the $this->userSession->login(ā€˜usernameā€™, ā€˜passwordā€™);

It allowed me to do what I needed to do, but I didnā€™t actually want to login the user, I just wanted to authenticate the controller. I donā€™t think it by itself is enough to be logged in. Another project we had where we actually wanted to login a user we had to do something like this:

$this->userSession->getSession()->regenerateId();
$this->userSession->createSessionToken($request, $user->getUID(), $user->getUID());
$this->userSession->login(ā€˜usernameā€™, ā€˜passwordā€™);
$this->userSession->createSessionToken($request, $user->getUID(), $user->getUID());

I donā€™t have the excact code and donā€™t know excatly how it worked but hopefully this can be of help.

1 Like