Skip to content

Instantly share code, notes, and snippets.

@archatas
Created Apr 16, 2022
Embed
What would you like to do?
oauth2_provider_adjustments
from django.urls import re_path
from oauth2_provider import views as oauth2_provider_views
from . import views
app_name = "oauth2_provider_adjustments"
urlpatterns = [
# Base
re_path(r"^authorize/$", oauth2_provider_views.AuthorizationView.as_view(), name="authorize"),
re_path(r"^token/$", oauth2_provider_views.TokenView.as_view(), name="token"),
re_path(r"^revoke_token/$", oauth2_provider_views.RevokeTokenView.as_view(), name="revoke-token"),
re_path(r"^introspect/$", oauth2_provider_views.IntrospectTokenView.as_view(), name="introspect"),
# Application management views
re_path(r"^applications/$", views.ApplicationList.as_view(), name="list"),
re_path(r"^applications/register/$", views.ApplicationRegistration.as_view(), name="register"),
re_path(r"^applications/(?P<pk>[\w-]+)/$", views.ApplicationDetail.as_view(), name="detail"),
re_path(r"^applications/(?P<pk>[\w-]+)/delete/$", views.ApplicationDelete.as_view(), name="delete"),
re_path(r"^applications/(?P<pk>[\w-]+)/update/$", views.ApplicationUpdate.as_view(), name="update"),
# Token management views
re_path(r"^authorized_tokens/$", oauth2_provider_views.AuthorizedTokensListView.as_view(), name="authorized-token-list"),
re_path(
r"^authorized_tokens/(?P<pk>[\w-]+)/delete/$",
oauth2_provider_views.AuthorizedTokenDeleteView.as_view(),
name="authorized-token-delete",
),
]
from django.contrib.auth.mixins import AccessMixin
from django.forms.models import modelform_factory
from django.urls import reverse_lazy
from django.views.generic import (
CreateView,
DeleteView,
DetailView,
ListView,
UpdateView,
)
from oauth2_provider.models import get_application_model
class SuperUserOnlyMixin(AccessMixin):
def dispatch(self, request, *args, **kwargs):
if not request.user.is_superuser:
return self.handle_no_permission()
return super().dispatch(request, *args, **kwargs)
class ApplicationOwnerIsUserMixin(SuperUserOnlyMixin):
"""
This mixin is used to provide an Application queryset filtered by the current request.user.
"""
fields = "__all__"
def get_queryset(self):
return get_application_model().objects.filter(user=self.request.user)
class ApplicationRegistration(SuperUserOnlyMixin, CreateView):
"""
View used to register a new Application for the request.user
"""
template_name = "oauth2_provider/application_registration_form.html"
def get_form_class(self):
"""
Returns the form class for the application model
"""
return modelform_factory(
get_application_model(),
fields=(
"name",
"client_id",
"client_secret",
"client_type",
"authorization_grant_type",
"redirect_uris",
"algorithm",
),
)
def form_valid(self, form):
form.instance.user = self.request.user
return super().form_valid(form)
class ApplicationDetail(ApplicationOwnerIsUserMixin, DetailView):
"""
Detail view for an application instance owned by the request.user
"""
context_object_name = "application"
template_name = "oauth2_provider/application_detail.html"
class ApplicationList(ApplicationOwnerIsUserMixin, ListView):
"""
List view for all the applications owned by the request.user
"""
context_object_name = "applications"
template_name = "oauth2_provider/application_list.html"
class ApplicationDelete(ApplicationOwnerIsUserMixin, DeleteView):
"""
View used to delete an application owned by the request.user
"""
context_object_name = "application"
success_url = reverse_lazy("oauth2_provider:list")
template_name = "oauth2_provider/application_confirm_delete.html"
class ApplicationUpdate(ApplicationOwnerIsUserMixin, UpdateView):
"""
View used to update an application owned by the request.user
"""
context_object_name = "application"
template_name = "oauth2_provider/application_form.html"
def get_form_class(self):
"""
Returns the form class for the application model
"""
return modelform_factory(
get_application_model(),
fields=(
"name",
"client_id",
"client_secret",
"client_type",
"authorization_grant_type",
"redirect_uris",
"algorithm",
),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment