本文整理匯總了Python中pyotp.random_base32方法的典型用法代碼示例。如果您正苦於以下問題:Python pyotp.random_base32方法的具體用法?Python pyotp.random_base32怎麽用?Python pyotp.random_base32使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pyotp
的用法示例。
在下文中一共展示了pyotp.random_base32方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: mfa_get_secret
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def mfa_get_secret(username):
#secret=pyotp.random_base32()
conn = sqlite3.connect('db_users.sqlite')
conn.set_trace_callback(print)
conn.row_factory = sqlite3.Row
c = conn.cursor()
#c.execute("UPDATE users SET mfa = ? WHERE username = ?", (secret, username,))
user = c.execute("SELECT * FROM users WHERE username = ?", (username, )).fetchone()
if user:
return user['mfa_secret'] #True
else:
return False
示例2: mfa_reset_secret
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def mfa_reset_secret(username):
secret=pyotp.random_base32()
conn = sqlite3.connect('db_users.sqlite')
conn.set_trace_callback(print)
conn.row_factory = sqlite3.Row
c = conn.cursor()
#c.execute("UPDATE users SET mfa = ? WHERE username = ?", (secret, username,))
#user = c.execute("SELECT * FROM users WHERE username = ?", (username, )).fetchone()
c.execute("UPDATE users SET mfa_secret = ? WHERE username = ?", (secret, username))
conn.commit()
#if user:
# return user['mfa_secret'] #True
#else:
return False
示例3: get_qrcode
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def get_qrcode(user):
if not user.qrcode:
user.qrcode = pyotp.random_base32()
user.save()
file_name = str(aes.encrypt(user.qrcode), encoding='utf-8')
file = settings.QCODE_ROOT+'/'+file_name+'.png'
if not os.path.exists(file):
data = pyotp.totp.TOTP(user.qrcode).provisioning_uri(user.username, issuer_name="devEops")
qr = QRCode(
version=1,
error_correction=constants.ERROR_CORRECT_L,
box_size=6,
border=4,)
try:
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image()
img.save(file)
return '/media/qrcode/' + file_name + '.png'
except Exception as e:
return '/media/qrcode/' + file_name + '.png'
else:
return '/media/qrcode/' + file_name + '.png'
示例4: refresh_secret
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def refresh_secret(self, secret_path: Optional[str] = None) -> Response:
"""
Refresh the secret token for key generation given a secret path.
:param secret_path: Secret path to refresh (default: default configured path).
"""
secret_path = secret_path or self.secret_path
assert secret_path, 'No secret_path configured'
os.makedirs(os.path.dirname(os.path.abspath(os.path.expanduser(secret_path))), exist_ok=True)
secret = pyotp.random_base32()
with open(secret_path, 'w') as f:
f.writelines([secret])
os.chmod(secret_path, 0o600)
return secret
示例5: test_two_factor_auth_user
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def test_two_factor_auth_user(app):
with app.app_context():
user = User(username="TEST_FACTOR_USER", password="TEST_USER_PASSWORD")
ug = Group(name="TEST_FACTOR_USER", user_group=True, members=[user])
secret = pyotp.random_base32()
auth = TwoFactorAuth(user=user, enabled=True)
auth.secret_key = secret
otp_generator = partial(get_two_factor_code, secret)
db.session.add(user)
db.session.add(auth)
db.session.add(ug)
db.session.commit()
backup_codes = generate_backup_codes()
for code in backup_codes:
backup = TwoFactorBackup(auth_id=auth.user_id)
backup.backup_code = code
db.session.add(backup)
db.session.commit()
return TestTwoFactorUser(
user.id, user.username, "TEST_USER_PASSWORD", otp_generator, backup_codes
)
示例6: mfa_setup
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def mfa_setup():
if current_user.enable_otp:
flash("you have already enabled MFA", "warning")
return redirect(url_for("dashboard.index"))
otp_token_form = OtpTokenForm()
if not current_user.otp_secret:
LOG.d("Generate otp_secret for user %s", current_user)
current_user.otp_secret = pyotp.random_base32()
db.session.commit()
totp = pyotp.TOTP(current_user.otp_secret)
if otp_token_form.validate_on_submit():
token = otp_token_form.token.data.replace(" ", "")
if totp.verify(token) and current_user.last_otp != token:
current_user.enable_otp = True
current_user.last_otp = token
db.session.commit()
flash("MFA has been activated", "success")
return redirect(url_for("dashboard.recovery_code_route"))
else:
flash("Incorrect token", "warning")
otp_uri = pyotp.totp.TOTP(current_user.otp_secret).provisioning_uri(
name=current_user.email, issuer_name="SimpleLogin"
)
return render_template(
"dashboard/mfa_setup.html", otp_token_form=otp_token_form, otp_uri=otp_uri
)
示例7: set_TFA_secret
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def set_TFA_secret(self):
secret = pyotp.random_base32()
self._TFA_secret = encrypt_string(secret)
示例8: edit_profile
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def edit_profile():
"""Edit profile function allows the user to modify their about me section."""
form = EditProfileForm(current_user.username)
if form.validate_on_submit():
current_user.username = form.username.data
current_user.about_me = form.about_me.data
user_name = User.query.filter_by(username=current_user.username).first()
if user_name is None:
render_error_page_template(404)
if form.otp_token_checkbox.data:
if user_name.otp_secret:
current_user.otp_secret = user_name.otp_secret
else:
current_user.otp_secret = pyotp.random_base32()
db.session.commit()
url = pyqrcode.create(user_name.get_totp_uri())
stream = BytesIO()
url.svg(stream, scale=3)
return render_template('two-factor-setup.html'), 200, {
'Cache-Control': 'no-cache, no-store, must-revalidate',
'Pragma': 'no-cache',
'Expires': '0'}
elif request.method == 'GET':
form.username.data = current_user.username
form.about_me.data = current_user.about_me
if form.otp_token_checkbox:
if form.otp_token_checkbox.data:
form.otp_token.data = current_user.otp_token
else:
form.otp_token_checkbox = current_user.otp_token_checkbox
else:
for error in form.errors:
flash(str(form.errors[error][0]), 'error')
return render_template('edit_profile.html', title=_('Edit Profile'), form=form)
示例9: set_otp_secret
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def set_otp_secret(self):
"""Set two factor token for user."""
if self.otp_secret is None:
# generate a random secret
self.otp_secret = pyotp.random_base32()
示例10: mfa_enable
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def mfa_enable(username):
#secret=pyotp.random_base32()
conn = sqlite3.connect('db_users.sqlite')
conn.set_trace_callback(print)
conn.row_factory = sqlite3.Row
c = conn.cursor()
#c.execute("UPDATE users SET mfa = ? WHERE username = ?", (secret, username,))
c.execute("UPDATE users SET mfa_enabled = 1 WHERE username = ?", (username,))
conn.commit()
return True
示例11: get
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def get(self, request, *args, **kwargs):
profile = self.profile
if not profile.totp_key:
profile.totp_key = pyotp.random_base32(length=32)
profile.save(update_fields=['totp_key'])
if not profile.scratch_codes:
profile.generate_scratch_codes()
return self.render_to_response(self.get_context_data())
示例12: generate_scratch_codes
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def generate_scratch_codes(self):
codes = [pyotp.random_base32(length=16) for i in range(settings.DMOJ_SCRATCH_CODES_COUNT)]
self.scratch_codes = json.dumps(codes)
self.save(update_fields=['scratch_codes'])
return codes
示例13: generate_otp_hash
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def generate_otp_hash(self):
try:
import pyotp
return pyotp.random_base32()
except ImportError:
print("You must install pyotp to use `generate_otp_hash`.")
print("pip install pyotp")
return None
示例14: __init__
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user")
super().__init__(*args, **kwargs)
self.initial_secret = pyotp.random_base32()
self.fields["secret"].initial = self.initial_secret
示例15: post_create
# 需要導入模塊: import pyotp [as 別名]
# 或者: from pyotp import random_base32 [as 別名]
def post_create(cls, sender, instance, created, *args, **kwargs): # pylint: disable=unused-argument
if created:
instance.secret = pyotp.random_base32()
instance.save()