This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
authentic2-idp-oauth2/authentic2_idp_oauth2/views.py

40 lines
1.4 KiB
Python

from django.conf import settings
from rest_framework.decorators import (api_view, authentication_classes,
permission_classes)
from rest_framework.authentication import (OAuth2Authentication,
SessionAuthentication)
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from provider.oauth2.views import Authorize
from provider import scope
from . import forms, app_settings
@api_view(['GET'])
@authentication_classes([OAuth2Authentication, SessionAuthentication])
@permission_classes([IsAuthenticated])
def user_info(request):
user = request.user
return Response({
'username': user.username,
'first_name': user.first_name,
'last_name': user.last_name,
'email': user.email,
'display_name': user.get_full_name(),
'role': user.groups.values_list('name', flat=True),
})
class Authorize(Authorize):
def get_authorization_form(self, request, client, data, client_data):
for url_prefix, scopes in app_settings.AUTOMATIC_GRANT:
if client.url.startswith(url_prefix) and \
scope.check(client_data['scope'], scope.to_int(*scopes)):
# return an always valid form
return forms.EmptyForm({}, scope=client_data['scope'])
return super(Authorize, self).get_authorization_form(
request, client, data, client_data)