🛡️2FA with Python.

What is 2FA and why 2FA?🤔

2FA stands for two factor authentication. It adds an extra layer of security other than password. The user must enter a 2FA code along with password in order to sign in. 2FA codes can be generated in two ways, time …


This content originally appeared on DEV Community and was authored by B.Jothin kumar

What is 2FA and why 2FA?🤔

2FA stands for two factor authentication. It adds an extra layer of security other than password. The user must enter a 2FA code along with password in order to sign in. 2FA codes can be generated in two ways, time based codes and counter based codes.

Advantages of 2FA over E-Mail or SMS verification:

  • No network required: 2FA codes can be generated offline.
  • 🛡️ Better security.

Time based codes vs counter based code.

Time based codes Counter based codes
Time based codes changes depending on time. 🕖 Counter based codes change depending on number of successful sign-in(s). ✔️
No need of adding counter every time in client side. After every successful login, counter must be increased by one in server side as well as client side.

2FA flowchart

2FA with Python

Requirements

  • onetimepass python package (Can be installed using the command: pip install onetimepass).
  • Your favourite authenticator app (Example: Google authenticator, Microsoft authenticator).

Let's start!đź‘€

For both time based codes and counter based code, a secret string is securely shared with the authenticator app while setting up 2FA. All codes are generated based on this secret string. This string is not case sensitive.

đź•–Time based codes

Let us now, write a simple Python script to understand how time based 2FA works!

from onetimepass import valid_totp
from random import choice


def generate_secret():  # Function to return a random string with length 16.
    secret = ''
    while len(secret) < 16:
        secret += choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567')
    return secret


secret = generate_secret()
print('Enter the following secret in your authenticator app: ', secret)
print("""
Instructions for saving this secret it Google Authenticator:
1. Open Google Authenticator.
2. Click plus icon at the right bottom.
3. Click Enter a setup key.
4. Enter an Account name of your choice and enter the secret provided above.
5. Click Add.
""")
while True:
    otp = int(input('Please enter the otp generated by your authenticator app: '))
    authenticated = valid_totp(otp, secret)
    if authenticated:
        print('Correct otp, Authenticated!')
    elif not authenticated:
        print('Wrong otp, please try again.')

✔️ Counter based codes

Here is a complete Python script to understand how counter based 2FA works!

from onetimepass import valid_hotp
from random import choice


def generate_secret():  # Function to return a random string with length 16.
    secret = ''
    while len(secret) < 16:
        secret += choice('ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567')
    return secret


secret = generate_secret()
print('Enter the following secret in your authenticator app: ', secret)
print("""
Instructions for saving this secret it Google Authenticator:
1. Open Google Authenticator.
2. Click plus icon at the right bottom.
3. Click Enter a setup key.
4. Enter an Account name of your choice and enter the secret provided above.
5. Click Add.
""")
while True:
    counter = 0
    otp = int(input('Please enter the otp generated by your authenticator app: '))
    authenticated = valid_hotp(otp, secret)
    if authenticated:
        print('Correct otp, Authenticated!')
        counter += 1
    elif not authenticated:
        print('Wrong otp, please try again.')

Thank you! Leave a comment and a like if you find this article useful :-)

About me

Jothin kumar


This content originally appeared on DEV Community and was authored by B.Jothin kumar


Print Share Comment Cite Upload Translate Updates
APA

B.Jothin kumar | Sciencx (2021-12-22T06:07:31+00:00) 🛡️2FA with Python.. Retrieved from https://www.scien.cx/2021/12/22/%f0%9f%9b%a1%ef%b8%8f2fa-with-python/

MLA
" » 🛡️2FA with Python.." B.Jothin kumar | Sciencx - Wednesday December 22, 2021, https://www.scien.cx/2021/12/22/%f0%9f%9b%a1%ef%b8%8f2fa-with-python/
HARVARD
B.Jothin kumar | Sciencx Wednesday December 22, 2021 » 🛡️2FA with Python.., viewed ,<https://www.scien.cx/2021/12/22/%f0%9f%9b%a1%ef%b8%8f2fa-with-python/>
VANCOUVER
B.Jothin kumar | Sciencx - » 🛡️2FA with Python.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/22/%f0%9f%9b%a1%ef%b8%8f2fa-with-python/
CHICAGO
" » 🛡️2FA with Python.." B.Jothin kumar | Sciencx - Accessed . https://www.scien.cx/2021/12/22/%f0%9f%9b%a1%ef%b8%8f2fa-with-python/
IEEE
" » 🛡️2FA with Python.." B.Jothin kumar | Sciencx [Online]. Available: https://www.scien.cx/2021/12/22/%f0%9f%9b%a1%ef%b8%8f2fa-with-python/. [Accessed: ]
rf:citation
» 🛡️2FA with Python. | B.Jothin kumar | Sciencx | https://www.scien.cx/2021/12/22/%f0%9f%9b%a1%ef%b8%8f2fa-with-python/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.