-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: server-side changes for authentication
- Loading branch information
Showing
7 changed files
with
91 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import os | ||
from django.contrib.auth.models import User | ||
from django.contrib.sites.models import Site | ||
from django.core.management.base import BaseCommand, CommandError | ||
from oauth2_provider.models import Application | ||
|
||
|
||
class Command(BaseCommand): | ||
help = 'Creates a client Application object for authentication purposes.' | ||
|
||
def handle(self, **kwargs): | ||
uri = os.environ.get('VUE_APP_BASE_URL') | ||
client_id = os.environ.get('VUE_APP_OAUTH_CLIENT_ID') | ||
if uri is None: | ||
raise CommandError('Environment variable VUE_APP_BASE_URL is not set.') | ||
if client_id is None: | ||
raise CommandError('Environment variable VUE_APP_OAUTH_CLIENT_ID is not set.') | ||
|
||
site = Site.objects.get_current() # type: ignore | ||
site.domain = 'uvdat.demo' | ||
site.name = 'UVDAT' | ||
site.save() | ||
|
||
try: | ||
user = User.objects.first() | ||
if Application.objects.filter(user=user).exists(): | ||
raise CommandError( | ||
'The client already exists. You can administer it from the admin console.' | ||
) | ||
application = Application( | ||
user=user, | ||
redirect_uris=uri, | ||
client_id=client_id, | ||
name='client-app', | ||
client_type='public', | ||
authorization_grant_type='authorization-code', | ||
skip_authorization=True, | ||
) | ||
application.save() | ||
self.stdout.write( | ||
self.style.SUCCESS('Client Application created.') | ||
) | ||
except User.DoesNotExist: | ||
raise CommandError( | ||
'A user must exist before creating a client. Use createsuperuser command.' | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import json | ||
from django.http import HttpResponse | ||
from django.contrib.auth.models import User | ||
from django.contrib.auth import logout | ||
from rest_framework.decorators import action | ||
from rest_framework.viewsets import ReadOnlyModelViewSet | ||
|
||
from .serializers import UserSerializer | ||
|
||
|
||
class UserViewSet(ReadOnlyModelViewSet): | ||
queryset = User.objects.all() | ||
serializer_class = UserSerializer | ||
|
||
@action(detail=False, pagination_class=None) | ||
def me(self, request): | ||
"""Return the currently logged in user's information.""" | ||
if request.user.is_anonymous: | ||
return HttpResponse(status=204) | ||
return HttpResponse( | ||
json.dumps(UserSerializer(request.user).data), | ||
status=200 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters