GitHub user dosubot[bot] added a comment to the discussion: How to customize 
authentication in version 6

<!-- Answer -->
Those warnings are very telling - especially **"Class 
'werkzeug.local.LocalProxy' is not mapped"**. This is a [known issue in 
Superset 6.0.0rc4](https://github.com/apache/superset/issues/36602) related to 
Flask-AppBuilder 5.x changes.

The problem is that when you call `self.find_role("Gamma")` from within 
`request_loader`, the session context isn't properly initialized yet, and 
something is getting wrapped in a LocalProxy that SQLAlchemy can't handle.

The SAWarnings about "collection append" and "Session.add() operation not 
supported within the execution stage of the flush process" indicate that 
`add_user()` is being called while SQLAlchemy is already mid-flush - this is 
the root cause.

**The harsh reality**: Based on my research, [AUTH_REMOTE_USER has known 
regressions in Superset 
6.0.0rc2+](https://github.com/apache/superset/issues/36117) that worked fine in 
5.0.0. [Multiple users have reported similar 
issues](https://github.com/apache/superset/discussions/36446#discussioncomment-15186689)
 and no clean workaround has been documented.

**Try this approach** - use `FLASK_APP_MUTATOR` to create users in 
`before_request` instead of `request_loader`, and access the security manager 
directly from appbuilder (not via LocalProxy):

```python
def init_custom_auth(app):
    @app.before_request
    def ensure_remote_user():
        from flask import request, g
        from flask_login import login_user
        
        if hasattr(g, '_remote_user_checked'):
            return
        g._remote_user_checked = True
        
        username = request.environ.get("REMOTE_USER")
        if not username:
            return
        
        # Access security manager directly, not via LocalProxy
        sm = app.appbuilder.sm
        
        user = sm.find_user(username=username)
        if not user:
            # Get role object in a fresh query
            role = 
sm.get_session.query(sm.role_model).filter_by(name="Gamma").first()
            if role:
                user = sm.add_user(
                    username=username,
                    first_name=request.environ.get("FIRST_NAME") or "",
                    last_name=request.environ.get("LAST_NAME") or "",
                    email=username,
                    role=role
                )
                sm.get_session.commit()
                user = sm.find_user(username=username)
        
        if user:
            login_user(user)

FLASK_APP_MUTATOR = init_custom_auth
```

Note: Use `sm.get_session` here since we're accessing the appbuilder directly, 
not through `self`.

If this still doesn't work, the most reliable path may be to **stay on Superset 
5.x** until the upstream AUTH_REMOTE_USER issues are resolved. This is 
unfortunately a real regression in v6.


<!-- Dosu Comment Footer -->
*To reply, just mention [@dosu](https://go.dosu.dev/dosubot).*

---
<sup>How did I do? 
[Good](https://app.dosu.dev/response-feedback/495c2525-a29d-4434-b02f-5c1b20b5ddad?feedback_type=great_response)
 | 
[Irrelevant](https://app.dosu.dev/response-feedback/495c2525-a29d-4434-b02f-5c1b20b5ddad?feedback_type=irrelevant_answer)
 | 
[Incorrect](https://app.dosu.dev/response-feedback/495c2525-a29d-4434-b02f-5c1b20b5ddad?feedback_type=incorrect_sources)
 | 
[Verbose](https://app.dosu.dev/response-feedback/495c2525-a29d-4434-b02f-5c1b20b5ddad?feedback_type=too_verbose)
 | 
[Hallucination](https://app.dosu.dev/response-feedback/495c2525-a29d-4434-b02f-5c1b20b5ddad?feedback_type=hallucination)
 | [Report 
🐛](https://app.dosu.dev/response-feedback/495c2525-a29d-4434-b02f-5c1b20b5ddad?feedback_type=bug_report)
 | 
[Other](https://app.dosu.dev/response-feedback/495c2525-a29d-4434-b02f-5c1b20b5ddad?feedback_type=other)</sup>

[![Chat with 
Dosu](https://dosu.dev/dosu-chat-badge.svg)](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github)&nbsp;[![Open
 in 
Cursor](https://dosu.dev/dosu-in-cursor.svg)](https://cursor.com/link/prompt?text=Those%20warnings%20are%20very%20telling%20-%20especially%20%2A%2A%22Class%20%27werkzeug.local.LocalProxy%27%20is%20not%20mapped%22%2A%2A.%20This%20is%20a%20%5Bknown%20issue%20in%20Superset%206.0.0rc4%5D%28https%3A//github.com/apache/superset/issues/36602%29%20related%20to%20Flask-AppBuilder%205.x%20changes.%0A%0AThe%20problem%20is%20that%20when%20you%20call%20%60self.find_role%28%22Gamma%22%29%60%20from%20within%20%60request_loader%60%2C%20the%20session%20context%20isn%27t%20properly%20initialized%20yet%2C%20and%20something%20is%20getting%20wrapped%20in%20a%20LocalProxy%20that%20SQLAlchemy%20can%27t%20handle.%0A%0AThe%20SAWarnings%20about%20%22collection%20append%22%20and%20%22Session.add%28%29%20operation%20not%20supported%20within%20the%20execution%
 
20stage%20of%20the%20flush%20process%22%20indicate%20that%20%60add_user%28%29%60%20is%20being%20called%20while%20SQLAlchemy%20is%20already%20mid-flush%20-%20this%20is%20the%20root%20cause.%0A%0A%2A%2AThe%20harsh%20reality%2A%2A%3A%20Based%20on%20my%20research%2C%20%5BAUTH_REMOTE_USER%20has%20known%20regressions%20in%20Superset%206.0.0rc2%2B%5D%28https%3A//github.com/apache/superset/issues/36117%29%20that%20worked%20fine%20in%205.0.0.%20%5BMultiple%20users%20have%20reported%20similar%20issues%5D%28https%3A//github.com/apache/superset/discussions/36446%23discussioncomment-15186689%29%20and%20no%20clean%20workaround%20has%20been%20documented.%0A%0A%2A%2ATry%20this%20approach%2A%2A%20-%20use%20%60FLASK_APP_MUTATOR%60%20to%20create%20users%20in%20%60before_request%60%20instead%20of%20%60request_loader%60%2C%20and%20access%20the%20security%20manager%20directly%20from%20appbuilder%20%28not%20via%20LocalProxy%29%3A%0A%0A%60%60%60python%0Adef%20init_custom_auth%28app%29%3A%0A%20%20%20%20%40a
 
pp.before_request%0A%20%20%20%20def%20ensure_remote_user%28%29%3A%0A%20%20%20%20%20%20%20%20from%20flask%20import%20request%2C%20g%0A%20%20%20%20%20%20%20%20from%20flask_login%20import%20login_user%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20if%20hasattr%28g%2C%20%27_remote_user_checked%27%29%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20%20%20g._remote_user_checked%20%3D%20True%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20username%20%3D%20request.environ.get%28%22REMOTE_USER%22%29%0A%20%20%20%20%20%20%20%20if%20not%20username%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Access%20security%20manager%20directly%2C%20not%20via%20LocalProxy%0A%20%20%20%20%20%20%20%20sm%20%3D%20app.appbuilder.sm%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20user%20%3D%20sm.find_user%28username%3Dusername%29%0A%20%20%20%20%20%20%20%20if%20not%20user%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Get
 
%20role%20object%20in%20a%20fresh%20query%0A%20%20%20%20%20%20%20%20%20%20%20%20role%20%3D%20sm.get_session.query%28sm.role_model%29.filter_by%28name%3D%22Gamma%22%29.first%28%29%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20role%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20user%20%3D%20sm.add_user%28%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20username%3Dusername%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20first_name%3Drequest.environ.get%28%22FIRST_NAME%22%29%20or%20%22%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20last_name%3Drequest.environ.get%28%22LAST_NAME%22%29%20or%20%22%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20email%3Dusername%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20role%3Drole%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20sm.get_session.commit%28%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20
 
user%20%3D%20sm.find_user%28username%3Dusername%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20if%20user%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20login_user%28user%29%0A%0AFLASK_APP_MUTATOR%20%3D%20init_custom_auth%0A%60%60%60%0A%0ANote%3A%20Use%20%60sm.get_session%60%20here%20since%20we%27re%20accessing%20the%20appbuilder%20directly%2C%20not%20through%20%60self%60.%0A%0AIf%20this%20still%20doesn%27t%20work%2C%20the%20most%20reliable%20path%20may%20be%20to%20%2A%2Astay%20on%20Superset%205.x%2A%2A%20until%20the%20upstream%20AUTH_REMOTE_USER%20issues%20are%20resolved.%20This%20is%20unfortunately%20a%20real%20regression%20in%20v6.)&nbsp;[![Join
 
Discord](https://img.shields.io/badge/join-5865F2?logo=discord&logoColor=white&label=)](https://go.dosu.dev/discord-bot)&nbsp;[![Share
 on 
X](https://img.shields.io/badge/X-share-black)](https://twitter.com/intent/tweet?text=%40dosu_ai%20helped%20me%20solve%20this%20issue!&url=https%3A//github.com/apache/superset/discussions/38199)

GitHub link: 
https://github.com/apache/superset/discussions/38199#discussioncomment-15905006

----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: 
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to