2018-11-11 12:41:48 +00:00
|
|
|
"""Dispatch OAuth views to respective views"""
|
|
|
|
from django.http import Http404
|
|
|
|
from django.shortcuts import get_object_or_404
|
|
|
|
from django.views import View
|
2020-05-19 19:53:36 +00:00
|
|
|
from structlog import get_logger
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2019-10-07 14:33:48 +00:00
|
|
|
from passbook.sources.oauth.models import OAuthSource
|
|
|
|
from passbook.sources.oauth.types.manager import MANAGER, RequestKind
|
2018-11-11 12:41:48 +00:00
|
|
|
|
2020-05-19 19:53:36 +00:00
|
|
|
LOGGER = get_logger()
|
|
|
|
|
2018-11-11 12:41:48 +00:00
|
|
|
|
|
|
|
class DispatcherView(View):
|
|
|
|
"""Dispatch OAuth Redirect/Callback views to their proper class based on URL parameters"""
|
|
|
|
|
2019-12-31 11:51:16 +00:00
|
|
|
kind = ""
|
2018-11-11 12:41:48 +00:00
|
|
|
|
|
|
|
def dispatch(self, *args, **kwargs):
|
|
|
|
"""Find Source by slug and forward request"""
|
2019-12-31 11:51:16 +00:00
|
|
|
slug = kwargs.get("source_slug", None)
|
2018-11-11 12:41:48 +00:00
|
|
|
if not slug:
|
|
|
|
raise Http404
|
|
|
|
source = get_object_or_404(OAuthSource, slug=slug)
|
|
|
|
view = MANAGER.find(source, kind=RequestKind(self.kind))
|
2020-05-19 19:53:36 +00:00
|
|
|
LOGGER.debug("dispatching OAuth2 request to", view=view, kind=self.kind)
|
2018-11-11 12:41:48 +00:00
|
|
|
return view.as_view()(*args, **kwargs)
|