FastAPI Authentication: OAuth2, JWT, & Tortoise ORM Implementation

Added:

Setup & Dependencies
OAuth2 Flow Basics
Simple Auth Demo
Database & Models
User Creation
Token Generation
Protected Endpoints
Client Authentication

Setup & Dependencies

0:00
Playing Section
  • 1

    Installs FastAPI and Hypercorn server for the project.

  • 2

    Creates two examples to demonstrate the authentication flow.

Basic Python asynchronous programming (async/await) and FastAPI framework fundamentals, including dependency injection.
Fundamental database concepts (SQL, tables, relationships) and the general purpose of an Object-Relational Mapper (ORM).
Understanding of HTTP protocol mechanics, specifically request/response lifecycles, HTTP methods, and the Authorization header.
Core security concepts such as the difference between authentication (who you are) and authorization (what you can do), as well as basic password hashing.
Implementing Refresh Tokens and token revocation (blacklisting) strategies to securely manage long-lived user sessions.
Designing Role-Based Access Control (RBAC) and managing granular user permissions using OAuth2 scopes in FastAPI.
Managing database schema evolution and migrations using Aerich, the official migration tool for Tortoise ORM.
Writing robust integration and unit tests for secured API endpoints using Pytest and HTTPX.
Deploying secure APIs, including setting up CORS policies, HTTPS enforcement, and secure handling of environment secrets.
110.4K views2.2Klikes40:02@prettyprintedOriginal Release: 2021-01-05

This video demonstrates how to implement secure authentication in FastAPI applications using OAuth2 with JSON Web Tokens (JWT) and Tortoise ORM. The implementation involves creating an OAuth2 password bearer scheme, a token endpoint that generates JWT tokens upon successful username/password verification, and protected endpoints that require authentication. The process includes using Tortoise ORM for database operations, bcrypt for password hashing, and Pydantic models for data validation. The token endpoint authenticates users against the database, generates JWT tokens containing user information, and protected endpoints decode these tokens to access authenticated user data.