Skip to content

Commit 1a691d8

Browse files
committed
Do some refactoring
1 parent ca92a51 commit 1a691d8

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

seleniumbase/fixtures/base_case.py

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8510,33 +8510,9 @@ def get_chromium_driver_version(self):
85108510

85118511
def get_mfa_code(self, totp_key=None):
85128512
"""Same as get_totp_code() and get_google_auth_password().
8513-
Returns a time-based one-time password based on the
8514-
Google Authenticator algorithm for multi-factor authentication.
8515-
If the "totp_key" is not specified, this method defaults
8516-
to using the one provided in [seleniumbase/config/settings.py].
8517-
Google Authenticator codes expire & change at 30-sec intervals.
8518-
If the fetched password expires in the next 1.2 seconds, waits
8519-
for a new one before returning it (may take up to 1.2 seconds).
8520-
See https://pyotp.readthedocs.io/en/latest/ for details."""
8521-
import pyotp
8522-
8523-
if not totp_key:
8524-
totp_key = settings.TOTP_KEY
8525-
8526-
epoch_interval = time.time() / 30.0
8527-
cycle_lifespan = float(epoch_interval) - int(epoch_interval)
8528-
if float(cycle_lifespan) > 0.96:
8529-
# Password expires in the next 1.2 seconds. Wait for a new one.
8530-
for i in range(30):
8531-
time.sleep(0.04)
8532-
epoch_interval = time.time() / 30.0
8533-
cycle_lifespan = float(epoch_interval) - int(epoch_interval)
8534-
if not float(cycle_lifespan) > 0.96:
8535-
# The new password cycle has begun
8536-
break
8537-
8538-
totp = pyotp.TOTP(totp_key)
8539-
return str(totp.now())
8513+
Returns a time-based one-time password based on the Google
8514+
Authenticator algorithm for multi-factor authentication."""
8515+
return shared_utils.get_mfa_code(totp_key)
85408516

85418517
def enter_mfa_code(
85428518
self, selector, totp_key=None, by="css selector", timeout=None

seleniumbase/fixtures/shared_utils.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import time
88
from contextlib import suppress
99
from seleniumbase import config as sb_config
10+
from seleniumbase.config import settings
1011
from seleniumbase.fixtures import constants
1112

1213

@@ -29,6 +30,34 @@ def pip_install(package, version=None):
2930
)
3031

3132

33+
def get_mfa_code(totp_key=None):
34+
"""Returns a time-based one-time password based on the
35+
Google Authenticator algorithm for multi-factor authentication.
36+
If the "totp_key" is not specified, this method defaults
37+
to using the one provided in [seleniumbase/config/settings.py].
38+
Google Authenticator codes expire & change at 30-sec intervals.
39+
If the fetched password expires in the next 1.2 seconds, waits
40+
for a new one before returning it (may take up to 1.2 seconds).
41+
See https://pyotp.readthedocs.io/en/latest/ for details."""
42+
import pyotp
43+
44+
if not totp_key:
45+
totp_key = settings.TOTP_KEY
46+
epoch_interval = time.time() / 30.0
47+
cycle_lifespan = float(epoch_interval) - int(epoch_interval)
48+
if float(cycle_lifespan) > 0.96:
49+
# Password expires in the next 1.2 seconds. Wait for a new one.
50+
for i in range(30):
51+
time.sleep(0.04)
52+
epoch_interval = time.time() / 30.0
53+
cycle_lifespan = float(epoch_interval) - int(epoch_interval)
54+
if not float(cycle_lifespan) > 0.96:
55+
# The new password cycle has begun
56+
break
57+
totp = pyotp.TOTP(totp_key)
58+
return str(totp.now())
59+
60+
3261
def is_arm_linux():
3362
"""Returns True if machine is ARM Linux.
3463
This will be useful once Google adds

0 commit comments

Comments
 (0)