How to run custom code at login using fly-security

I'm new to flask, but moderately versed in python - I have a flash application that uses a flash drive to authenticate the user. I would like to add some additional features to the login process. In particular, I need to save the auth_token user (which I configured as a one-time token) in db when I log in and delete it when I log out. The problem arises because flash protection (as far as I know) does not reveal the registration mechanism directly to the developer. As far as I can tell from this code, it imports a login flag that uses the login_user function.

I started by trying to override this function by importing flask.ext.login (which I usually didn't need to do) and overriding the function as follows:

import flask.ext.login as a def new_login_user(): ...copy of existing function goes here... ...Plus new stuff with current_user.get_auth_token()... a.login_user = new_login_user 

however, I got into all kinds of namespace problems, and it seems like this is a really ugly way to do this.

I thought there might be a way to do this with a decorator, but I'm new to the bulb and haven't used decorators in many ways.

Any ideas on what could be the best way to get closer to this? for context, I want auth_token in db because I need to pass website authentication to another process that also accesses db. Another process is an API server using websockets. I do not want to combine processes.

+8
python flask flask-security
source share
2 answers

I think using a signal decoder seems the easiest - in the following example, on_user_logged_in should be called when the user logs into your application. More details in the docs .

 from flask.ext.login import user_logged_in @user_logged_in.connect_via(app) def on_user_logged_in(sender, user): log_auth_token(user.get_auth_token()) # or whatever. 
+5
source share

Better to keep it clean, write your own new_login_user function and use this whenever you would otherwise not use flask.ext.login.login_user . You can put your new_login_user in your own file and import it from there.

You can even name it login_user and never import flask.ext.login.login_user directly.

0
source share

All Articles