api.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. from typing import Optional
  2. from allauth.socialaccount.models import SocialApp
  3. from allauth.socialaccount.providers.microsoft.views import MicrosoftGraphOAuth2Adapter
  4. from allauth.socialaccount.providers.openid_connect.views import OpenIDConnectAdapter
  5. from asgiref.sync import sync_to_async
  6. from django.conf import settings
  7. from django.http import HttpRequest
  8. from ninja import ModelSchema, NinjaAPI
  9. from glitchtip.constants import SOCIAL_ADAPTER_MAP
  10. from users.utils import ais_user_registration_open
  11. from .authentication import django_auth
  12. from .exceptions import ThrottleException
  13. from .parsers import EnvelopeParser
  14. from .schema import CamelSchema
  15. try:
  16. from djstripe.settings import djstripe_settings
  17. except ImportError:
  18. pass
  19. api = NinjaAPI(
  20. parser=EnvelopeParser(),
  21. title="GlitchTip API",
  22. urls_namespace="api",
  23. auth=django_auth,
  24. )
  25. if settings.GLITCHTIP_ENABLE_NEW_ISSUES:
  26. from apps.event_ingest.api import router as event_ingest_router
  27. from apps.issue_events.api import router as issue_events_router
  28. api.add_router("v2", event_ingest_router)
  29. api.add_router("v2", issue_events_router)
  30. @api.exception_handler(ThrottleException)
  31. def throttled(request: HttpRequest, exc: ThrottleException):
  32. response = api.create_response(
  33. request,
  34. {"message": "Please retry later"},
  35. status=429,
  36. )
  37. if retry_after := exc.retry_after:
  38. if isinstance(retry_after, int):
  39. response["Retry-After"] = retry_after
  40. else:
  41. response["Retry-After"] = retry_after.strftime("%a, %d %b %Y %H:%M:%S GMT")
  42. return response
  43. class SocialAppSchema(ModelSchema):
  44. scopes: list[str]
  45. authorize_url: Optional[str]
  46. class Config:
  47. model = SocialApp
  48. model_fields = ["name", "client_id", "provider"]
  49. class SettingsOut(CamelSchema):
  50. social_apps: list[SocialAppSchema]
  51. billing_enabled: bool
  52. i_paid_for_glitchtip: bool
  53. enable_user_registration: bool
  54. enable_organization_creation: bool
  55. stripe_public_key: Optional[str]
  56. plausible_url: Optional[str]
  57. plausible_domain: Optional[str]
  58. chatwoot_website_token: Optional[str]
  59. sentryDSN: Optional[str]
  60. sentry_traces_sample_rate: Optional[float]
  61. environment: Optional[str]
  62. version: str
  63. server_time_zone: str
  64. @api.get("settings/", response=SettingsOut, by_alias=True, auth=None)
  65. async def get_settings(request: HttpRequest):
  66. social_apps: list[SocialApp] = []
  67. async for social_app in SocialApp.objects.order_by("name"):
  68. provider = social_app.get_provider(request)
  69. social_app.scopes = provider.get_scope(request)
  70. adapter_cls = SOCIAL_ADAPTER_MAP.get(social_app.provider)
  71. if adapter_cls == OpenIDConnectAdapter:
  72. adapter = adapter_cls(request, social_app.provider_id)
  73. elif adapter_cls:
  74. adapter = adapter_cls(request)
  75. else:
  76. adapter = None
  77. if adapter:
  78. if isinstance(adapter, MicrosoftGraphOAuth2Adapter):
  79. social_app.authorize_url = await sync_to_async(
  80. adapter._build_tenant_url
  81. )("/oauth2/v2.0/authorize")
  82. else:
  83. social_app.authorize_url = adapter.authorize_url
  84. social_app.provider = social_app.provider_id or social_app.provider
  85. social_apps.append(social_app)
  86. billing_enabled = settings.BILLING_ENABLED
  87. return {
  88. "social_apps": social_apps,
  89. "billing_enabled": billing_enabled,
  90. "i_paid_for_glitchtip": settings.I_PAID_FOR_GLITCHTIP,
  91. "enable_user_registration": await ais_user_registration_open(),
  92. "enable_organization_creation": settings.ENABLE_ORGANIZATION_CREATION,
  93. "stripe_public_key": djstripe_settings.STRIPE_PUBLIC_KEY
  94. if billing_enabled
  95. else None,
  96. "plausible_url": settings.PLAUSIBLE_URL,
  97. "plausible_domain": settings.PLAUSIBLE_DOMAIN,
  98. "chatwoot_website_token": settings.CHATWOOT_WEBSITE_TOKEN,
  99. "sentryDSN": settings.SENTRY_FRONTEND_DSN,
  100. "sentry_traces_sample_rate": settings.SENTRY_TRACES_SAMPLE_RATE,
  101. "environment": settings.ENVIRONMENT,
  102. "version": settings.GLITCHTIP_VERSION,
  103. "server_time_zone": settings.TIME_ZONE,
  104. }