Package: django-admin-auth-emailpin
DjangoAuthEmailPin libraryIn RussianThe free, open-source DjangoAuthEmailPin library is designed for password-free user authorization in the Django admin panel.
One-time codes are sent to users by email to enter the admin area.Installationpipinstalldjango-admin-auth-emailpinUsageTo send authorization codes, the regular way ofsending email in Djangois used.
Therefore, in thesettings.pyfile of your project, you must specify the details of the mail server used.# settings.pyEMAIL_HOST='smtp.gmail.com'EMAIL_PORT=587EMAIL_USE_TLS=TrueThe Django admin user model must have special fields to use the library.name: primary email address for user.custom_email: optional additional user email.is_active: if this field is set to False, then the user is disabled in admin panel.The library provides an abstractUserclass that defines these fields.
This class can be used as a base class when defining the user model of your project.# models.pyfromdjango_admin_auth_emailpin.modelsimportUserclassMyUser(User):# Titles for fields in Django admintitle_fld_name="Login"title_fld_email="Custom email"title_fld_active="Active"For user authorization using the library, your project must contain a model,
inherited from thePinCodeclass from the library.# models.pyfromdjango_admin_auth_emailpin.modelsimportPinCodeclassPin(PinCode):The following methods need to be defined in this model.Method mail_secretsReturns a tuple of three elements specifying the parameters for accessing the mail server.address where authorization codes for theadminuser (database superuser) will be sentusername to connect to the mail serverpassword to connect to the mail server# models.py@classmethoddefmail_secrets(cls):return('admin@example.com','example.admin@gmail.com','smtp-password',)Method mail_inaciveGets an instance of the user model that is denied access to the admin panel (theis_activefield is set to False).Returns a tuple of three elements specifying the parameters of the email that will be sent to this user.the address from which the email will be sentemail subject textemail body text# models.py@classmethoddefmail_inacive(cls,user):return("noreply@example.com","Failed login at example.com.","Your account{}is disabled. Ask for admin to solve issue.".format(user),)Method mail_loginGets the model instance of the user who requested authorization and the model instance of the authorization code generated for that user.Returns a tuple of three elements defining the parameters of the email with authorization data that will be sent to this user.the address from which the email will be sentemail subject textemail body text# models.py@classmethoddefmail_login(cls,user,pin):return("noreply@example.com","Login at example.com","To login as{}use PIN code:{}".format(user,pin.code),)User AuthorizationsTo start authorization of a user namedusername, you need to call thePin.authmethod,
passing it the model class and username.If there is a user with this name in the database and he is allowed to enter the admin panel (fieldis_active=True),
a one-time authorization code will be generated for this user and an email with this code will be sent to the user.ThePin.authmethod will returnTruein this case.If the user with the given name is not in the database or he is denied access to the admin panel (fieldis_active=False),
thePin.authmethod will returnFalse.# views.pyfrom.modelsimportMyUser,PinassertPin.auth(MyUser,'username')To complete the authorization, you need to get the value of the authorization code from the user and call thePin.is_validmethod.
This method should be passed the name of the authorized user and the authorization code.If a valid code value is used, the method will returnTrueand the authorization code used in the call will become invalid.# views.pyfromdjango.contrib.authimportloginfromdjango.contrib.auth.modelsimportUserfrom.modelsimportPinifPin.is_valid(username,code):user=User.objects.filter(username=username)[0]login(request,user)Development$ git clone git@github.com:vb64/django.admin.auth.emailpin
$ cd django.admin.auth.emailpin
$ make setup PYTHON_BIN=/path/to/python3
$ make tests