SlideShare a Scribd company logo
Andreas Jung • info@zopyx.com • www.zopyx.com • www.andreas-jung.com
zopyx-fastapi-auth
An opinionated security solution for FastAPI
Agenda
• Introduction to FastAPI
• Quick intro intro authentication and authorization
• Key concepts of fastapi-auth
• Integration and usage in FastAPI
• Pluggable user source architecture
• User management
• Demo time
• Q & A
Publishing developer & consultant
since mid-90s
https://www.zopyx.com
https://andreas-jung.com
✉ info@zopyx.com
Funder of print-css.rocks project
Independent PrintCSS consulting
- Software developer & software architect
- Requirements engineering
- Python developer & Python expert
- NGOs, EDU
- medical & pharmaceutical
- energy
- research & development quantum mechanics
- CMS Solutions
- Publishing Solutions
- Individual software solutions
About me
Introduction to FastAPI
What is FastAPI?
FastAPI is a modern, fast (high-performance), web framework
for building APIs with Python 3 based on standard Python type
hints and the data-validation framework Pydantic.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: str = None):
return {"item_id": item_id, "q": q}
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)
Reasons for using FastAPI
• Use of Pydantic
FastAPI uses Pydantic for data validation and serialization, ensuring robust input and output
handling.
• Full Async Support
FastAPI supports asynchronous programming, enabling high-performance and efficient request
handling.
• Automatic OpenAPI Generation
FastAPI automatically generates OpenAPI documentation, simplifying API development and testing.
• It is Fast
FastAPI is optimized for speed, making it one of the fastest web frameworks available.
• High Adoption Rate
FastAPI’s growing popularity and community support make it a reliable choice for modern web
development.
Authentication Authorization
What is authentication?
• Authentication verifies the identity of a user, device,
or entity.
• It uses credentials like username + passwords,
biometrics, or security tokens.
• This ensures only authorized access to
sensitive information or resources.
What is authorization?
• Authorization determines what
resources a user or system can
access.
• It occurs after authentication and
enforces permissions and policies.
• This ensures users can only
perform actions they are
permitted to.
zopyx-fastapi-auth
An opinionated authentication solution for FastAPI
Why zopyx-fastapi-auth
…when there are already many other solutions!?
• Usecase
Migration of a CMS Plone-based application from Plone
to FastAPI for the University of Saarbrücken.
• Problem
• None of the existing authentication frameworks for FastAPI has fit our needs
• No direct translation of security concepts of the legacy system
available for FastAPI
• Transparent authentication against three different
user sources (local, SAP, LDAP)
Concepts: Permissions
An access right that defines what actions a user or role can
perform, such as viewing, editing, or deleting resources
within an application.
Permissions are defined as code.
from fastapi_auth.permissions import Permission
VIEW_PERMISSION = Permission(name="view", description="View permission")
EDIT_PERMISSION = Permission(name="edit", description="Edit permission")
DELETE_PERMISSION = Permission(name="delete", description="Delete permission")
Concepts: Roles
A collection of permissions
wrapped together as a role.
Roles define a user’s access level
and responsibilities within an
application.
A user can have multiple roles,
each granting a different set of
permissions.
Roles are defined in code.
from fastapi_auth.permissions import Role
ADMIN_ROLE = Role(
name="Administrator",
description="Admin role",
permissions=[VIEW_PERMISSION, EDIT_PERMISSION, DELETE_PERMISSION],
)
USER_ROLE = Role(
name="User",
description="User role",
permissions=[VIEW_PERMISSION, EDIT_PERMISSION],
)
VIEWER_ROLE = Role(
name="Viewer",
description="Viewer role",
permissions=[VIEW_PERMISSION],
)
Concepts: Roles II
Roles must be registered with the (gobal) roles registry.
Interim solution…likely to go away soon.
from fastapi_auth.roles import ROLES_REGISTRY
ROLES_REGISTRY.register(ADMIN_ROLE)
ROLES_REGISTRY.register(USER_ROLE)
ROLES_REGISTRY.register(VIEWER_ROLE)
Concepts: Users
A user represents the currently user context accessing the system.
User types:
• Anonymous User
• Authenticated User
• (Superuser)
Users are stored in (external) user sources like a database, LDAP etc.
The User object in fastapi-auth
• The Protected() method is used to protect an endpoint
by permission, role or a custom checker
• Integration with FastAPI through dependency injection
• Successful authorization will return a User object
• Unsuccessful authorization ➡ HTTP 403/Forbidden
• User properties: name, description, roles, is_anonymous
@app.get(„/admin")
def admin(user: User = Depends(Protected(<protection_conditions)):
return {"user": user}
Concepts II
• A user can have multiple roles
• A role can multiple permissions
How to use fastapi-auth in your FastAPI app?
Endpoint protection by role(s)
# This is an endpoint that requires the user to be authenticated. In this case,
# the user must have the ADMIN_ROLE role. It is also possible to require a
# permission instead. Use the Protected dependency to require authentication.
# An unauthenticated request as ANONYMOUS_USER will be rejected.
@app.get(„/admin")
def admin(user: User = Depends(Protected(required_roles=[ADMIN_ROLE]))):
return {"user": user}
How to use fastapi-auth in your FastAPI app?
Endpoint protection by permission
from fastapi_auth.dependencies import Protected
@app.get(„/admin2")
def admin2(user: User = Depends(Protected(required_permission=VIEW_PERMISSION))):
return {"user": user}
How to use fastapi-auth in your FastAPI app?
Endpoint protection by custom checker
from fastapi_auth.dependencies import Protected
def my_check(request: Request, user: User) -> bool:
# perform some checks based on request and/or user....
return True # or False
@app.get(„/admin3")
def admin3(user: User = Depends(Protected(required_checker=my_check))):
return {"user": user}
Concept of user sources
• A user source manages user accounts
• A user source can authenticate (or not) a user based on
the parameters of a login form
• …usually determined by validating the username and the
password against the data stored in a database
• Typical: RDBMS, LDAP etc.
Pluggable authenticators
from fastapi import Request
from fastapi.authenticator_registry import Authenticator, AUTHENTICATOR_REGISTRY
from fastapi.users import User
class MyAuthenticator(Authenticator):
async def authenticate(request: Request) -> User:
# extract credentials from request
username = request.form....
password = request.form....
# perform authentication against your own authentication system
user_data = my_backend.authenticate_user(username, password)
return User(name=user_data["name"], roles=[...])
AUTHENTICATOR_REGISTRY.add_authenticator(MyAuthenticator(), 0)
Build-in user management
• zopyx-fastapi-auth comes with a default user management
• with sqlite as database backend
• commandline utility fastapi-auth-user-admin
> fastapi-auth-user-admin add <username> <password> „Role1,Role2…“
> fastapi-auth-user-admin delete <username>
> fastapi-auth-user-admin lists-users
> fastapi-auth-user-admin set-password <username> <new-password>
Internals
• based on starlette.middleware.sessions
• authentication information stored and exchanged through
an encrypted cookie (symmetric encryption)
• client/browser can not decrypt the cookie
• lifetime of cookie: session or persistent
• ToDo:
• switching to secure cookies (starlette-securecookies)
• better control over lifetime of cookies, expiration, revocation!?
Demo time 🥳
• https://github.com/zopyx/fastapi-auth
• https://pypi.org/project/zopyx-fastapi-auth/
Resources
Questions & Answers
Thank you for listening
Andreas Jung • info@zopyx.com • www.zopyx.com • www.andreas-jung.com

More Related Content

PDF
Cache Security- The Basics
PPTX
Code your Own: Authentication Provider for Blackboard Learn
PPTX
Aws iam best practices to live by
PDF
FIWARE Global Summit - Adding Identity Management, Access Control and API Man...
PDF
Yii Framework Security
PDF
Federated Authentication in a Campus System
PDF
E5: Predix Security with ACS & UAA (Predix Transform 2016)
PDF
Implementing Authorization
Cache Security- The Basics
Code your Own: Authentication Provider for Blackboard Learn
Aws iam best practices to live by
FIWARE Global Summit - Adding Identity Management, Access Control and API Man...
Yii Framework Security
Federated Authentication in a Campus System
E5: Predix Security with ACS & UAA (Predix Transform 2016)
Implementing Authorization

Similar to zopyx-fastapi-auth - authentication and authorization for FastAPI (20)

PPT
SAP BI 7 security concepts
PDF
MongoDB World 2019: Securing Application Data from Day One
PPTX
Keycloak for Science Gateways - SGCI Technology Sampler Webinar
PDF
Welcome to the Jungle: Pentesting AWS
PPTX
Adding Identity Management and Access Control to your App
PDF
Automated Inference of Access Control Policies for Web Applications
PDF
Attribute-Based Access Control in Symfony
PPTX
Externalizing Authorization in Micro Services world
PDF
Cache Security- Configuring a Secure Environment
PPTX
Intro to Apache Shiro
PDF
S5-Authorization
PPTX
Presentation
PPTX
Adding identity management and access control to your app
PPTX
Api security-eic-prabath
PDF
Securing FIWARE Architectures
PDF
Super simple application security with Apache Shiro
PDF
Governance and Security Solution Patterns
PDF
IBM Cloud Paris meetup 20180329 - Access Management & Social coding
PPTX
SC-900 Capabilities of Microsoft Identity and Access Management Solutions
PPTX
Q4_Fortify your IBM Power Systems with Strong Access Control_E_FINAL.pptx
SAP BI 7 security concepts
MongoDB World 2019: Securing Application Data from Day One
Keycloak for Science Gateways - SGCI Technology Sampler Webinar
Welcome to the Jungle: Pentesting AWS
Adding Identity Management and Access Control to your App
Automated Inference of Access Control Policies for Web Applications
Attribute-Based Access Control in Symfony
Externalizing Authorization in Micro Services world
Cache Security- Configuring a Secure Environment
Intro to Apache Shiro
S5-Authorization
Presentation
Adding identity management and access control to your app
Api security-eic-prabath
Securing FIWARE Architectures
Super simple application security with Apache Shiro
Governance and Security Solution Patterns
IBM Cloud Paris meetup 20180329 - Access Management & Social coding
SC-900 Capabilities of Microsoft Identity and Access Management Solutions
Q4_Fortify your IBM Power Systems with Strong Access Control_E_FINAL.pptx
Ad

More from Andreas Jung (20)

PDF
A fool with a tool is still a fool - Plone Tagung 2025 in Koblenz
PDF
State of PrintCSS - MarkupUK 2023.pdf
PDF
Typesense Plone Integration Plone Conference 2022 Namur
PDF
Onkopedia - Plone Tagung 2020 Dresden
PDF
PrintCSS W3C workshop at XMLPrague 2020
PDF
PrintCSS workshop XMLPrague 2020
PDF
Plone 5.2 migration at University Ghent, Belgium
PDF
Back to the future - Plone 5.2 und Python 3 Migration am Beispiel Onkopedia
PDF
Plone migrations using plone.restapi
PDF
Plone Migrationen mit Plone REST API
PDF
Plone im Einsatz bei der Universität des Saarländes als Shop-System und Gefah...
PDF
Generierung von PDF aus XML/HTML mit PrintCSS
PDF
Creating Content Together - Plone Integration with SMASHDOCs
PPTX
Creating Content Together - Plone Integration with SMASHDOCs
PDF
The Plone and The Blockchain
PDF
Content Gemeinsam Erstellen: Integration Plone mit SMASHDOCs
PDF
PDF Generierung mit XML/HTML und CSS - was die Tools können und was nicht.
PDF
Why we love ArangoDB. The hunt for the right NosQL Database
PDF
XML Director - the technical foundation of onkopedia.com
PDF
PyFilesystem
A fool with a tool is still a fool - Plone Tagung 2025 in Koblenz
State of PrintCSS - MarkupUK 2023.pdf
Typesense Plone Integration Plone Conference 2022 Namur
Onkopedia - Plone Tagung 2020 Dresden
PrintCSS W3C workshop at XMLPrague 2020
PrintCSS workshop XMLPrague 2020
Plone 5.2 migration at University Ghent, Belgium
Back to the future - Plone 5.2 und Python 3 Migration am Beispiel Onkopedia
Plone migrations using plone.restapi
Plone Migrationen mit Plone REST API
Plone im Einsatz bei der Universität des Saarländes als Shop-System und Gefah...
Generierung von PDF aus XML/HTML mit PrintCSS
Creating Content Together - Plone Integration with SMASHDOCs
Creating Content Together - Plone Integration with SMASHDOCs
The Plone and The Blockchain
Content Gemeinsam Erstellen: Integration Plone mit SMASHDOCs
PDF Generierung mit XML/HTML und CSS - was die Tools können und was nicht.
Why we love ArangoDB. The hunt for the right NosQL Database
XML Director - the technical foundation of onkopedia.com
PyFilesystem
Ad

Recently uploaded (20)

PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Digital Strategies for Manufacturing Companies
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
top salesforce developer skills in 2025.pdf
PDF
Softaken Excel to vCard Converter Software.pdf
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PPTX
Introduction to Artificial Intelligence
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
ManageIQ - Sprint 268 Review - Slide Deck
CHAPTER 2 - PM Management and IT Context
Digital Strategies for Manufacturing Companies
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PTS Company Brochure 2025 (1).pdf.......
Upgrade and Innovation Strategies for SAP ERP Customers
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Wondershare Filmora 15 Crack With Activation Key [2025
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
top salesforce developer skills in 2025.pdf
Softaken Excel to vCard Converter Software.pdf
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Odoo Companies in India – Driving Business Transformation.pdf
Introduction to Artificial Intelligence
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf

zopyx-fastapi-auth - authentication and authorization for FastAPI

  • 1. Andreas Jung • info@zopyx.com • www.zopyx.com • www.andreas-jung.com zopyx-fastapi-auth An opinionated security solution for FastAPI
  • 2. Agenda • Introduction to FastAPI • Quick intro intro authentication and authorization • Key concepts of fastapi-auth • Integration and usage in FastAPI • Pluggable user source architecture • User management • Demo time • Q & A
  • 3. Publishing developer & consultant since mid-90s https://www.zopyx.com https://andreas-jung.com ✉ info@zopyx.com Funder of print-css.rocks project Independent PrintCSS consulting - Software developer & software architect - Requirements engineering - Python developer & Python expert - NGOs, EDU - medical & pharmaceutical - energy - research & development quantum mechanics - CMS Solutions - Publishing Solutions - Individual software solutions About me
  • 5. What is FastAPI? FastAPI is a modern, fast (high-performance), web framework for building APIs with Python 3 based on standard Python type hints and the data-validation framework Pydantic. from fastapi import FastAPI app = FastAPI() @app.get("/") def read_root(): return {"Hello": "World"} @app.get("/items/{item_id}") def read_item(item_id: int, q: str = None): return {"item_id": item_id, "q": q} if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)
  • 6. Reasons for using FastAPI • Use of Pydantic FastAPI uses Pydantic for data validation and serialization, ensuring robust input and output handling. • Full Async Support FastAPI supports asynchronous programming, enabling high-performance and efficient request handling. • Automatic OpenAPI Generation FastAPI automatically generates OpenAPI documentation, simplifying API development and testing. • It is Fast FastAPI is optimized for speed, making it one of the fastest web frameworks available. • High Adoption Rate FastAPI’s growing popularity and community support make it a reliable choice for modern web development.
  • 8. What is authentication? • Authentication verifies the identity of a user, device, or entity. • It uses credentials like username + passwords, biometrics, or security tokens. • This ensures only authorized access to sensitive information or resources.
  • 9. What is authorization? • Authorization determines what resources a user or system can access. • It occurs after authentication and enforces permissions and policies. • This ensures users can only perform actions they are permitted to.
  • 11. Why zopyx-fastapi-auth …when there are already many other solutions!? • Usecase Migration of a CMS Plone-based application from Plone to FastAPI for the University of Saarbrücken. • Problem • None of the existing authentication frameworks for FastAPI has fit our needs • No direct translation of security concepts of the legacy system available for FastAPI • Transparent authentication against three different user sources (local, SAP, LDAP)
  • 12. Concepts: Permissions An access right that defines what actions a user or role can perform, such as viewing, editing, or deleting resources within an application. Permissions are defined as code. from fastapi_auth.permissions import Permission VIEW_PERMISSION = Permission(name="view", description="View permission") EDIT_PERMISSION = Permission(name="edit", description="Edit permission") DELETE_PERMISSION = Permission(name="delete", description="Delete permission")
  • 13. Concepts: Roles A collection of permissions wrapped together as a role. Roles define a user’s access level and responsibilities within an application. A user can have multiple roles, each granting a different set of permissions. Roles are defined in code. from fastapi_auth.permissions import Role ADMIN_ROLE = Role( name="Administrator", description="Admin role", permissions=[VIEW_PERMISSION, EDIT_PERMISSION, DELETE_PERMISSION], ) USER_ROLE = Role( name="User", description="User role", permissions=[VIEW_PERMISSION, EDIT_PERMISSION], ) VIEWER_ROLE = Role( name="Viewer", description="Viewer role", permissions=[VIEW_PERMISSION], )
  • 14. Concepts: Roles II Roles must be registered with the (gobal) roles registry. Interim solution…likely to go away soon. from fastapi_auth.roles import ROLES_REGISTRY ROLES_REGISTRY.register(ADMIN_ROLE) ROLES_REGISTRY.register(USER_ROLE) ROLES_REGISTRY.register(VIEWER_ROLE)
  • 15. Concepts: Users A user represents the currently user context accessing the system. User types: • Anonymous User • Authenticated User • (Superuser) Users are stored in (external) user sources like a database, LDAP etc.
  • 16. The User object in fastapi-auth • The Protected() method is used to protect an endpoint by permission, role or a custom checker • Integration with FastAPI through dependency injection • Successful authorization will return a User object • Unsuccessful authorization ➡ HTTP 403/Forbidden • User properties: name, description, roles, is_anonymous @app.get(„/admin") def admin(user: User = Depends(Protected(<protection_conditions)): return {"user": user}
  • 17. Concepts II • A user can have multiple roles • A role can multiple permissions
  • 18. How to use fastapi-auth in your FastAPI app? Endpoint protection by role(s) # This is an endpoint that requires the user to be authenticated. In this case, # the user must have the ADMIN_ROLE role. It is also possible to require a # permission instead. Use the Protected dependency to require authentication. # An unauthenticated request as ANONYMOUS_USER will be rejected. @app.get(„/admin") def admin(user: User = Depends(Protected(required_roles=[ADMIN_ROLE]))): return {"user": user}
  • 19. How to use fastapi-auth in your FastAPI app? Endpoint protection by permission from fastapi_auth.dependencies import Protected @app.get(„/admin2") def admin2(user: User = Depends(Protected(required_permission=VIEW_PERMISSION))): return {"user": user}
  • 20. How to use fastapi-auth in your FastAPI app? Endpoint protection by custom checker from fastapi_auth.dependencies import Protected def my_check(request: Request, user: User) -> bool: # perform some checks based on request and/or user.... return True # or False @app.get(„/admin3") def admin3(user: User = Depends(Protected(required_checker=my_check))): return {"user": user}
  • 21. Concept of user sources • A user source manages user accounts • A user source can authenticate (or not) a user based on the parameters of a login form • …usually determined by validating the username and the password against the data stored in a database • Typical: RDBMS, LDAP etc.
  • 22. Pluggable authenticators from fastapi import Request from fastapi.authenticator_registry import Authenticator, AUTHENTICATOR_REGISTRY from fastapi.users import User class MyAuthenticator(Authenticator): async def authenticate(request: Request) -> User: # extract credentials from request username = request.form.... password = request.form.... # perform authentication against your own authentication system user_data = my_backend.authenticate_user(username, password) return User(name=user_data["name"], roles=[...]) AUTHENTICATOR_REGISTRY.add_authenticator(MyAuthenticator(), 0)
  • 23. Build-in user management • zopyx-fastapi-auth comes with a default user management • with sqlite as database backend • commandline utility fastapi-auth-user-admin > fastapi-auth-user-admin add <username> <password> „Role1,Role2…“ > fastapi-auth-user-admin delete <username> > fastapi-auth-user-admin lists-users > fastapi-auth-user-admin set-password <username> <new-password>
  • 24. Internals • based on starlette.middleware.sessions • authentication information stored and exchanged through an encrypted cookie (symmetric encryption) • client/browser can not decrypt the cookie • lifetime of cookie: session or persistent • ToDo: • switching to secure cookies (starlette-securecookies) • better control over lifetime of cookies, expiration, revocation!?
  • 28. Thank you for listening Andreas Jung • info@zopyx.com • www.zopyx.com • www.andreas-jung.com