funkwhale/api/config/schema.py

63 wiersze
2.2 KiB
Python

import os
2022-07-15 18:14:19 +00:00
from drf_spectacular.contrib.django_oauth_toolkit import OpenApiAuthenticationExtension
2022-07-20 12:31:57 +00:00
from drf_spectacular.plumbing import build_bearer_security_scheme_object
2022-07-15 18:14:19 +00:00
2022-07-16 07:21:33 +00:00
2022-07-15 18:14:19 +00:00
class CustomOAuthExt(OpenApiAuthenticationExtension):
target_class = "funkwhale_api.common.authentication.OAuth2Authentication"
name = "oauth2"
def get_security_definition(self, auto_schema):
from drf_spectacular.settings import spectacular_settings
from oauth2_provider.scopes import get_scopes_backend
2022-07-15 18:14:19 +00:00
flows = {}
for flow_type in spectacular_settings.OAUTH2_FLOWS:
flows[flow_type] = {}
if flow_type in ("implicit", "authorizationCode"):
flows[flow_type][
"authorizationUrl"
] = spectacular_settings.OAUTH2_AUTHORIZATION_URL
if flow_type in ("password", "clientCredentials", "authorizationCode"):
flows[flow_type]["tokenUrl"] = spectacular_settings.OAUTH2_TOKEN_URL
if spectacular_settings.OAUTH2_REFRESH_URL:
flows[flow_type]["refreshUrl"] = spectacular_settings.OAUTH2_REFRESH_URL
scope_backend = get_scopes_backend()
flows[flow_type]["scopes"] = scope_backend.get_all_scopes()
return {"type": "oauth2", "flows": flows}
2022-07-16 07:17:12 +00:00
2022-07-16 07:21:33 +00:00
2022-07-20 12:31:57 +00:00
class CustomApplicationTokenExt(OpenApiAuthenticationExtension):
target_class = "funkwhale_api.common.authentication.ApplicationTokenAuthentication"
name = "ApplicationToken"
def get_security_definition(self, auto_schema):
return build_bearer_security_scheme_object(
header_name="Authorization",
token_prefix="Bearer",
)
2022-07-16 07:17:12 +00:00
def custom_preprocessing_hook(endpoints):
filtered = []
2022-09-25 13:57:22 +00:00
2022-07-16 07:17:12 +00:00
# your modifications to the list of operations that are exposed in the schema
2022-07-20 12:31:57 +00:00
api_type = os.environ.get("API_TYPE", "v1")
2022-09-25 13:57:22 +00:00
2023-03-28 17:34:19 +00:00
for path, path_regex, method, callback in endpoints:
2022-07-20 12:31:57 +00:00
if path.startswith("/api/v1/providers"):
continue
2022-09-25 13:57:22 +00:00
if path.startswith("/api/v1/users/users"):
continue
if path.startswith("/api/v1/oauth/authorize"):
continue
2022-07-16 07:17:12 +00:00
if path.startswith(f"/api/{api_type}"):
2022-07-16 07:21:33 +00:00
filtered.append((path, path_regex, method, callback))
2022-09-25 13:57:22 +00:00
2022-07-16 07:17:12 +00:00
return filtered