#1 2020-12-16 11:58:01

Horbs
Member
Registered: 2014-04-20
Posts: 23

How to hook an Authentication event on the Server.

I need to store some user variables for the life of a Server User Session.
These vars will be used to populate some records in Interface Service requests throughout the user session life cycle.
What I'd like to achieve is to identify some kind of event that gets triggered by mORMot after a successful authentication, and be able to hook that event from my code. I'd then want to store that info (User TID and AuthGroup etc) for the life of that User Session in a way that's accessible to Service Requests (I'm thinking of SessionGetUser to retrieve the info, but knowing how to and what to hook has me stumped).

Can anyone suggest a way to automatically capture the result of the authentication process so that I can fire off a method to store the required user info?

Many thanks

Horbs

Offline

#2 2020-12-16 13:09:14

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 211

Re: How to hook an Authentication event on the Server.

Horbs wrote:

What I'd like to achieve is to identify some kind of event that gets triggered by mORMot after a successful authentication

TSQLRestServer.OnSessionCreate event is what you're looking for.

From what you've described I would suggest Client-Server services via interfaces.

You define an interface, on server side you register a class that implements that interface, setting instance lifetime to sicPerSession. Now you can store user variables in that object instance which is available for the lifetime of the user session.

Offline

#3 2020-12-18 10:17:10

Horbs
Member
Registered: 2014-04-20
Posts: 23

Re: How to hook an Authentication event on the Server.

Yes, I am already using Interface based Services.

OnSessionCreate looks good and will give me some of what I need (Session User Info). I will also  need to add some other variables derived from User Requests over the life of the User Session.

  "You define an interface, on server side you register a class that implements that interface, setting instance lifetime to sicPerSession. Now you can store user variables in that object
  instance which is available for the lifetime of the user session."

Not sure how to implement this. I'm thinking that I'll need to create some form of simple storage construct - maybe an in memory TSQLTable instance for the life of the User Session.
Problem is that it can't be created until after OnSessionCreate is invoked (but maybe I can instantiate it within the OnSessionCreate function).
Also, I don't know yet how to get one interface method to call another interface method on the server side (I'm assuming I'd need to invoke the sicPerSession method from within OnSessionCreate).

I'll have a play during the next few days and see what I can come up with.

Many thanks pvn0

Horbs

Offline

#4 2020-12-18 10:58:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,660
Website

Re: How to hook an Authentication event on the Server.

FIrst of all, I don't think you need to hook OnSesssionCreate.
YOu have the session information in the ServiceCUrrentContext threadvar.

If you need to store some information which remain between calls, use a sicPerClient kind of service.
Or a sicPerSession if it is enough for you.

Then add the fields to the implementation class, which instance will remain during the client fake class or session lifetime.

Offline

Board footer

Powered by FluxBB