Skip to main content

A WebAuthn Python module.

Project description

PyWebAuthn

PyPI GitHub license

PyWebAuthn is a Python module which can be used to handle WebAuthn registration and assertion. Currently, WebAuthn is supported in Firefox, Chrome, and Edge.

Installation

pip install webauthn

Usage

Generating credential options, (to be passed to navigator.credentials.create):

make_credential_options = webauthn.WebAuthnMakeCredentialOptions(
    challenge,
    rp_name,
    rp_id,
    user_id,
    username,
    display_name,
    icon_url)

Creating a WebAuthnUser object. Used during the assertion (login) process:

webauthn_user = webauthn.WebAuthnUser(
    user.id,
    user.username,
    user.display_name,
    user.icon_url,
    user.credential_id,
    user.pub_key,
    user.sign_count,
    user.rp_id)

Generating assertion options, (to be passed to navigator.credentials.get):

webauthn_assertion_options = webauthn.WebAuthnAssertionOptions(
    webauthn_user,
    challenge)

Verifying a registration response, (result of navigator.credentials.create):

webauthn_registration_response = webauthn.WebAuthnRegistrationResponse(
    RP_ID,
    ORIGIN,
    registration_response,
    challenge,
    trust_anchor_dir,
    trusted_attestation_cert_required,
    self_attestation_permitted,
    none_attestation_permitted,
    uv_required=False)  # User Verification

try:
    webauthn_credential = webauthn_registration_response.verify()
except Exception as e:
    return jsonify({'fail': 'Registration failed. Error: {}'.format(e)})

# Create User

Verifying an assertion response, (result of navigator.credentials.get):

webauthn_user = webauthn.WebAuthnUser(
    user.ukey,
    user.username,
    user.display_name,
    user.icon_url,
    user.credential_id,
    user.pub_key,
    user.sign_count,
    user.rp_id)

webauthn_assertion_response = webauthn.WebAuthnAssertionResponse(
    webauthn_user,
    assertion_response,
    challenge,
    origin,
    uv_required=False)  # User Verification

try:
    sign_count = webauthn_assertion_response.verify()
except Exception as e:
    return jsonify({'fail': 'Assertion failed. Error: {}'.format(e)})

# Update counter.
user.sign_count = sign_count

Flask Demo

There is a Flask demo available in the flask_demo directory. Follow these steps to run the Flask web app:

  1. cd flask_demo

  2. pip install -r requirements.txt

  3. python create_db.py

  4. python app.py

  5. Go to https://localhost:5000 in your web browser. Try registering and logging in with a compatible U2F or WebAuthn authenticator.

  6. Profit?

Flask Demo (Docker)

To run the Flask demo with Docker:

  1. Install Docker.

  2. docker-compose up -d

  3. Go to https://localhost:5000 in your web browser. Try registering and logging in with a compatible U2F or WebAuthn authenticator.

Demo Troubleshooting

By default, both the local and Docker demos try to run the web app using HTTPS. This may cause issues such as NET::ERR_CERT_AUTHORITY_INVALID on Chrome. To get around this issue on Chrome, you can do the following:

  1. Generate a self-signed certificate through tools like mkcert

  2. Enable requests to localhost over HTTPS through the following flag: chrome://flags/#allow-insecure-localhost.

For Firefox, you should be able to proceed to the page being served by the Flask app by doing the following:

  1. Clicking ‘Advanced’

  2. Clicking ‘Accept the Risk and Continue’.

Unit Tests

To run the unit tests, use the following command from the top directory:

python3 -m unittest

This will run both the py_webauthn library tests and the Flask demo tests.

Note

Currently, PyWebAuthn does not support performing the following verifications.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

webauthn-0.4.7.tar.gz (65.8 kB view details)

Uploaded Source

Built Distribution

webauthn-0.4.7-py2.py3-none-any.whl (18.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file webauthn-0.4.7.tar.gz.

File metadata

  • Download URL: webauthn-0.4.7.tar.gz
  • Upload date:
  • Size: 65.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for webauthn-0.4.7.tar.gz
Algorithm Hash digest
SHA256 8ad9072ff1d6169f3be30d4dc8733ea563dd266962397bc58b40f674a6af74ac
MD5 2b12173bc2b565fc2f6ca0603e4ace41
BLAKE2b-256 c1a3552f18e0d8a257219840f2a536f8c678dcaf95ef652f670f34b62619502a

See more details on using hashes here.

File details

Details for the file webauthn-0.4.7-py2.py3-none-any.whl.

File metadata

  • Download URL: webauthn-0.4.7-py2.py3-none-any.whl
  • Upload date:
  • Size: 18.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for webauthn-0.4.7-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 238391b2e2cc60fb51a2cd2d2d6be149920b9af6184651353d9f95856617a9e7
MD5 856074a857e4c071f68abec5e1c43a51
BLAKE2b-256 c7ed86e4437ed4c15a8c73b791c0500e9c80be5567aaa26356af21d02516b0e3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page