<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Kumar Sahil</title>
    <description>The latest articles on Forem by Kumar Sahil (@krsahil8825).</description>
    <link>https://forem.com/krsahil8825</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3807782%2F43958f18-65b1-4231-8543-6415b05c5b70.jpg</url>
      <title>Forem: Kumar Sahil</title>
      <link>https://forem.com/krsahil8825</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/krsahil8825"/>
    <language>en</language>
    <item>
      <title>Adding TOTP-Based 2FA to Django REST Framework with django-totp</title>
      <dc:creator>Kumar Sahil</dc:creator>
      <pubDate>Wed, 06 May 2026 11:25:19 +0000</pubDate>
      <link>https://forem.com/krsahil8825/adding-totp-based-2fa-to-django-rest-framework-with-django-totp-4ga7</link>
      <guid>https://forem.com/krsahil8825/adding-totp-based-2fa-to-django-rest-framework-with-django-totp-4ga7</guid>
      <description>&lt;p&gt;Two-factor authentication (2FA) is becoming a standard requirement for modern applications, especially for APIs that use JWT authentication or separate frontend/backend architectures.&lt;/p&gt;

&lt;p&gt;While working on Django REST Framework projects, I wanted a lightweight and API-focused way to add TOTP authentication without depending heavily on template-based flows or admin integrations.&lt;/p&gt;

&lt;p&gt;So I built &lt;code&gt;django-totp&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It is a reusable Django package that provides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;TOTP enrollment&lt;/li&gt;
&lt;li&gt;QR generation&lt;/li&gt;
&lt;li&gt;backup recovery codes&lt;/li&gt;
&lt;li&gt;encrypted secret storage&lt;/li&gt;
&lt;li&gt;DRF endpoints&lt;/li&gt;
&lt;li&gt;helper utilities for multi-step authentication flows&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;PyPI: django-totp&lt;/p&gt;

&lt;h2&gt;
  
  
  Requirements
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Python 3.12+&lt;/li&gt;
&lt;li&gt;Django 5.0+&lt;/li&gt;
&lt;li&gt;Django REST Framework 3.15+&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;Install the package from PyPI:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;pip &lt;span class="nb"&gt;install &lt;/span&gt;django-totp
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Add the apps to your Django settings:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;INSTALLED_APPS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="c1"&gt;# Django apps...
&lt;/span&gt;    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;rest_framework&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;django_totp&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Configure the Encryption Key
&lt;/h2&gt;

&lt;p&gt;TOTP secrets and backup codes are stored using Fernet encryption.&lt;/p&gt;

&lt;h3&gt;
  
  
  Generate a key once
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s2"&gt;"from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Add it to your environment
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TOTP_ENCRYPTION_KEY=your-generated-key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Load it in Django settings
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;

&lt;span class="n"&gt;TOTP_ENCRYPTION_KEY&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;environ&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;TOTP_ENCRYPTION_KEY&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Include the URLs
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django.urls&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;include&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;path&lt;/span&gt;

&lt;span class="n"&gt;urlpatterns&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nf"&gt;path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;api/&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;include&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="s"&gt;django_totp.urls&lt;/span&gt;&lt;span class="sh"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)),&lt;/span&gt;
&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Run migrations:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;python manage.py migrate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Available Endpoints
&lt;/h2&gt;

&lt;p&gt;The package provides endpoints for the full enrollment lifecycle.&lt;/p&gt;

&lt;h3&gt;
  
  
  Create Enrollment
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;POST /api/totp/create/
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Creates a TOTP secret and returns an SVG QR code.&lt;/p&gt;

&lt;p&gt;Example response:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"svg"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"&amp;lt;svg ...&amp;gt;...&amp;lt;/svg&amp;gt;"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Confirm Enrollment
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;POST /api/totp/confirm/
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Request:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"input_code"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"123456"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Successful confirmation returns backup recovery codes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Disable TOTP
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;POST /api/totp/disable/
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Disables TOTP and removes backup codes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Rotate Backup Codes
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight http"&gt;&lt;code&gt;&lt;span class="err"&gt;POST /api/totp/rotate_backup_codes/
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Generates a new backup code set.&lt;/p&gt;

&lt;h2&gt;
  
  
  Example Login Flow
&lt;/h2&gt;

&lt;p&gt;A common authentication flow looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Validate username/password
2. Check whether the user has TOTP enabled
3. Issue a temporary challenge token
4. Ask for TOTP or backup code
5. Verify the code
6. Issue final JWT/session
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The package includes helper utilities for this flow.&lt;/p&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django_totp.auth&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;generate_challenge_token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;is_totp_enabled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="n"&gt;django_totp.totp&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;verify_totp_code&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Other Utilities
&lt;/h2&gt;

&lt;p&gt;Useful helpers you can import directly:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;django_totp.auth

&lt;ul&gt;
&lt;li&gt;is_totp_enabled(user)&lt;/li&gt;
&lt;li&gt;generate_challenge_token(user)&lt;/li&gt;
&lt;li&gt;verify_challenge_token(token)&lt;/li&gt;
&lt;li&gt;get_user_from_challenge_token(token)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;django_totp.totp

&lt;ul&gt;
&lt;li&gt;generate_totp_secret()&lt;/li&gt;
&lt;li&gt;verify_totp_code(user, input_code)&lt;/li&gt;
&lt;li&gt;create_totp_setup(user)&lt;/li&gt;
&lt;li&gt;confirm_totp_setup(user, input_code)&lt;/li&gt;
&lt;li&gt;disable_totp(user)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;django_totp.backup_code_utils

&lt;ul&gt;
&lt;li&gt;store_backup_codes(user, codes)&lt;/li&gt;
&lt;li&gt;verify_backup_code(user, input_code)&lt;/li&gt;
&lt;li&gt;rotate_backup_codes(user)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;django_totp.encryption

&lt;ul&gt;
&lt;li&gt;generate_fernet_key()&lt;/li&gt;
&lt;li&gt;resolve_fernet_key(default=None)&lt;/li&gt;
&lt;li&gt;encrypt(value)&lt;/li&gt;
&lt;li&gt;decrypt(value)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h2&gt;
  
  
  Features
&lt;/h2&gt;

&lt;p&gt;The package currently includes:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Encrypted TOTP secret storage&lt;/li&gt;
&lt;li&gt;QR generation for authenticator apps&lt;/li&gt;
&lt;li&gt;Backup code generation and rotation&lt;/li&gt;
&lt;li&gt;One-time-use backup code validation&lt;/li&gt;
&lt;li&gt;DRF integration&lt;/li&gt;
&lt;li&gt;Configurable issuer name&lt;/li&gt;
&lt;li&gt;Endpoint throttling support&lt;/li&gt;
&lt;li&gt;Signed temporary challenge tokens&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Why I Built It
&lt;/h2&gt;

&lt;p&gt;Many existing Django 2FA solutions are designed primarily for server-rendered applications.&lt;/p&gt;

&lt;p&gt;I wanted something focused more on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;DRF APIs&lt;/li&gt;
&lt;li&gt;JWT authentication flows&lt;/li&gt;
&lt;li&gt;SPA/mobile frontends&lt;/li&gt;
&lt;li&gt;reusable API endpoints&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The goal was to keep the package relatively lightweight while still covering common 2FA requirements.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Links
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;PyPI: &lt;a href="https://pypi.org/project/django-totp/" rel="noopener noreferrer"&gt;https://pypi.org/project/django-totp/&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;GitHub: &lt;a href="https://github.com/krsahil8825/django-totp" rel="noopener noreferrer"&gt;https://github.com/krsahil8825/django-totp&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feedback, issues, and contributions are welcome.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>django</category>
      <category>python</category>
      <category>opensource</category>
    </item>
  </channel>
</rss>
