django-saml2-auth icon indicating copy to clipboard operation
django-saml2-auth copied to clipboard

Configurable authentication backend

Open henxing opened this issue 2 years ago • 2 comments

Currently, the authentication backend passed to the django.contrib.auth.login method is hard-coded to "django.contrib.auth.backends.ModelBackend". This makes it impossible to use this library if you use any other authentication backend, e.g. a custom one. Since the django settings dict is already imported in views.py, one implementation method could be to replace the hard-coded line with

model_backend = dictor(
    settings,
    "AUTHENTICATION_BACKENDS.0",
    default="django.contrib.auth.backends.ModelBackend"
)

This will look at the current list of authentication backends defined in settings.py and return the first one from the list.

henxing avatar Jul 21 '22 15:07 henxing

I'm forking the repo to do this, as I need this functionality right now, but I'll open a PR back into this repo once it's ready for primetime.

Also, thanks for adopting the abandoned library @mostafa!

henxing avatar Jul 21 '22 15:07 henxing

Ahh, looks like dictor doesn't like the settings object, but this works

if hasattr(settings, "AUTHENTICATION_BACKENDS") and settings.AUTHENTICATION_BACKENDS:
    model_backend = settings.AUTHENTICATION_BACKENDS[0]
else:
    model_backend = "django.contrib.auth.backends.ModelBackend"

henxing avatar Jul 21 '22 18:07 henxing

Hey @henxing,

thanks for your contribution. What is the problem with dictor here?

mostafa avatar Sep 02 '22 13:09 mostafa

I was just saying that my initial suggested code snipped didn't work because dictor works on dictionaries, but settings is a module with attributes, hence the use of hasattr in my second suggestion and pull request.

henxing avatar Sep 02 '22 14:09 henxing

@henxing Fair enough! I don't see the CLA here. Let me fix that and I'll get back to you.

Which means you need to sign the CLA before I can merge your PR.

mostafa avatar Sep 02 '22 14:09 mostafa

No problem, I thought I filled it out, but I'll go do it now.

henxing avatar Sep 02 '22 14:09 henxing

Yeah, if I go to https://cla-assistant.io/grafana/django-saml2-auth?pullRequest=73 and log in with GitHub, it tells me I've already signed it. If this isn't the correct way to do that, please let me know. And thanks!

henxing avatar Sep 02 '22 14:09 henxing

@henxing But this is PR #72. 🤔

mostafa avatar Sep 02 '22 14:09 mostafa

Whoops, sorry, I copied the link from a different pr and just changed the number when I pasted it in my browser, I forgot to change the number when I wrote that comment. I do notice that my pr doesn't have any mention of the CLA like #73 does, so maybe there's something wrong with cla assistant?

henxing avatar Sep 02 '22 15:09 henxing

Ah, there we go, I forced a recheck with cla assistant and that seems to have done the trick.

henxing avatar Sep 02 '22 15:09 henxing