2018.05.19 Django Masashi SHIBATA c-bata c_bata_! "
Pythonista who loves Django Masashi SHIBATA c-bata c_bata_! "
Django django.contrib.auth.forms django.contrib.auth.views KEYWORDS




: Built-in Auth Forms / Built-in Auth View Classes
: Built-in Auth Forms / Built-in Auth View Classes Web
: Built-in Auth Forms / Built-in Auth View Classes view 1.11 Deprecated
Authentication Backend Email/Password Authentication KEYWORDS
Authentication Backend 1 AUTHENTICATION_BACKENDS = [ ‘django.contrib.auth.backends.ModelBackend', ‘accounts.backends.EmailAuthBackend’, # ]
Authentication Backend 1 AUTHENTICATION_BACKENDS = [ ‘django.contrib.auth.backends.ModelBackend', ‘accounts.backends.EmailAuthBackend’, # ] ModelBackend username/password
Authentication Backend 1 AUTHENTICATION_BACKENDS = [ ‘django.contrib.auth.backends.ModelBackend', ‘accounts.backends.EmailAuthBackend’, # ] EmailAuthBackend email/password
Authentication Backend 1 AUTHENTICATION_BACKENDS = [ ‘django.contrib.auth.backends.ModelBackend', ‘accounts.backends.EmailAuthBackend’, # ]
• authenticate(request, **credentials):
 HttpRequest • get_user(user_id):
from django.contrib.auth import get_user_model from django.contrib.auth.backends import ModelBackend UserModel = get_user_model() class EmailAuthBackend(ModelBackend): def authenticate(self, username="", password="", **kwargs): if username is None: username = kwargs.get(UserModel.USERNAME_FIELD) try: user = UserModel.objects.get(email=username) except UserModel.DoesNotExist: return None else: if user.check_password(password) and self.user_can_authenticate(user): return user
from django.contrib.auth import get_user_model from django.contrib.auth.backends import ModelBackend UserModel = get_user_model() class EmailAuthBackend(ModelBackend): def authenticate(self, username="", password="", **kwargs): if username is None: username = kwargs.get(UserModel.USERNAME_FIELD) try: user = UserModel.objects.get(email=username) except UserModel.DoesNotExist: return None else: if user.check_password(password) and self.user_can_authenticate(user): return user Github
from django.contrib.auth import get_user_model from django.contrib.auth.backends import ModelBackend UserModel = get_user_model() class EmailAuthBackend(ModelBackend): def authenticate(self, username="", password="", **kwargs): if username is None: username = kwargs.get(UserModel.USERNAME_FIELD) try: user = UserModel.objects.get(email=username) except UserModel.DoesNotExist: return None else: if user.check_password(password) and self.user_can_authenticate(user): return user username validation username @
https://github.com/c-bata/django-auth-example/pull/2
Django’s User Model AbstractUser / AbstractBaseUser KEYWORDS ASCIIUsernameValidator
class UserProfile(models.Model): user = models.OneToOneField(settings.AUTH_USER_MODEL) some_additional_columns1 = models.SomethingField(...) : 

https://github.com/c-bata/django-auth-example/pull/3
U+0061
 LATIN SMALL LETTER A U+0430
 CYRILLIC SMALL LETTER A
class User(AbstractBaseUser, PermissionsMixin): username_validator = ASCIIUsernameValidator() username = models.CharField(_(‘username'), validators=[username_validator], ... ) :
>>> import unicodedata >>> unicodedata.normalize('NFKC', ' ') ' ' >>> unicodedata.normalize('NFKC', ' ') ' ' >>> unicodedata.normalize('NFKC', '9⁹₉ ') '9999' >>> unicodedata.normalize('NFKC', 'Hℍℌ') 'HHH'
python-social-auth OAuth 2.0 social-auth-core / social-auth-app-django KEYWORDS from scratch without python-social-auth
Python
https://github.com/c-bata/django-auth-example/pull/4
https://github.com/c-bata/django-auth-example/pull/1 https://github.com/c-bata/django-auth-example/pull/4
THANK YOU

Django の認証処理実装パターン / Django Authentication Patterns