question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Proposal - change Security object to accept instances instead of classes

See original GitHub issue

Core Security object now accepts classes for various logic - registration, login, etc

flask_security.Security(
        app=application,
        datastore=user_datastore,
        register_form=MyCustomUserRegistrationForm,
        confirm_register_form=MyCustomConfirmUserRegistrationForm,
        login_form=MyCustomLoginForm)

Passing custom logic as classes means we don’t have a chance to seed objects performing validation with arguments outside of what FlaskSecurity envisioned in constructors for base/reference classes.

Say that for whatever reason I need to run some logic against Redis service in my login form - maybe I need to check if user isn’t logged already from too many other machines because I run Netlifx and people are trying to share their accounts. I don’t see any non-hackish way of injecting redis service instance to validation performed by MyCustomLoginForm now.

If instead of classes, Security object would accept instances that need to comply with expected interface (say have validation(payload) function), then I can easily seed my object with whatever arguments I need to do my complex custom logic that FlaskSecurity never envisioned:

class MyCustomLoginForm

  def __init__(self, redis_service):

    self.redis_service = redis_service

  def validate(self, payload):

      # ...


flask_security.Security(
        app=application,
        datastore=user_datastore,
        ...
        login_form=MyCustomLoginForm(my_redis_service))

In short - if Flask Security was accepting objects instead of classes, we would have flexibility to construct objects as we want, which would help with implementing arbitrary validation logic. With current setup we don’t have this ability.

Or to phrase it a bit different: Flask Security doesn’t allow me to pass arbitrary data to my own validation logic. Well, why?

I understand this is quite a large change, but it’s a pattern I see over and over in various frameworks - ones that expect classes to configure them limit users in what they can do in hooks, while ones that accept instance give users much more freedom for complex logic in hooks. Does the proposal look reasonable to others? Or maybe there is already a clean method to achieve what I’m trying to do here that I don’t realize?

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
kuba-lilzcommented, Oct 20, 2022

Using with app.test_request_context() I got my custom registration form builder to work 😃

0reactions
jwag956commented, Oct 19, 2022

Ahh yes - forms really really want a request context - I was hoping to get away with an app context - but no luck. The answer is simply to do

with app.test_request_context():
  fi = MyLoginForm(xxxxx)

I have updated the PR and added a specific test for CSRF.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Instance and prototype fields · Issue #265 · tc39/proposal-class ...
It describes the function and its prototype as an object. With instance fields, class body becomes mixed code, where some parts are evaluated...
Read more >
Using Amazon S3 storage classes - AWS Documentation
Setting the storage class of an object. To set and update object storage classes, you can use the Amazon S3 console, AWS SDKs,...
Read more >
ChangeProposal Class (Microsoft.VisualStudio.Data.Schema ...
Gets or sets a value that indicates whether this ChangeProposal will be included in the changes to be applied. Public property, ProjectName, Specifies...
Read more >
Understanding Class Inheritance in Python 3 | DigitalOcean
One way that object-oriented programming achieves recyclable code is through inheritance, when one subclass can leverage code from another base ...
Read more >
Define objects and their attributes with classes
Log in or subscribe for free to enjoy all this course has to offer! You've probably heard the term object in a programming...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found