Google Authenticator Authenticator Authenticator Authenticator in Python
Python's google authenticator authentication
Environment description
- python 3.7
- Required installation package:
- pyotp qrcode Image
Implementation principle
- Use python module of pyotp to generate the key required by Google auth
- Generate barcode pictures based on key
- Use the Google Authenticator client to scan the barcode, and the client generates a 6-digit verification code based on the time and key passing algorithm.
- The platform secondary authentication passes verification of the input verification code, and the verification is also based on time and key.
Code implementation
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Author : SLP # @Time : 2021/10/3 10:25 import os import traceback import pyotp from qrcode import QRCode, constants class GoogleAuthenticatorClient: def __init__(self, secret_key=None): self.secret_key = secret_key def create_secret(self): """ Generate the key required for Google auth :return: """ self.secret_key = pyotp.random_base32(64) return self.secret_key def create_secret_qrcode(self, name=None, issuer_name=None, save_to_file=True): """ Generate QR code pictures based on username and key :param name:user name :param issuer_name: issuer :param save_to_file: Save to file :return: """ data = (self.secret_key).provisioning_uri(name=name, issuer_name=issuer_name) qr = QRCode( version=1, error_correction=constants.ERROR_CORRECT_L, box_size=6, border=4, ) try: qr.add_data(data) (fit=True) img = qr.make_image() if save_to_file: base_dir = ((__file__)) dir_path = (base_dir, 'static', 'image') if not (dir_path): (dir_path) filepath = dir_path + + self.secret_key + '.png' (filepath) # Save barcode pictures return True, filepath else: return img.get_image() except Exception as e: traceback.print_exc() return False, None def verify_code_func(self, verify_code): t = (self.secret_key) result = (verify_code) return result if __name__ == '__main__': secret_key = 'PU6PY6FWPVQ4BXE7ZP6X7YMVM3BH3ODS7SW53GL3LJPED7AAQUVF2EKP6AGNFFOX' google_auth_ = GoogleAuthenticatorClient(secret_key=secret_key) # secret = google_auth_.create_secret() # print('Key', secret) # # Generate image QR code image = google_auth_.create_secret_qrcode(name='slp', issuer_name='GoldBull', save_to_file=False) print(()) # verify # res = google_auth_.verify_code_func(verify_code='635543') # print(res)
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.
Related Articles
Summary of various dimension transformation functions of Pytorch
This article summarizes the functions of various dimension transformations in PyTorch, including reshape(), view(), resize_(), transpose(), permute(), squeeze(), unsqeeze(), expand(), and repeat() functions. Those who are interested can learn about it.2024-02-02The difference between threads and processes and Python code examples
This article mainly introduces the difference between threads and processes and Python code examples. This article gives a python script to run two threads in a process. Friends who need it can refer to it.2015-02-02python's unittest test class code example
This article mainly introduces python's unittest test code examples, which have certain reference value. Friends who need it can learn about it.2017-12-12Steps to recognize PCB board pictures on Opencv+Python
This article mainly introduces the steps of Opencv+Python to recognize PCB board pictures, helping everyone better understand and use python for machine learning. Interested friends can learn more2021-01-01Python implements word guessing game
This article mainly introduces the python word guessing game. The sample code in the article is introduced in detail and has a certain reference value. Interested friends can refer to it.2018-06-06Detailed explanation of Python crawler's parsing HTML page
This article introduces one of the important tools used in Python to parse HTML pages - the BeautifulSoup library. It explains in detail the basic usage methods, label selectors, CSS selectors, regular expressions, traversal document trees and other contents of the BeautifulSoup library. It also displays the application scenarios of the BeautifulSoup library in combination with the example code.2023-04-04python matplotlib comment text arrow simple code example
This article mainly introduces the simple code example of python matplotlib annotation text arrow, which has certain reference value.2018-01-01python collection common operations summary
This article mainly introduces relevant information about the commonly used operations of Python collection. Friends who need it can refer to it.2022-12-12Find the Python installation directory, set the environment path, and run the Python script instance on the command line
This article mainly introduces finding the Python installation directory, setting the environment path, and running python script instances on the command line. It has good reference value and hopes it will be helpful to everyone. Let's take a look with the editor2020-03-03() Calculate matrix eigenvector method
Today, the editor will share with you an article () calculation matrix feature vector method, which has good reference value and hope it will be helpful to everyone. Let's take a look with the editor2019-11-11