GitHub user dosubot[bot] added a comment to the discussion: How to customize
authentication in version 6
<!-- Answer -->
After more research, I need to correct my earlier suggestion -
`auth_user_remote_user()` is actually defined in Flask-AppBuilder (not
Superset), and given the v6 architecture changes we discussed, overriding it
may not be the cleanest approach.
**Better approach for Superset 6**: Override the `request_loader` method in
your custom security manager to intercept authentication at the Flask-Login
level:
```python
from superset.security import SupersetSecurityManager
from flask import request
from flask_login import login_user
import logging
logger = logging.getLogger(__name__)
class CustomSecurityManager(SupersetSecurityManager):
def request_loader(self, request):
"""Called by Flask-Login on every request to load the user."""
remote_user = request.environ.get('REMOTE_USER')
if not remote_user:
logger.debug("No REMOTE_USER in environment")
return None
logger.info(f"REMOTE_USER found: {remote_user}")
# Get or create user
user = self.find_user(username=remote_user)
if not user:
# Read custom attributes from environment (set by your middleware)
first_name = request.environ.get('FIRST_NAME', '')
last_name = request.environ.get('LAST_NAME', '')
roles_str = request.environ.get('ROLES', 'Gamma')
# Parse roles
role_names = [r.strip() for r in roles_str.split(',') if r.strip()]
roles = [self.find_role(rn) for rn in role_names if
self.find_role(rn)]
user = self.add_user(
username=remote_user,
first_name=first_name or 'Unknown',
last_name=last_name or 'User',
email=f"{remote_user}@example.com",
role=roles or [self.find_role('Gamma')]
)
self.session.commit() # Note: use .session not .get_session in v6
logger.info(f"Created new user: {remote_user}")
else:
# Optionally update user attributes on each login
first_name = request.environ.get('FIRST_NAME')
last_name = request.environ.get('LAST_NAME')
if first_name:
user.first_name = first_name
if last_name:
user.last_name = last_name
self.session.commit()
return user
```
Then in `superset_config.py`:
```python
from flask_appbuilder.security.manager import AUTH_REMOTE_USER
AUTH_TYPE = AUTH_REMOTE_USER
CUSTOM_SECURITY_MANAGER = CustomSecurityManager
ADDITIONAL_MIDDLEWARE = [YourMiddleware]
LOG_LEVEL = logging.DEBUG
```
This bypasses the view layer entirely and handles authentication at the request
level, which should work regardless of the React frontend changes in v6.
[Make sure your middleware properly sets the environment
variables](https://github.com/apache/superset/discussions/36446) - use
`environ['REMOTE_USER']` (not headers) for the username since that's what
`AUTH_REMOTE_USER` expects.
<!-- 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/7da007f4-7a76-4c4f-a267-d3d2b948ad08?feedback_type=great_response)
|
[Irrelevant](https://app.dosu.dev/response-feedback/7da007f4-7a76-4c4f-a267-d3d2b948ad08?feedback_type=irrelevant_answer)
|
[Incorrect](https://app.dosu.dev/response-feedback/7da007f4-7a76-4c4f-a267-d3d2b948ad08?feedback_type=incorrect_sources)
|
[Verbose](https://app.dosu.dev/response-feedback/7da007f4-7a76-4c4f-a267-d3d2b948ad08?feedback_type=too_verbose)
|
[Hallucination](https://app.dosu.dev/response-feedback/7da007f4-7a76-4c4f-a267-d3d2b948ad08?feedback_type=hallucination)
| [Report
🐛](https://app.dosu.dev/response-feedback/7da007f4-7a76-4c4f-a267-d3d2b948ad08?feedback_type=bug_report)
|
[Other](https://app.dosu.dev/response-feedback/7da007f4-7a76-4c4f-a267-d3d2b948ad08?feedback_type=other)</sup>
[](https://app.dosu.dev/a28d3c7e-a9d3-459e-9fb6-3a6f9ff4f357/ask?utm_source=github) [](https://cursor.com/link/prompt?text=After%20more%20research%2C%20I%20need%20to%20correct%20my%20earlier%20suggestion%20-%20%60auth_user_remote_user%28%29%60%20is%20actually%20defined%20in%20Flask-AppBuilder%20%28not%20Superset%29%2C%20and%20given%20the%20v6%20architecture%20changes%20we%20discussed%2C%20overriding%20it%20may%20not%20be%20the%20cleanest%20approach.%0A%0A%2A%2ABetter%20approach%20for%20Superset%206%2A%2A%3A%20Override%20the%20%60request_loader%60%20method%20in%20your%20custom%20security%20manager%20to%20intercept%20authentication%20at%20the%20Flask-Login%20level%3A%0A%0A%60%60%60python%0Afrom%20superset.security%20import%20SupersetSecurityManager%0Afrom%20flask%20import%20request%0Afrom%20flask_login%20import%20login_user%0Aimport%20logging%0A%0Alogger%20%3D%20logging.get
Logger%28__name__%29%0A%0Aclass%20CustomSecurityManager%28SupersetSecurityManager%29%3A%0A%20%20%20%20%0A%20%20%20%20def%20request_loader%28self%2C%20request%29%3A%0A%20%20%20%20%20%20%20%20%22%22%22Called%20by%20Flask-Login%20on%20every%20request%20to%20load%20the%20user.%22%22%22%0A%20%20%20%20%20%20%20%20remote_user%20%3D%20request.environ.get%28%27REMOTE_USER%27%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20if%20not%20remote_user%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20logger.debug%28%22No%20REMOTE_USER%20in%20environment%22%29%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20None%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20logger.info%28f%22REMOTE_USER%20found%3A%20%7Bremote_user%7D%22%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%23%20Get%20or%20create%20user%0A%20%20%20%20%20%20%20%20user%20%3D%20self.find_user%28username%3Dremote_user%29%0A%20%20%20%20%20%20%20%20%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%20Read%20custom%20attributes%20from%20environment%20%28set%20by%20your%20middleware%29%0A%20%20%20%20%20%20%20%20%20%20%20%20first_name%20%3D%20request.environ.get%28%27FIRST_NAME%27%2C%20%27%27%29%0A%20%20%20%20%20%20%20%20%20%20%20%20last_name%20%3D%20request.environ.get%28%27LAST_NAME%27%2C%20%27%27%29%0A%20%20%20%20%20%20%20%20%20%20%20%20roles_str%20%3D%20request.environ.get%28%27ROLES%27%2C%20%27Gamma%27%29%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Parse%20roles%0A%20%20%20%20%20%20%20%20%20%20%20%20role_names%20%3D%20%5Br.strip%28%29%20for%20r%20in%20roles_str.split%28%27%2C%27%29%20if%20r.strip%28%29%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20roles%20%3D%20%5Bself.find_role%28rn%29%20for%20rn%20in%20role_names%20if%20self.find_role%28rn%29%5D%0A%20%20%20%20%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20%20%20%20%20user%20%3D%20self.add_user%28%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20username%3Dremote_user%2C%0A%20%20%2
0%20%20%20%20%20%20%20%20%20%20%20%20%20first_name%3Dfirst_name%20or%20%27Unknown%27%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20last_name%3Dlast_name%20or%20%27User%27%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20email%3Df%22%7Bremote_user%7D%40example.com%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20role%3Droles%20or%20%5Bself.find_role%28%27Gamma%27%29%5D%0A%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%20self.session.commit%28%29%20%20%23%20Note%3A%20use%20.session%20not%20.get_session%20in%20v6%0A%20%20%20%20%20%20%20%20%20%20%20%20logger.info%28f%22Created%20new%20user%3A%20%7Bremote_user%7D%22%29%0A%20%20%20%20%20%20%20%20else%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%23%20Optionally%20update%20user%20attributes%20on%20each%20login%0A%20%20%20%20%20%20%20%20%20%20%20%20first_name%20%3D%20request.environ.get%28%27FIRST_NAME%27%29%0A%20%20%20%20%20%20%20%20%20%20%20%20last_name%20%3D%20request.environ.get%28%27LAST_NAME%2
7%29%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20first_name%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20user.first_name%20%3D%20first_name%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20last_name%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20user.last_name%20%3D%20last_name%0A%20%20%20%20%20%20%20%20%20%20%20%20self.session.commit%28%29%0A%20%20%20%20%20%20%20%20%0A%20%20%20%20%20%20%20%20return%20user%0A%60%60%60%0A%0AThen%20in%20%60superset_config.py%60%3A%0A%60%60%60python%0Afrom%20flask_appbuilder.security.manager%20import%20AUTH_REMOTE_USER%0A%0AAUTH_TYPE%20%3D%20AUTH_REMOTE_USER%0ACUSTOM_SECURITY_MANAGER%20%3D%20CustomSecurityManager%0AADDITIONAL_MIDDLEWARE%20%3D%20%5BYourMiddleware%5D%0ALOG_LEVEL%20%3D%20logging.DEBUG%0A%60%60%60%0A%0AThis%20bypasses%20the%20view%20layer%20entirely%20and%20handles%20authentication%20at%20the%20request%20level%2C%20which%20should%20work%20regardless%20of%20the%20React%20frontend%20changes%20in%20v6.%0A%0A%5BMake%20sure%20your%20midd
leware%20properly%20sets%20the%20environment%20variables%5D%28https%3A//github.com/apache/superset/discussions/36446%29%20-%20use%20%60environ%5B%27REMOTE_USER%27%5D%60%20%28not%20headers%29%20for%20the%20username%20since%20that%27s%20what%20%60AUTH_REMOTE_USER%60%20expects.) [](https://go.dosu.dev/discord-bot) [](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-15902636
----
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]