Skip to content

Commit

Permalink
Handle PIN_AUTH_BLOCKED better
Browse files Browse the repository at this point in the history
  • Loading branch information
dainnilsson committed Nov 6, 2024
1 parent 190b682 commit 5bcdf75
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 27 deletions.
74 changes: 50 additions & 24 deletions fido2/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,17 +726,30 @@ def _do_make():
pin_token,
)

try:
att_obj, pin_protocol, pin_token = _do_make()
except CtapError as e:
# The Authenticator may still require UV, try again
if (
e.code == CtapError.ERR.PUAT_REQUIRED
and user_verification == UserVerificationRequirement.DISCOURAGED
):
user_verification = UserVerificationRequirement.REQUIRED
dev = self.ctap2.device
reconnected = False
while True:
try:
att_obj, pin_protocol, pin_token = _do_make()
else:
break
except CtapError as e:
# The Authenticator may still require UV, try again
if (
e.code == CtapError.ERR.PUAT_REQUIRED
and user_verification == UserVerificationRequirement.DISCOURAGED
):
user_verification = UserVerificationRequirement.REQUIRED
continue
# NFC may require reconnect
if (
e.code == CtapError.ERR.PIN_AUTH_BLOCKED
and hasattr(dev, "connect")
and not reconnected
):
dev.close()
dev.connect()
reconnected = True # We only want to try this once
continue
raise

# Process extenstion outputs
Expand Down Expand Up @@ -847,17 +860,30 @@ def _do_auth():
pin_protocol,
)

try:
return _do_auth()
except CtapError as e:
# The Authenticator may still require UV, try again
if (
e.code == CtapError.ERR.PUAT_REQUIRED
and user_verification == UserVerificationRequirement.DISCOURAGED
):
user_verification = UserVerificationRequirement.REQUIRED
dev = self.ctap2.device
reconnected = False
while True:
try:
return _do_auth()
raise
except CtapError as e:
# The Authenticator may still require UV, try again
if (
e.code == CtapError.ERR.PUAT_REQUIRED
and user_verification == UserVerificationRequirement.DISCOURAGED
):
user_verification = UserVerificationRequirement.REQUIRED
continue
# NFC may require reconnect
if (
e.code == CtapError.ERR.PIN_AUTH_BLOCKED
and hasattr(dev, "connect")
and not reconnected
):
dev.close()
dev.connect()
reconnected = True # We only want to try this once
continue
raise


class Fido2Client(WebAuthnClient, _BaseClient):
Expand Down Expand Up @@ -1037,7 +1063,7 @@ def __init__(
def is_available() -> bool:
return platform.system().lower() == "windows" and WinAPI.version > 0

def make_credential(self, options, **kwargs):
def make_credential(self, options, event=None):
"""Create a credential using Windows WebAuthN APIs.
:param options: PublicKeyCredentialCreationOptions data.
Expand Down Expand Up @@ -1092,7 +1118,7 @@ def make_credential(self, options, **kwargs):
),
options.exclude_credentials,
options.extensions,
kwargs.get("event"),
event,
enterprise_attestation,
)
except OSError as e:
Expand All @@ -1103,7 +1129,7 @@ def make_credential(self, options, **kwargs):
client_data, AttestationObject(result), extensions
)

def get_assertion(self, options, **kwargs):
def get_assertion(self, options, event=None):
"""Get assertion using Windows WebAuthN APIs.
:param options: PublicKeyCredentialRequestOptions data.
Expand Down Expand Up @@ -1131,7 +1157,7 @@ def get_assertion(self, options, **kwargs):
),
options.allow_credentials,
options.extensions,
kwargs.get("event"),
event,
)
)
except OSError as e:
Expand Down
9 changes: 6 additions & 3 deletions fido2/pcsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,11 @@ class CtapPcscDevice(CtapDevice):
"""

def __init__(self, connection: CardConnection, name: str):
self._name = name
self._capabilities = CAPABILITY(0)
self.use_ext_apdu = False
self._conn = connection
self._conn.connect()
self._name = name
self._select()
self.connect()

try: # Probe for CTAP2 by calling GET_INFO
self.call(CTAPHID.CBOR, b"\x04")
Expand All @@ -73,6 +72,10 @@ def __init__(self, connection: CardConnection, name: str):
if not self._capabilities:
raise ValueError("Unsupported device")

def connect(self):
self._conn.connect()
self._select()

def __repr__(self):
return f"CtapPcscDevice({self._name})"

Expand Down

0 comments on commit 5bcdf75

Please sign in to comment.