Skip to content

Commit

Permalink
Merge pull request #198 from duo-labs/fix/document-default-challenge-…
Browse files Browse the repository at this point in the history
…length

fix/document-default-challenge-length
  • Loading branch information
MasterKale committed Jan 11, 2024
2 parents 3a4be36 + c471b0f commit 41d6eca
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
11 changes: 3 additions & 8 deletions tests/test_generate_challenge.py
Expand Up @@ -7,16 +7,11 @@ class TestWebAuthnGenerateChallenge(TestCase):
def test_generates_byte_sequence(self) -> None:
output = generate_challenge()

assert type(output) == bytes
assert len(output) == 64
self.assertEqual(type(output), bytes)
self.assertEqual(len(output), 64)

def test_generates_unique_value_each_time(self) -> None:
output1 = generate_challenge()
output2 = generate_challenge()

assert output1 != output2

def test_supports_custom_lengths(self) -> None:
output = generate_challenge(32)

assert len(output) == 32
self.assertNotEqual(output1, output2)
2 changes: 1 addition & 1 deletion webauthn/authentication/generate_authentication_options.py
Expand Up @@ -20,7 +20,7 @@ def generate_authentication_options(
Args:
`rp_id`: The Relying Party's unique identifier as specified in attestations.
(optional) `challenge`: A byte sequence for the authenticator to return back in its response. If no value is specified then a sequence of random bytes will be generated.
(optional) `challenge`: A byte sequence for the authenticator to return back in its response. Defaults to 64 random bytes.
(optional) `timeout`: How long in milliseconds the browser should give the user to choose an authenticator. This value is a *hint* and may be ignored by the browser.
(optional) `allow_credentials`: A list of credentials registered to the user.
(optional) `user_verification`: The RP's preference for the authenticator's enforcement of the "user verified" flag.
Expand Down
10 changes: 7 additions & 3 deletions webauthn/helpers/generate_challenge.py
@@ -1,8 +1,12 @@
import secrets


def generate_challenge(length: int = 64) -> bytes:
def generate_challenge() -> bytes:
"""
Generate a random authenticator challenge
Create a random value for the authenticator to sign, going above and beyond the recommended
number of random bytes as per https://www.w3.org/TR/webauthn-2/#sctn-cryptographic-challenges:
"In order to prevent replay attacks, the challenges MUST contain enough entropy to make
guessing them infeasible. Challenges SHOULD therefore be at least 16 bytes long."
"""
return secrets.token_bytes(length)
return secrets.token_bytes(64)
2 changes: 1 addition & 1 deletion webauthn/registration/generate_registration_options.py
Expand Up @@ -61,7 +61,7 @@ def generate_registration_options(
`user_name`: A value that will help the user identify which account this credential is associated with. Can be an email address, etc...
(optional) `user_id`: A collection of random bytes that identify a user account. For privacy reasons it should NOT be something like an email address. Defaults to 64 random bytes.
(optional) `user_display_name`: A user-friendly representation of their account. Can be a full name ,etc... Defaults to the value of `user_name`.
(optional) `challenge`: A byte sequence for the authenticator to return back in its response. If no value is specified then a sequence of random bytes will be generated.
(optional) `challenge`: A byte sequence for the authenticator to return back in its response. Defaults to 64 random bytes.
(optional) `timeout`: How long in milliseconds the browser should give the user to choose an authenticator. This value is a *hint* and may be ignored by the browser.
(optional) `attestation`: The level of attestation to be provided by the authenticator.
(optional) `authenticator_selection`: Require certain characteristics about an authenticator, like attachment, support for resident keys, user verification, etc...
Expand Down

0 comments on commit 41d6eca

Please sign in to comment.