pallets-eco / flask-principal

Identity management for Flask applications
MIT License
492 stars 89 forks source link

Lazy load Identity object #25

Open mviamari opened 10 years ago

mviamari commented 10 years ago

Currently, the identity object is loaded on each request using Principle._on_before_request and Principle.identity_loaders.

The problem is that the identity is not already required for each request and each access to the identity implies an access to the session data.

Ideally, the identity would only be loaded when needed.

Is this a planned feature? Something that I could help out with?

joelcox commented 10 years ago

I think I'm running into somewhat the same problem. Principal looks really neat, but as far as I can see from the docs, Principal requires me to load all the permission as soon as the identity_loaded signal is fired.

This model breaks down in the following cases:

Please correct me if I made any wrong assumptions about how Principal works, I only read the docs and skimmed the underlying code.

mattupstate commented 10 years ago

@mviamari @joelcox There is an issue for this already. See #6. Unfortunately I'm still trying to figure out how to tackle this. Part of my research has been looking into well established ACL systems and how they work in the context of a web application.

joelcox commented 10 years ago

Sorry Matt, how could I've missed that! Keep up the good work.

mviamari commented 10 years ago

@mattupstate I think that the issue I'm referring to is subtly different than #6, although I can certainly see how they might be solved together. (Or maybe I didn't fully understand the issue as presented in #6.)

My issue is that since the core of the Identity object is knowing who the requesting user is, which in turn is read from the session, every request reads from the session because the Identity object is assumed to exist before the request is processed. (It is created with the identity_loaders called on the before_request hook.) Thus, even for requesting resources that have no Needs, and maybe do not even require an authenticated user, there is a session read.

This in particular can be problem if the session is persisted using a backend rather than the default direct serialization in the cookie (creates unnecessary reads) or if session accessed checks are used determine if a Vary:Cookie header is needed when returning the response (very useful when using something like Varnish to cache responses).

A possible solution here would be to delay the execution of the identity_loaders until the identity object is accessed directly. I think this can be done separately from the loading of the provides for the Identity object so as to prevent a future conflict with #6.