Django: one authentication backend per user model

In my Django application I need multiple type of Django users:
1. A typical admin user which is identified by email and authenticated with password
2. A remote application which connects to django and authenticates with pre-shared key and tokens. The email is not required for such type of user

Every type of user has different associated attributes. I know that I can extend User model in Django. I already created email authentication backend. But this works for all the accounts, including the remote applications ones.

Is there any effective way to create a auth backend for a type of user?


You can create a custom authentication backend for the remote application and show it in your settings like this:

AUTHENTICATION_BACKENDS = (
    'path.to.email.authentication',
    'path.to.remote.app.authentication',
)

This way, Django will try to use the email authentication first and if that fails will try the remote app authentication.

You can read about custom authentication backends here

Hope it helps!


If you want to disable for some of you users password authentication you can use set_unusable_password . From the docs:

Marks the user as having no password set. This isn't the same as having a blank string for a password. check_password() for this user will never return True. Doesn't save the User object.

You may need this if authentication for your application takes place against an existing external source such as an LDAP directory.

链接地址: http://www.djcxy.com/p/5782.html

上一篇: 什么是回调函数?

下一篇: Django:每个用户模型一个身份验证后端