0

I'm trying to implement django two factor auth into my Rest API. Is there a possibility to implement this by using custom views and model creations? Because, at least that's the way I understood it, this library is mainly working based on default django templates and predefined routes. Is it possible to combine this library with a Rest API or should I use another library?

fullstacknoob
  • 161
  • 1
  • 7

2 Answers2

2

There is a package for Django REST 2FA: https://github.com/merixstudio/django-trench

Kristof Rado
  • 713
  • 1
  • 8
  • 19
  • 1
    I took a look at it and it seems that trench also provides endpoints for login returning e.g. JWT tokens. Do you know if it's possible to implement django-trench into an already existing JWT authentication system? There's also the problem that the login endpoint only takes a username & password while my already existing system supports username and email logins. – fullstacknoob Nov 10 '22 at 02:08
2

You cam use pyotp library. It's compatible with most of the two factor authenticator app like google authenticator. Very easy to use.

Here is an example how to use use:

base32 = pyotp.random_base32() 
>>> base32
'ERAAADLXLDFBVL2JSR4RLR73DWFWYSTU'
>>> totp = pyotp.TOTP(base32)
>>> totp.now() 
'206328'

It will generate random code after every 30 seconds. For generate provisioning URIs for use with the QR Code scanner:

pyotp.totp.TOTP('ERAAADLXLDFBVL2JSR4RLR73DWFWYSTU').provisioning_uri(name='alice@google.com', issuer_name='Secure App')
>>> 'otpauth://totp/Secure%20App:alice%40google.com?secret=ERAAADLXLDFBVL2JSR4RLR73DWFWYSTU&issuer=Secure%20App'
boyenec
  • 1,405
  • 5
  • 29