diff --git a/app/dashboard/views/fido_setup.py b/app/dashboard/views/fido_setup.py index a70bc8ee..14c1b471 100644 --- a/app/dashboard/views/fido_setup.py +++ b/app/dashboard/views/fido_setup.py @@ -62,25 +62,29 @@ def fido_setup(): LOG.error(f"An error occurred in WebAuthn registration process: {e}") flash("Key registration failed.", "warning") return redirect(url_for("dashboard.index")) - + if current_user.fido_uuid is None: current_user.fido_uuid = fido_uuid FIDO.create( - credential_id = str(fido_credential.credential_id, "utf-8"), - uuid = fido_uuid, - public_key = str(fido_credential.public_key, "utf-8"), - sign_count = fido_credential.sign_count, + credential_id=str(fido_credential.credential_id, "utf-8"), + uuid=fido_uuid, + public_key=str(fido_credential.public_key, "utf-8"), + sign_count=fido_credential.sign_count, ) db.session.commit() - LOG.d(f"credential_id={str(fido_credential.credential_id, 'utf-8')} added for {fido_uuid}") + LOG.d( + f"credential_id={str(fido_credential.credential_id, 'utf-8')} added for {fido_uuid}" + ) flash("Security key has been activated", "success") return redirect(url_for("dashboard.recovery_code_route")) # Prepare information for key registration process - fido_uuid = str(uuid.uuid4()) if current_user.fido_uuid is None else current_user.fido_uuid + fido_uuid = ( + str(uuid.uuid4()) if current_user.fido_uuid is None else current_user.fido_uuid + ) challenge = secrets.token_urlsafe(32) credential_create_options = webauthn.WebAuthnMakeCredentialOptions( @@ -102,11 +106,13 @@ def fido_setup(): # Prevent user from adding duplicated keys for record in fido_model: - registration_dict["excludeCredentials"].append({ - 'type': 'public-key', - 'id': record.credential_id, - 'transports': ['usb', 'nfc', 'ble', 'internal'], - }) + registration_dict["excludeCredentials"].append( + { + "type": "public-key", + "id": record.credential_id, + "transports": ["usb", "nfc", "ble", "internal"], + } + ) session["fido_uuid"] = fido_uuid session["fido_challenge"] = challenge.rstrip("=") diff --git a/app/models.py b/app/models.py index 72747c50..4d5f57c3 100644 --- a/app/models.py +++ b/app/models.py @@ -119,13 +119,19 @@ class AliasGeneratorEnum(EnumE): word = 1 # aliases are generated based on random words uuid = 2 # aliases are generated based on uuid + class FIDO(db.Model, ModelMixin): __tablename__ = "fido" credential_id = db.Column(db.String(), nullable=False, unique=True, index=True) - uuid = db.Column(db.ForeignKey("users.fido_uuid", ondelete="cascade"), unique=False, nullable=False) + uuid = db.Column( + db.ForeignKey("users.fido_uuid", ondelete="cascade"), + unique=False, + nullable=False, + ) public_key = db.Column(db.String(), nullable=False, unique=True) sign_count = db.Column(db.Integer(), nullable=False) + class User(db.Model, ModelMixin, UserMixin): __tablename__ = "users" email = db.Column(db.String(256), unique=True, nullable=False) diff --git a/server.py b/server.py index e15fd372..3e20335e 100644 --- a/server.py +++ b/server.py @@ -148,16 +148,16 @@ def fake_data(): db.session.commit() FIDO.create( - credential_id = "umR9q5vX61XG7vh7gi8wT0gJ9LkYwHKSzDL5vhtZs3o", - uuid = "59576167-6c37-4d67-943b-4683b24ff821", - public_key = "pQECAyYgASFYIEjQg3TOuUZJxylLE6gJDNHcNyYVW5hOAZ-vGOY9I_TDIlggfJqIh07bj3n6RVmrEsuozsYPYM6VeJKCeduz0DFp8AY", - sign_count = 1, + credential_id="umR9q5vX61XG7vh7gi8wT0gJ9LkYwHKSzDL5vhtZs3o", + uuid="59576167-6c37-4d67-943b-4683b24ff821", + public_key="pQECAyYgASFYIEjQg3TOuUZJxylLE6gJDNHcNyYVW5hOAZ-vGOY9I_TDIlggfJqIh07bj3n6RVmrEsuozsYPYM6VeJKCeduz0DFp8AY", + sign_count=1, ) FIDO.create( - credential_id = "1mR9q5vX61XG7vh7gi8wT0gJ9LkYwHKSzDL5vhtZs3o", - uuid = "59576167-6c37-4d67-943b-4683b24ff821", - public_key = "1QECAyYgASFYIEjQg3TOuUZJxylLE6gJDNHcNyYVW5hOAZ-vGOY9I_TDIlggfJqIh07bj3n6RVmrEsuozsYPYM6VeJKCeduz0DFp8AY", - sign_count = 1, + credential_id="1mR9q5vX61XG7vh7gi8wT0gJ9LkYwHKSzDL5vhtZs3o", + uuid="59576167-6c37-4d67-943b-4683b24ff821", + public_key="1QECAyYgASFYIEjQg3TOuUZJxylLE6gJDNHcNyYVW5hOAZ-vGOY9I_TDIlggfJqIh07bj3n6RVmrEsuozsYPYM6VeJKCeduz0DFp8AY", + sign_count=1, ) db.session.commit()