Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

トークンによる認証を追加 #49

Merged
merged 2 commits into from
Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions api/accounting/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import django_filters
from django.db.models import Q
from django.shortcuts import render
from rest_framework import filters, permissions, viewsets
from rest_framework import permissions, viewsets, authtoken

from utils import perm_method
from web import settings
Expand All @@ -11,9 +9,13 @@
ProjectSerializer, PurchaseSerializer, CreateProjectApprovalSerializer, CreatePurchaseSerializer)


def is_token_auth(request):
return isinstance(request.auth, authtoken.models.Token)


class AdminPermission(permissions.BasePermission):
def has_permission(self, request, view):
return request.user and request.user.is_superuser
return request.user and request.user.is_superuser and not is_token_auth(request)


class ReadOnlyPermission(permissions.BasePermission):
Expand All @@ -23,6 +25,9 @@ def has_permission(self, request, view):

class ProjectOwnerPermission(permissions.BasePermission):
def has_permission(self, request, view):
if is_token_auth(request):
return False

if 'pk' not in view.kwargs:
return False

Expand All @@ -48,10 +53,16 @@ def has_permission(self, request, view):
if request.method not in {'POST', 'PATCH', 'GET'}:
return False

if is_token_auth(request) and request.method != 'GET':
return False

return True

class ProjectApprovalRequestPermission(permissions.BasePermission):
def has_permission(self, request, view):
if is_token_auth(request):
return False

if not request.user:
return False

Expand All @@ -64,6 +75,10 @@ class PurchaseOwnerPermission(permissions.BasePermission):
def has_permission(self, request, view):
if not request.user or not request.user.is_authenticated:
return False

if is_token_auth(request):
return False

return True


Expand Down Expand Up @@ -91,6 +106,9 @@ def has_object_permission(self, request, view, obj):
if obj.closed:
return False

if is_token_auth(request):
return False

return True


Expand All @@ -103,6 +121,9 @@ def has_permission(self, request, view):
for perm_class in allowed_perm_classes)

def has_object_permission(self, request, view, obj):
if is_token_auth(request):
return False

if request.method == 'PATCH':
if request.user.is_superuser:
return True
Expand Down
14 changes: 6 additions & 8 deletions api/storaging/views.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
import datetime
import logging
import uuid
import pytz

import boto3

from rest_framework import permissions, views
from rest_framework import permissions, authtoken
from rest_framework.response import Response
from rest_framework.generics import (
CreateAPIView,
)

import constants
from web import settings

from .models import Medium
from .serializer import MediumSerializer


def is_token_auth(request):
return isinstance(request.auth, authtoken.models.Token)


class AdminPermission(permissions.BasePermission):
def has_permission(self, request, view):
return request.user and request.user.is_superuser
return request.user and request.user.is_superuser and not is_token_auth(request)


class GeneralPermission(permissions.BasePermission):
Expand Down
2 changes: 2 additions & 0 deletions api/web/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
'authenticate',
'storaging',
'rest_framework',
'rest_framework.authtoken',
'drf_yasg',
'web',
'corsheaders'
Expand Down Expand Up @@ -85,6 +86,7 @@
'DEFAULT_PERMISSION_CLASSES': ('rest_framework.permissions.IsAuthenticated', ),
'DEFAULT_AUTHENTICATION_CLASSES': [
'web.authenticate.CsrfExemptSessionAuthentication',
'rest_framework.authentication.TokenAuthentication',
],
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination'
}
Expand Down