Passkeys in Python Flask: Secure your Flask applications with passkeys by following this tutorial for Python Flask developers.
Janina
Created: September 15, 2023
Updated: August 28, 2024
We aim to make the Internet a safer place using passkeys. That's why we want to support developers with tutorials on how to implement passkeys.
Passkeys are emerging as a passwordless and more secure way to log into websites. In this tutorial, we will learn how to use the Corbado UI components to implement passkey login functionality for a great user experience. Using Python and the Flask framework, we will build a login page that accepts passkeys to authenticate users. With just a few lines of code, you can add this cutting-edge login method to enable fast, simple, and secure access to your web app.
2. Python passkey project prerequisites
3. Repository structure for Python passkey project
5. Configure Corbado for passkey authentication
5.1. Create your Corbado account and project
5.2. Configure environment variables
5.3. Create Flask routes and load environment variables
5.4. Create templates with session management
The image below provides a preview of the passkey login page we will create by following the step-by-step instructions in this tutorial:
This tutorial is suitable for both beginners and experienced developers. To follow along, you'll need to have the following:
pip
installedFor reference, the code snippet below outlines the directory structure of the Flask
passkey app we will be coding in this tutorial. You can grab the full
source code for the complete project from this GitHub URL. Copy the
.env.example
file and rename it to .env
.
Explanation:
/templates
: This folder will contain the HTML templates for your login and home pages..env
: This file will securely store the Corbado project ID.corbado-auth.py
: This is the main file where your Flask application will be defined.Become part of our Passkeys Community for updates and support.
JoinFollow the steps below to set up your project:
pip
is installed by running:pip
to install
Flask, python-dotenv, Corbado Python SDK (which is called passkeys
within pip
):This will install:
Visit the Corbado developer panel to sign up and create your account (you'll see the passkey sign-up in action here!).
After sign-up, a project wizard will guide you through the necessary steps to get everything up and running:
Application URL
and Relying Party ID
. The Application URL
is the URL where you embed the Corbado UI component. In this example, we set it to http://localhost:5000
. The Relying Party ID
is the domain (no protocol, no port, and no path) where passkeys should be bound. Here, it's localhost
(you can define both values als in the Settings > General > URLs of the Corbado developer panel).Afterwards, you will see the relevant HTML / JavaScript code snippets you need to integrate into the project. The subsequent sections of this article will explain them in detail.
As another step, we create an API secret
which will be needed to request user data from the
Corbado backend. Please create an API secret
in Settings > Credentials > API secrets.
In the .env
file, you will store sensitive credentials like your API secret
and project ID as environment variables. This is a security best practice to
avoid hardcoding these values directly in your scripts. To get your API secret
and project ID, visit your Corbado developer panel.
Please refer to the Corbado docs for more details on obtaining the necessary credentials and integrating Corbado authentication in your application.
Create the corbado-auth.py
file, which holds our Flask app that integrates
with Corbado for authentication. In this code, we loaded credentials from
environment variables using python-dotenv
. A Session
class validates JWTs from
cookies to determine the current user. The /
route displays a login page,
while /home
shows the user's profile if authenticated, else raises
Unauthorized
.
Routes are protected by validating JWTs which are short-lived. JWT
signatures are verified using public keys from a JWKS URI. The Session
class
handles JWT validation and protected routes grant access only to valid users. We use Corbado Python SDK
(passkeys) to handle the session.
Now, create HTML templates for the login and home pages. The project ID will
be dynamically inserted into these templates using the Jinja2
template engine,
which is integrated with Flask.
Copy and paste these codes in the templates/login.html
file to create the login
page using the Corbado UI component:
Now, we create our protected home page, visible only after user authentication
and validation. It showcases user details and incorporates the Corbado Python SDK
(passkeys) for
authentication, allowing users to log out with a simple button click.
In both templates, the PROJECT_ID
is dynamically inserted using {{ PROJECT_ID }}
to include the project ID from the environment variables.
Now, run your Flask application using the following command in the terminal:
Your Flask application will start, and you can open a web browser to visit http://localhost:5000 to see your login page.
After a successful login and session validation, you should be redirected to the home page where you can see some user data and the logout button as shown in the image above.
Add passkeys to your Flask app.
Start For FreeImplementing passkey authentication creates a seamless and secure login experience for users, removing the vulnerabilities of passwords. This tutorial showed how to easily integrate passkeys into a Flask app using Corbado.
With just a few lines of Python code, we built a login page that accepts passkeys for authentication. Passkeys represent are the new standard of login, eliminating phishing and breaches tied to reused passwords. By following this guide, you can add cutting-edge passkey support to your own Flask apps and take a step towards a passwordless web.
Table of Contents
Enjoyed this read?
🤝 Join our Passkeys Community
Share passkeys implementation tips and get support to free the world from passwords.
🚀 Subscribe to Substack
Get the latest news, strategies, and insights about passkeys sent straight to your inbox.
We provide UI components, SDKs and guides to help you add passkeys to your app in <1 hour
Start for free