CNSA-276-FP/tet.py

57 lines
1.7 KiB
Python
Raw Normal View History

2024-04-26 17:45:27 -07:00
from fido2.server import Fido2Server
2024-04-26 18:08:06 -07:00
from fido2.webauthn import (PublicKeyCredentialRpEntity, PublicKeyCredentialUserEntity,
PublicKeyCredentialParameters, PublicKeyCredentialCreationOptions)
2024-04-26 17:45:27 -07:00
from fido2.client import Fido2Client
from fido2.hid import CtapHidDevice
2024-04-26 18:08:06 -07:00
import os
# Setup the relying party (RP) entity
2024-05-02 09:13:51 -07:00
rp = PublicKeyCredentialRpEntity("eggtech.net", "test")
2024-04-26 18:08:06 -07:00
# Setup the user entity
user = PublicKeyCredentialUserEntity(
2024-05-02 09:13:51 -07:00
id=b'91974', # User ID as bytes
name="cyrus@eggtech.net",
display_name="Cyrus Schick"
2024-04-26 18:08:06 -07:00
)
# Define the public key credential parameters
cred_params = [
PublicKeyCredentialParameters("public-key", -7), # ES256
PublicKeyCredentialParameters("public-key", -257) # RS256
]
# FIDO2 Server setup
server = Fido2Server(rp)
# Generate a random challenge
challenge = os.urandom(32)
# Manually create the PublicKeyCredentialCreationOptions
options = PublicKeyCredentialCreationOptions(
rp=rp,
user=user,
challenge=challenge,
pub_key_cred_params=cred_params
)
2024-04-26 17:45:27 -07:00
2024-04-26 18:08:06 -07:00
# Start the registration process (adjust this method if needed)
registration_data, state = server.register_begin(
user=user,
challenge=challenge
)
# Assuming the device is the first one connected
device = next(CtapHidDevice.list_devices(), None)
if device is None:
2024-04-26 17:45:27 -07:00
raise ValueError("No FIDO device found")
2024-04-26 18:08:06 -07:00
# Client instance for the device
2024-04-30 17:59:17 -07:00
client = Fido2Client(device, "eggtech.net")
2024-04-26 18:08:06 -07:00
# Use the manual options we created for make_credential
attestation_object, client_data = client.make_credential(options)
# Finalize the registration to validate the response and store the credentials
auth_data = server.register_complete(state, client_data, attestation_object)