import pyotp import urllib.parse Parse the URI uri =…
- User
import pyotp import urllib.parse
Parse the URI
uri = 'otpauth://totp/Test:nexign?secret=XXXXXXXXXXXXXXXXX&issuer=Test' parsed_uri = urllib.parse.urlparse(uri) query_params = urllib.parse.parse_qs(parsed_uri.query)
Extract the secret and issuer from the query parameters
secret = query_params['secret'][0] issuer = query_params['issuer'][0]
Create an OTP object from the secret
otp = pyotp.TOTP(secret)
Get the OTP code
otp_code = otp.now()
Verify the OTP code
is_valid = otp.verify(otp_code)
Print the details
print(f"Issuer: {issuer}") print(f"OTP code: {otp_code}") print(f"Is valid: {is_valid}")
измени так чтобы Test и XXXXXXXXXXXXXXXXX брался из переменного окружения
- Assistant
import pyotp import urllib.parse import os
Parse the URI
uri = 'otpauth://totp/Test:nexign?secret=XXXXXXXXXXXXXXXXX&issuer=Test' parsed_uri = urllib.parse.urlparse(uri) query_params = urllib.parse.parse_qs(parsed_uri.query)
Extract the secret and issuer from the query parameters
secret = query_params['secret'][0] issuer = query_params['issuer'][0]
Use environment variables for Test and XXXXXXXXXXXXXXXXX
test_variable = os.environ.get('TEST_VARIABLE') secret_variable = os.environ.get('SECRET_VARIABLE')
Create an OTP object from the secret
otp = pyotp.TOTP(secret_variable)
Get the OTP code
otp_code = otp.now()
Verify the OTP code
is_valid = otp.verify(otp_code)
Print the details
print(f"Issuer: {issuer}") print(f"Test variable: {test_variable}") print(f"OTP code: {otp_code}") print(f"Is valid: {is_valid}")