Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
s = self.make_serializer(
"predictable-key", serializer_kwargs={"sort_keys": True}
)
# pickle tests pop serializer kwargs, so skip this test for those
if not s.serializer_kwargs:
return
ts1 = s.dumps({"c": 3, "a": 1, "b": 2})
ts2 = s.dumps(dict(a=1, b=2, c=3))
self.assertEqual(ts1, ts2)
class TimedSerializerTestCase(SerializerTestCase):
serializer_class = itsdangerous.TimedSerializer
def setUp(self):
self._time = time.time
time.time = lambda: 0
def tearDown(self):
time.time = self._time
def test_decode_with_timeout(self):
secret_key = "predictable-key"
value = u"hello"
s = self.make_serializer(secret_key)
ts = s.dumps(value)
self.assertNotEqual(ts, itsdangerous.Serializer(secret_key).dumps(value))
def __init__(self, model, secret):
self.model = model
self.serializer = itsdangerous.TimedSerializer(secret)
self.hook = self.model.xom.config.hook.devpiserver_auth_user
if "backup_codes_signature" not in json:
raise InvalidUsage("Must supply signed backup codes.")
code = json["two_factor_code"]
try:
secret = (
TimestampSigner(current_app.config["SECRET_KEY"])
.unsign(json["secret"], max_age=86400)
.decode()
)
except BadSignature:
raise Unauthorized("Two-factor setup attempt has been tampered with.")
except SignatureExpired:
raise Unauthorized("Two-factor setup attempt has expired.")
try:
backup_codes = TimedSerializer(current_app.config["SECRET_KEY"]).loads(
json["backup_codes_signature"], max_age=86400
)
except BadSignature:
raise Unauthorized("Two-factor setup attempt has been tampered with.")
except SignatureExpired:
raise Unauthorized("Two-factor setup attempt has expired.")
old_auth = current_user.two_factor_auth
if old_auth is not None:
db.session.delete(old_auth)
auth = TwoFactorAuth(user_id=current_user.id)
auth.secret_key = secret
auth.validate(code)
auth.enabled = True
db.session.add(auth)
def __init__(self, config_path='/config.yml', root_suffix='/../',
max_age=10):
self.root = os.path.abspath(os.path.dirname(__file__) + root_suffix)
self.config = current_app.config
del current_app.logger.handlers[0]
current_app.logger.addHandler(ch)
self.serializer = TimedSerializer(self.config['rpc_signature'])
self.max_age = max_age
def check_signature():
g.signer = TimedSerializer(current_app.config['rpc_signature'])
try:
g.signed = g.signer.loads(request.data)
except BadData:
abort(403)
def _get_token_serializer():
return TimedSerializer(current_app.config['SECRET_KEY'])
def reset_backup_codes():
"""
Generate a new list of two-factor auth backup codes for the currently logged in user.
"""
backup_codes = generate_backup_codes()
serialised_codes = TimedSerializer(current_app.config["SECRET_KEY"]).dumps(
backup_codes
)
return (
jsonify(
{"backup_codes": backup_codes, "backup_codes_signature": serialised_codes}
),
200,
)
def confirm_reset_backup_codes():
"""
Generate a new list of two-factor auth backup codes for the currently logged in user and
replace any existing backup codes.
"""
json = request.get_json()
if "backup_codes_signature" not in json:
raise InvalidUsage("Must supply signed backup codes.")
try:
backup_codes = TimedSerializer(current_app.config["SECRET_KEY"]).loads(
json["backup_codes_signature"], max_age=86400
)
except BadSignature:
raise Unauthorized("Backup codes been tampered with.")
except SignatureExpired:
raise Unauthorized("Backup codes reset has expired.")
auth = TwoFactorAuth.query.filter(
TwoFactorAuth.user_id == current_user.id
).first_or_404()
for code in auth.two_factor_backups:
db.session.delete(code)
for code in backup_codes:
backup = TwoFactorBackup(auth_id=auth.user_id)
backup.backup_code = code
db.session.add(backup)