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
- Request an implicit grant access token from Discord
- 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file harmony_auth-0.1.5.tar.gz
.
File metadata
- Download URL: harmony_auth-0.1.5.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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1cb65399fcb84db1a86cfaa99f4590a243fe082c00fcb2ebb808f5d940663d89 |
|
MD5 | a13d0b853effc9932445042be0c868ff |
|
BLAKE2b-256 | 795ff60521a92ca17711b21a6498b8fec7ef4e0ebe6d9a50b6c6045bb89f922d |
File details
Details for the file harmony_auth-0.1.5-py3-none-any.whl
.
File metadata
- Download URL: harmony_auth-0.1.5-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
Algorithm | Hash digest | |
---|---|---|
SHA256 | 96ef6deeb4f081865d6ad2a2f9f0a55cd4065fbd6a6bbdbf915d9469b075894e |
|
MD5 | 2045767ae0d144daf53376cd371f84ac |
|
BLAKE2b-256 | 890c8633e5572402915836744ba76aef531bb25c485bd05ea814406ca90ee916 |