This commit is contained in:
Iliyan Angelov
2025-12-01 06:50:10 +02:00
parent 91f51bc6fe
commit 62c1fe5951
4682 changed files with 544807 additions and 31208 deletions

View File

@@ -0,0 +1,42 @@
from authlib.common.urls import add_params_to_qs
from authlib.common.urls import add_params_to_uri
def add_to_uri(token, uri):
"""Add a Bearer Token to the request URI.
Not recommended, use only if client can't use authorization header or body.
http://www.example.com/path?access_token=h480djs93hd8
"""
return add_params_to_uri(uri, [("access_token", token)])
def add_to_headers(token, headers=None):
"""Add a Bearer Token to the request URI.
Recommended method of passing bearer tokens.
Authorization: Bearer h480djs93hd8
"""
headers = headers or {}
headers["Authorization"] = f"Bearer {token}"
return headers
def add_to_body(token, body=None):
"""Add a Bearer Token to the request body.
access_token=h480djs93hd8
"""
if body is None:
body = ""
return add_params_to_qs(body, [("access_token", token)])
def add_bearer_token(token, uri, headers, body, placement="header"):
if placement in ("uri", "url", "query"):
uri = add_to_uri(token, uri)
elif placement in ("header", "headers"):
headers = add_to_headers(token, headers)
elif placement == "body":
body = add_to_body(token, body)
return uri, headers, body