Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Add flag to identify whether a user was logged in via SSO #15

Open
jkgenser opened this issue Mar 14, 2024 · 1 comment

Comments

@jkgenser
Copy link

I would like to add a flag to the session like request.session["is_sso"] to indicate to other parts of my app that the user's session was established via SSO.

In order to support this feature, you could simply add a flag to the request that tells us that it was authenticated via SSO or via username/password.

This would allow us to use a django signal on login to then set the field.

Another option is to add ability to pass an on_logged_in callback so users of your library can modify the session immediately on establishment with arbitrary logic.

@matthiask
Copy link
Owner

Hi @jkgenser

I don't think it's documented anywhere but you could maybe achieve this already with the user.backend attribute:
https://github.com/django/django/blob/cbf1e87398a58737e27e1b680283903caf661f90/django/contrib/auth/__init__.py#L87 This wouldn't work if you wanted to differentiate between uses of the django-authlib ModelBackend of course.

You could also write your own email_login helper:

from functools import partial
from authlib.views import email_login, oauth2
def my_email_login(request, **kwargs):
    user, created = email_login(request, **kwargs)
    if user:
        request.session["is_sso"] = True
    return user, created

my_oauth2 = partial(oauth2, email_login=email_login)

Or maybe even better, override post_login_response with your own callable which does all the things you want?

post_login_response=post_login_response,

I'm not against adding additional functionality to django-authlib, e.g. a signal or something, if there are good reasons why the existing hooks aren't sufficient?

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants