Skip to main content

Discord OAuth2 Implicit Grant Dependency for FastAPI

Project description

Harmony Auth

Discord OAuth2 Implicit Grant thingy for FastAPI.

Tokens are cached in Redis with the user's information & guilds (if enabled)

Yes. I know this is a mess. :)

Server Usage

from harmony_auth import HarmonyAuth
from fastapi import FastAPI, Depends

auth = HarmonyAuth()
app = FastAPI()


@app.get('/secure')
async def secure_route(user=Depends(auth)):
    return user

Client Usage

  1. Request an implicit grant access token from Discord
  2. Pass received access_token to any endpoint with the Harmony Auth dependency to login and access resources.
curl -X GET --location "http://127.0.0.1:8000/secure" \
    -H "Accept: application/json" \
    -H "Authorization: Bearer {{access_token}}"

Reload User Data

If you need to reload the user's data in the cache (if they joined a guild, for example), call get_user(token, force_fetch=True)

Example:

@app.get("/refresh")
async def refresh_user_data(token: str = Depends(auth.token)):
    user = await auth.get_user(token, force_fetch=True)
    ...

This will update the cache and return the user.

Log Out / Revoke token

Use revoke_token(token) to remove user data from the cache. This removes all the cached user information.

If you specify a client_id and client_secret, Harmony Auth will request that Discord revokes the token.

Q: How do I log in?

A: You don't need to log in. Just provide a valid access token.

Rate limit protections

I personally don't want to handle this, so I am using twilight-http-proxy.

TODO: Create fork to add OAuth2 routes to twilight http proxy.

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

harmony_auth-0.1.4.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

harmony_auth-0.1.4-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file harmony_auth-0.1.4.tar.gz.

File metadata

  • Download URL: harmony_auth-0.1.4.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0b3 CPython/3.9.13 Darwin/21.6.0

File hashes

Hashes for harmony_auth-0.1.4.tar.gz
Algorithm Hash digest
SHA256 d368af47798b1ed9622463f58b2bfa14f795d9a670a5e7ea267b9cb7bf5e162e
MD5 8d2d8419bd7919e9568eac637b69aa48
BLAKE2b-256 2315404feac10379edfe591d08b553c3c9d88211e9a04ab6093e033600e40da6

See more details on using hashes here.

File details

Details for the file harmony_auth-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: harmony_auth-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.0b3 CPython/3.9.13 Darwin/21.6.0

File hashes

Hashes for harmony_auth-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 7adbf30f2495deb9669dff08a872627ead8cc0e090bc7e97b8f1ffec163edeb7
MD5 e4eff3671d37cabc93c3e53fa00f3145
BLAKE2b-256 ac142c9c31e81305643eea4b0237b3da15da787e113c8dd4e1b60c2f529fe170

See more details on using hashes here.

Supported by

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