Skip to main content

No project description provided

Project description

Starlette OAuth2

A Startlette middleware for authentication and authorization through oauth2.

This middleware is intented to be used when the application relies on an external tenant (e.g. Microsoft AD) for authentication, and the organization's user groups (externally administred) for authorization.

It assumes that a user that is not authenticated is not authorized to do anything.

Check example/ for a concrete implementation.

How to run the example

This guide uses Microsoft (Azure AD) as the tenant. Refer to your tenant for details.

The values in capital such as CLIENT_ID are to be replaced in example/.venv.

  1. Generate a secret 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.

    • replace the value on CLIENT_ID by the value on Application (client) ID
    • replace the value on TENANT_ID by the value on Directory (tenant) ID
  3. In Certificates & secrets, create a new client secret.

    • replace the value on CLIENT_SECRET by the value of the key you just created under Client secrets
  4. Go to Azure AD groups and create a new group.

    • replace the value on USER_GROUP_ID by the Object ID of that group.
  5. Install dependencies and run:

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

When you visit http://localhost:5001, you will be redirected to your tenant, to authorize access. Once authorized, you will be redirected back to http://localhost:5001, and your email will appear. If you try http://localhost:5001/private, you will get a message that you are not authorized to see that.

If you add yourself to the group created in step 3, you are now authorized to see http://localhost:5001/private.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

starlette-oauth2-0.1.2.tar.gz (4.3 kB view details)

Uploaded Source

File details

Details for the file starlette-oauth2-0.1.2.tar.gz.

File metadata

  • Download URL: starlette-oauth2-0.1.2.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.40.0 CPython/3.7.3

File hashes

Hashes for starlette-oauth2-0.1.2.tar.gz
Algorithm Hash digest
SHA256 a1888f600ed636c7af5004ecd7c133ae725ff94ea3dc1bc80bb934347cb529ed
MD5 56b661cd99e39c2737ad12b0f5e7031c
BLAKE2b-256 2f03ccb0d29db19d885948c09a1a439b5a5a57b4b832305ad89aef55bf755043

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page