One-Time Passwords (OTP) - Example
In this section, we shall provide an example of how to generate and validate One-Time Passwords (OTP) in Python. The Python library of our choice is PyOTP, which implement the RFC 4226 and RFC 6238 standards. If you want to use this library you should follow the requirements in those standards. Installation:
Server-Side Setup
We need to create a base32 secret which has to be shared between the authentication server and the client. We will use Google Authenticator OpenSource OTP model which produce a URI for an exchange, the secret and additional client-server details. It includes the shared secret, the client's username, and the issuer's name.
Run the above code example: https://repl.it/@nakov/OTP-Server-Side-in-Python.
Sample output:
Client-Side Setup
Once the client stores the secret in a secure way, in a time-interval of a 30 seconds (by default) a new code will be generated.
Run the above code example: https://repl.it/@nakov/OTP-Client-Side-in-Python.
Sample output:
Working Example
You can install Google Authenticator from Google Play or App Store and scan the QR code below:
Example validation check:
Run the above code example: https://repl.it/@nakov/QR-code-scanner-in-Python.
Output:
Last updated