Implements authentication and authorization as FastAPI dependencies

Overview

FastAPI Security

Implements authentication and authorization as dependencies in FastAPI.

Features

  • Authentication via JWT-based OAuth 2 access tokens and via Basic Auth
  • Pydantic-based User model for authenticated and anonymous users
  • Sub-classable UserPermission dependency to check against the permissions attribute returned in OAuth 2 access tokens
  • Able to extract user info from access tokens via OpenID Connect

Limitations

  • Only supports validating access tokens using public keys from a JSON Web Key Set (JWKS) endpoint. I.e. for use with external identity providers such as Auth0 and ORY Hydra.
  • Permissions can only be picked up automatically from OAuth2 tokens, from the non-standard permissions list attribute (Auth0 provides this, maybe other identity providers as well). For all other use cases, permission_overrides must be used. For example if there's a basic auth user called user1 you can set permission_overrides={"user1": ["*"]} to give the user access to all permissions, or permission_overrides={"user1": ["products:create"]} to only assign user1 with the permission products:create.

Installation

pip install fastapi-security

Usage examples

Examples on how to use can be found here.

TODO

  • Write more tests
Comments
  • Server with *only* basic auth configured returns WWW-Authenticate

    Server with *only* basic auth configured returns WWW-Authenticate "Bearer" whereas it should probably return "Basic"

    Hi!

    First off, thank you for a nice and useful package! Please find a bug report below.

    Repro

    • any fastapi app
    • fastapi security configured with:
    from fastapi_security import FastAPISecurity
    security = FastAPISecurity()
    security.init_basic_auth(settings.BASIC_AUTH_CREDENTIALS)
    
    • request:
    $ curl localhost:8080/api/v1/ -v
    

    Expected Behaviour

    Response contains WWW-Authenticate: Basic header, as it is the only supported authentication method.

    Actual Behaviour:

    $ curl localhost:8080/api/v1/ -v
    *   Trying 127.0.0.1:8080...
    * TCP_NODELAY set
    * Connected to localhost (127.0.0.1) port 8080 (#0)
    > GET /api/v1/ HTTP/1.1
    > Host: localhost:8080
    > User-Agent: curl/7.68.0
    > Accept: */*
    > 
    * Mark bundle as not supporting multiuse
    < HTTP/1.1 401 Unauthorized
    < date: Thu, 03 Jun 2021 10:34:20 GMT
    < server: uvicorn
    < www-authenticate: Bearer
    < content-length: 43
    < content-type: application/json
    < 
    * Connection #0 to host localhost left intact
    {"detail":"Could not validate credentials"}
    

    There's a section of code that is responsible for www-authenticate header, https://github.com/jmagnusson/fastapi-security/blob/main/fastapi_security/api.py#L212-L215, that is only returning "Basic" if there is a valid Authorization: Basic ... header, which seems like it is not needed.

    According to https://datatracker.ietf.org/doc/html/rfc7235#section-4.1, a WWW-Authenticate header may contain multiple challenges,

    image,

    so it should be possible to return both Basic and Bearer if they are initialized.

    opened by immerrr 2
  • chore(main): release 0.5.0

    chore(main): release 0.5.0

    :robot: I have created a release beep boop

    0.5.0 (2022-03-11)

    ⚠ BREAKING CHANGES

    • Make oauth2 dependencies optional

    Bug Fixes

    • Make oauth2 dependencies optional (e0db0f4)

    Documentation

    • Document new extra for oauth2 support (73e1696)
    • Update changelog to conform to release-please format (c9bfb16)

    This PR was generated with Release Please. See documentation.

    autorelease: tagged 
    opened by github-actions[bot] 1
  • CI: macos, windows jobs run on ubuntu, not on the respective OS

    CI: macos, windows jobs run on ubuntu, not on the respective OS

    Problem

    By declaring ubuntu-latest on the workflows.ci like so:

    jobs:
      ci:
        runs-on: ubuntu-latest
    

    the following happens:

    bug-example (screenshot taken from the latest CI action of this project)

    However, given that the purpose of the CI is to test multiple python versions on multiple OS environments, this is not correct. Instead, the above should declare something like:

    expected (screenshot taken from another open-source project)

    Proposed Solution

    We could replace the above code snippet with a parameterized version of it:

    jobs:
      ci:
        runs-on: ${{ matrix.os }}
    
    opened by ilias-ant 0
  • Make audiences nullable in Oauth2JwtAccessTokenValidator

    Make audiences nullable in Oauth2JwtAccessTokenValidator

    Sometimes it's not required to check for aud. Previously, this library didn't allow that, requiring a list of audiences to check the JWT token against. A simple change of logic allows now allows doing so.

    opened by irdkwmnsb 0
  • Add a way to configure basic auth without storing passwords in plaintext in settings

    Add a way to configure basic auth without storing passwords in plaintext in settings

    This PR's goal is to enable storing password digests (instead of plaintext) to increase security.

    It is admittedly a very early version aimed mostly at collecting feedback. I tried to introduce this with as little change as possible to the existing functionality to maintain backward compat, althought it is probably possible to add this to the basic_auth class directly.

    I'm open to suggestions, and I have enabled edits by maintainers if you feel like applying some minor changes directly. Also, feel free to take this as a proof-of-concept, and implement it in a completely independent branch, that's absolutely fine by me.

    opened by immerrr 6
Releases(v0.5.0)
Owner
Jacob Magnusson
Jacob Magnusson
Authentication testing framework

What is this This is a framework designed to test authentication for web applications. While web proxies like ZAProxy and Burpsuite allow authenticate

DigeeX 140 Jul 06, 2022
Crie seus tokens de autenticação com o AScrypt.

AScrypt tokens O AScrypt é uma forma de gerar tokens de autenticação para sua aplicação de forma rápida e segura. Todos os tokens que foram, mesmo que

Jaedson Silva 0 Jun 24, 2022
Google Auth Python Library

Google Auth Python Library This library simplifies using Google's various server-to-server authentication mechanisms to access Google APIs. Installing

Google APIs 598 Jan 07, 2023
Two factor authentication system using azure services and python language and its api's

FUTURE READY TALENT VIRTUAL INTERSHIP PROJECT PROJECT NAME - TWO FACTOR AUTHENTICATION SYSTEM Resources used: * Azure functions(python)

BHUSHAN SATISH DESHMUKH 1 Dec 10, 2021
Alisue 299 Dec 06, 2022
Pingo provides a uniform API to program devices like the Raspberry Pi, BeagleBone Black, pcDuino etc.

Pingo provides a uniform API to program devices like the Raspberry Pi, BeagleBone Black, pcDuino etc. just like the Python DBAPI provides an uniform API for database programming in Python.

Garoa Hacker Clube 12 May 22, 2022
Extending the Django authentication system with a phone verification step.

Extending the Django authentication system with a phone verification step.

Miguel Grinberg 50 Dec 04, 2022
Kube OpenID Connect is an application that can be used to easily enable authentication flows via OIDC for a kubernetes cluster

Kube OpenID Connect is an application that can be used to easily enable authentication flows via OIDC for a kubernetes cluster. Kubernetes supports OpenID Connect Tokens as a way to identify users wh

7 Nov 20, 2022
Login System Using Django

Login System Django

Nandini Chhajed 6 Dec 12, 2021
Boilerplate/Starter Project for building RESTful APIs using Flask, SQLite, JWT authentication.

auth-phyton Boilerplate/Starter Project for building RESTful APIs using Flask, SQLite, JWT authentication. Setup Step #1 - Install dependencies $ pip

sandhika 0 Aug 03, 2022
Connect-4-AI - AI that plays Connect-4 using the minimax algorithm

Connect-4-AI Brief overview I coded up the Connect-4 (or four-in-a-row) game in

Favour Okeke 1 Feb 15, 2022
A host-guest based app in which host can CREATE the room. and guest can join room with room code and vote for song to skip. User is authenticated using Spotify API

A host-guest based app in which host can CREATE the room. and guest can join room with room code and vote for song to skip. User is authenticated using Spotify API

Aman Raj 5 May 10, 2022
Authentication, JWT, and permission scoping for Sanic

Sanic JWT Sanic JWT adds authentication protection and endpoints to Sanic. It is both easy to get up and running, and extensible for the developer. It

Adam Hopkins 229 Jan 05, 2023
Ready to use and customizable Authentications and Authorisation management for FastAPI ⚡

AuthenticationX 💫 Ready-to-use and customizable Authentications and Oauth2 management for FastAPI ⚡ Source Code: https://github.com/yezz123/AuthX Doc

Yasser Tahiri 404 Dec 27, 2022
Flask user session management.

Flask-Login Flask-Login provides user session management for Flask. It handles the common tasks of logging in, logging out, and remembering your users

Max Countryman 3.2k Dec 28, 2022
Luca Security Concept

Luca Security Concept This is the document source of luca's security concept. Please go here for the HTML version: https://luca-app.de/securityconcept

luca 43 Oct 22, 2022
An open source Flask extension that provides JWT support (with batteries included)!

Flask-JWT-Extended Features Flask-JWT-Extended not only adds support for using JSON Web Tokens (JWT) to Flask for protecting views, but also many help

Landon Gilbert-Bland 1.4k Jan 04, 2023
Graphical Password Authentication System.

Graphical Password Authentication System. This is used to increase the protection/security of a website. Our system is divided into further 4 layers of protection. Each layer is totally different and

Hassan Shahzad 12 Dec 16, 2022
A Login/Registration GUI Application with SQLite database for manipulating data.

Login-Register_Tk A Login/Registration GUI Application with SQLite database for manipulating data. What is this program? This program is a GUI applica

Arsalan 1 Feb 01, 2022
Django Authetication with Twitch.

Django Twitch Auth Dependencies Install requests if not installed pip install requests Installation Install using pip pip install django_twitch_auth A

Leandro Lopes Bueno 1 Jan 02, 2022