Skip to main content

Starlette OAuth2

A Startlette middleware for authentication through oauth2's via a secret key, which is often used to add authentication and authorization to a web application that interacts with an API on behalf of the user.

This middleware is intented to be used when the application relies on an external tenant (e.g. Microsoft AD) for authentication.

Check example/ for a concrete implementation.

How to run the example against Microsoft AD

Note: the values in capital such as CLIENT_ID are to be added to example/.venv.

  1. Generate a secret (e.g. openssl rand -base64 32) and write its value on SECRET_KEY

  2. Go to Azure AD, create an app registration (app registrations), give it a name, and add http://localhost:5001/authorized as a Redirect URI.

    • add the value on Application (client) ID to CLIENT_ID
    • add the value on Endpoints > OpenID Connect metadata document to SERVER_METADATA_URL
  3. In Certificates & secrets tab, create a new client secret.

    • add the value of the key you just created under Client secrets to CLIENT_SECRET
  4. Install dependencies and run:

cd example
python -m venv venv
venv/bin/pip install -r requirements.txt
venv/bin/python -m app

When you visit http://localhost:5001/public, you will see that you are not authenticated. When you visit http://localhost:5001/other, you will be redirected to your tenant, to authenticate. Once authenticated, you will be redirected back to http://localhost:5001/other, and your email will appear.

Public endpoints are optional. They are useful for e.g. health checks.

Release files for starlette-oauth2 0.3.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for starlette-oauth2 0.3.4
File Size Uploaded
starlette-oauth2-0.3.4.tar.gz 3.3 kB Details

Release files / starlette-oauth2-0.3.4.tar.gz

Download URL starlette-oauth2-0.3.4.tar.gz
Size 3.3 kB
Tags Source
SHA-256 checksum
How to use checksums
ff270a0e1e9127c0e49976f3e5844406acf8c8097b5c84eb82e7757fdda2a4bc
BLAKE2b-256 checksum
How to use checksums
f83b347a8c36d2288984aab228d4ae1c4ee42fb0d555d90e8d341e8179ebbb77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.4

Release history Release notifications | RSS feed

This release

0.3.4 This release

1 release file

0.3.3

1 release file

0.3.2

1 release file

0.3.1

1 release file

0.3.0

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2.0

1 release file

0.1.3

1 release file

0.1.2

1 release file

0.1.1

1 release file

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page