Package: django-magicauth
Django MagicauthDjango Magicauth brings password-less authentication to your project.How it worksThe user inputs their email address (and optionnally an OTP).An email is sent to the user with a link that contains a one-time token.Once they click on this link, they are logged in to the service.For a detailed step-by-step with screenshots of the default templates, seeStep-by-step in pictures.Fonctionnement (FR)Authentifiez vos utilisateurs sans mot de passe avec Django Magicauth.Comment ça marcheLes utilisateurs renseignent leur adresse email (et en option un OTP).Ils reçoivent alors un email avec un lien de connexion.Lorsqu'ils cliquent sur le lien, ils sont authentifiés et redirigés sur le service.Pour un détail du fonctionnement en image, voir ledéroulé en images.Installations and testing instructionsQuick startInstall Magicauthpipinstallgit+https://github.com/betagouv/django-magicauth.gitAdd "magicauth" to your INSTALLED_APPS insettings.pyINSTALLED_APPS=[# all your apps"magicauth",]Include the magicauth URLconf in your app'surl.py# After your previous importsfrommagicauthimportviewsasmagicauth_viewsfrommagicauth.urlsimporturlpatternsasmagicauth_urlsurlpatterns=[# here are your URL patterns]urlpatterns.extend(magicauth_urls)Add the following items in your project's settings.py`MAGICAUTH_FROM_EMAIL = 'contact@mysite.com'
MAGICAUTH_LOGGED_IN_REDIRECT_URL_NAME = 'home'Runpython manage.py migrateto create the polls models.Setup yourmailerinsettings.pyIn dev mode, you can use aconsole mailerMake sure you have the following middlewaresMIDDLEWARE = [
    # [...] other middleware you may have
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.sites.middleware.CurrentSiteMiddleware",
]Two-Factor Authentication (2FA) using One Time Passwords (OTP)Two-Factor Authentication means you ask for two different passwords from your user : their normal password and an OTP. (Seehttps://en.wikipedia.org/wiki/Multi-factor_authentication)
They will get the magic link only if the two are valid, else they get an error and the link is not sent.What is a OTP ? It is a short code (6 to 8 digits), usually generated by a dedicated app on a user's device (smartphone, hardware). Seehttps://en.wikipedia.org/wiki/Time-based_One-time_Password_algorithmHow to enable OTPsWe use thedjango-otppackage. You must have it installed and configured in your project.install django_otp :pip install django_otp(or whatever package manager you use)add the required apps in INSTALLED_APPS :'django_otp','django_otp.plugins.otp_totp'run the migrations in your project :python manage.py migrateSetMAGICAUTH_ENABLE_2FA = Truein your settings.pyIf you use the default login template, an OTP input will show up in the login page, below the Email. (To check your install has worked, you can revert to the default template and see if the field appears : remove your MAGICAUTH_LOGIN_VIEW_TEMPLATE var.)If you have a custom login template, you will have to add the field for entering OTPs, in anOTP_form. See the default login template for an example : templates/magicauth/login.html`For testing purposes, you can generate OTPs for a given user from the command line :Add 'django_otp.plugins.otp_static' to INSTALLED_APPS.Then you can useaddstatictokenfrom command line. E.g :python manage.py addstatictoken -t 123456 "thierry@coucou.fr"Contribute to MagicauthTo contribute to Magicauth, you can install the package in the "editable" modepip uninstall django-magicauth  # just in case...
pip install -e git+https://github.com/betagouv/django-magicauth.git#egg=django-magicauthYou can also install a specific branch, for instance for testing a PR. To install branchmy-branch:pip install -e git+https://github.com/betagouv/django-magicauth.git@my-branch#egg=django-magicauthDjango-magicauth is now a dependency of your project, and you can edit the code located here:cd src/django-magicauthrun testsCreate a virtual env for the project or reuse one and source it.Install dependencies and runtoxcd src/django-magicauth
pip install -r requirements.txt
toxRelease processThe follwing dependencies need to be installed:pip setuptools wheel twine:python-mpipinstall--upgradepipsetuptoolswheeltwineCreate a new MD file underdocs/releasesnamed after the new version and fill the file with the changesChange the version insetup.pyCreate a commit namedRelease versionOpen and merge the PR for that releaseCreate a new release athttps://github.com/betagouv/django-magicauth/releases/newPublish the new version on PyPI:pythonsetup.pysdistbdist_wheeltwineuploaddist/*Projects using Magicauthhttps://github.com/betagouv/e-controle/https://github.com/betagouv/Aidants_Connecthttps://github.com/betagouv/pepshttps://github.com/betagouv/urbanvitaliz-djangoJoin us !Step-by-step in picturesNote : the screenshots below use the default templates, which don't look very nice. It is expected that you replace them with your own better-looking ones.Step 1 : the user goes to the login page. (seeLoginViewinmagicauth/views.py)Step 2 : The user inputs their email address in the login page.Step 3 : The user sees a confirmation page (EmailSentView), explaining that an email has been sent.An email is sent to the user with a link that contains a one-time token.The link looks something like this :Step 4 : once they click on the button and follow the link, the user is directed to a wait page (this is optional) (WaitView)Step 5 : the user is logged in to the service (ValidateTokenView) and redirected to the landing page.