Skip to content

Commit

Permalink
master: fix authorization, improve error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
gardenerik committed Jan 23, 2024
1 parent fdf2407 commit 2c97ad1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
5 changes: 0 additions & 5 deletions trojstenid/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@
class Application(AbstractApplication):
group = models.ForeignKey(Group, on_delete=models.RESTRICT, blank=True, null=True)

def is_usable(self, request):
if self.group is not None:
return request.user.groups.contains(self.group)
return True


def user_avatar_name(user, filename):
_, ext = path.splitext(filename)
Expand Down
13 changes: 12 additions & 1 deletion trojstenid/users/urls_oauth.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
from django.urls import re_path
from oauth2_provider import urls
from oauth2_provider.views import IntrospectTokenView, RevokeTokenView, TokenView

from trojstenid.users.views import TrojstenAuthorizationView

app_name = urls.app_name

urlpatterns = urls.base_urlpatterns + urls.oidc_urlpatterns
base_urlpatterns = [
re_path(r"^authorize/$", TrojstenAuthorizationView.as_view(), name="authorize"),
re_path(r"^token/$", TokenView.as_view(), name="token"),
re_path(r"^revoke_token/$", RevokeTokenView.as_view(), name="revoke-token"),
re_path(r"^introspect/$", IntrospectTokenView.as_view(), name="introspect"),
]

urlpatterns = base_urlpatterns + urls.oidc_urlpatterns
30 changes: 29 additions & 1 deletion trojstenid/users/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,37 @@
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.shortcuts import redirect
from django.core.exceptions import PermissionDenied
from django.shortcuts import get_object_or_404, redirect, render
from django.views.generic import TemplateView, UpdateView
from oauth2_provider.views import AuthorizationView

from trojstenid.users.forms.settings import ProfileForm
from trojstenid.users.models import Application


class TrojstenAuthorizationView(AuthorizationView):
def dispatch(self, request, *args, **kwargs):
application = get_object_or_404(
Application, client_id=request.GET.get("client_id")
)

if application.group:
if not request.user.is_authenticated:
raise PermissionDenied()

if not request.user.groups.contains(application.group):
return render(
request,
"oauth2_provider/authorize.html",
{
"error": {
"error": "Chýbajúce oprávnenia.",
"description": "Nemáš práva na prístup do tejto aplikácie.",
}
},
)

return super().dispatch(request, *args, **kwargs)


class ProfileView(LoginRequiredMixin, UpdateView):
Expand Down

0 comments on commit 2c97ad1

Please sign in to comment.