-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from observatorycontrolsystem/update_model
Update model
- Loading branch information
Showing
15 changed files
with
224 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[run] | ||
source = ocs_authentication | ||
|
||
[report] | ||
omit = | ||
*/migrations/* | ||
*/management/* | ||
*settings.py | ||
*wsgi.py | ||
*apps.py |
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
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
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,8 @@ | ||
from django.conf.urls import url | ||
|
||
from ocs_authentication.auth_profile.views import AddUpdateUserView | ||
|
||
|
||
urlpatterns = [ | ||
url(r'^addupdateuser/$', AddUpdateUserView.as_view(), name='add_update_user') | ||
] |
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,35 @@ | ||
import json | ||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
|
||
from ocs_authentication.permissions import IsServer | ||
from ocs_authentication.util import create_or_update_user, Profile | ||
|
||
|
||
class AddUpdateUserView(APIView): | ||
""" | ||
This view is meant to be called by the Oauth Server when a new user account is created. This will create | ||
the corresponding user account within this Oauth client app and give it the same api-token, so the user | ||
can access this application with their api-token without needing to initially login with their password. | ||
This should also be called on token change or on any user info change. | ||
""" | ||
permission_classes = [IsServer] | ||
|
||
def post(self, request): | ||
data = json.loads(request.body.decode('utf-8')) | ||
profile = Profile( | ||
data.get('first_name', ''), | ||
data.get('last_name', ''), | ||
data.get('username', ''), | ||
data.get('email', ''), | ||
data.get('tokens', {}).get('api_token', ''), | ||
data.get('is_staff', False), | ||
data.get('is_superuser', False), | ||
data.get('profile', {}).get('staff_view', False) | ||
) | ||
# The password will not be set here since this only has the profile api info. | ||
# The password will only get set when logging in using username/password auth | ||
# which is forwarded through Oauth. | ||
create_or_update_user(profile, password=None) | ||
return Response({'message': 'User account updated'}, status=status.HTTP_200_OK) |
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,31 @@ | ||
|
||
from rest_framework import permissions | ||
from django.conf import settings | ||
|
||
|
||
class IsAdminOrReadOnly(permissions.BasePermission): | ||
"""The request is either read-only, or the user is staff""" | ||
def has_permission(self, request, view): | ||
return bool( | ||
request.method in permissions.SAFE_METHODS | ||
or request.user and request.user.is_staff | ||
) | ||
|
||
|
||
class IsServer(permissions.BasePermission): | ||
message = 'Invalid or missing API Key.' | ||
|
||
def has_permission(self, request, view): | ||
authorization = request.META.get("HTTP_AUTHORIZATION") | ||
|
||
key = '' | ||
if authorization: | ||
try: | ||
_, key = authorization.split("Server ") | ||
except ValueError: | ||
pass | ||
|
||
if key: | ||
return key == settings.OCS_AUTHENTICATION['OAUTH_SERVER_KEY'] | ||
|
||
return False |
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
Oops, something went wrong.