2018-01-11 12:10:27 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2019-05-20 04:30:55 +00:00
|
|
|
import time
|
2022-07-04 03:29:39 +00:00
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
from django.conf import settings
|
2022-07-04 03:29:39 +00:00
|
|
|
from rest_framework import permissions
|
|
|
|
|
2018-01-11 12:10:27 +00:00
|
|
|
|
2023-09-11 03:13:34 +00:00
|
|
|
class IsValidUser(permissions.IsAuthenticated):
|
2018-01-11 12:10:27 +00:00
|
|
|
"""Allows access to valid user, is active and not expired"""
|
|
|
|
|
|
|
|
def has_permission(self, request, view):
|
2022-11-07 12:41:18 +00:00
|
|
|
return super().has_permission(request, view) \
|
2023-02-16 06:27:54 +00:00
|
|
|
and request.user.is_valid
|
2018-01-11 12:10:27 +00:00
|
|
|
|
|
|
|
|
2022-02-17 12:13:31 +00:00
|
|
|
class OnlySuperUser(IsValidUser):
|
2018-07-25 03:21:12 +00:00
|
|
|
def has_permission(self, request, view):
|
2022-02-17 12:13:31 +00:00
|
|
|
return super().has_permission(request, view) \
|
2023-02-16 06:27:54 +00:00
|
|
|
and request.user.is_superuser
|
2018-07-25 03:21:12 +00:00
|
|
|
|
|
|
|
|
2022-11-07 12:41:18 +00:00
|
|
|
class IsServiceAccount(IsValidUser):
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
return super().has_permission(request, view) \
|
2023-02-16 06:27:54 +00:00
|
|
|
and request.user.is_service_account
|
2022-11-07 12:41:18 +00:00
|
|
|
|
|
|
|
|
2018-11-23 02:25:35 +00:00
|
|
|
class WithBootstrapToken(permissions.BasePermission):
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
authorization = request.META.get('HTTP_AUTHORIZATION', '')
|
|
|
|
if not authorization:
|
|
|
|
return False
|
|
|
|
request_bootstrap_token = authorization.split()[-1]
|
|
|
|
return settings.BOOTSTRAP_TOKEN == request_bootstrap_token
|
2019-06-19 02:47:26 +00:00
|
|
|
|
|
|
|
|
2023-09-25 14:58:12 +00:00
|
|
|
class ServiceAccountSignaturePermission(permissions.BasePermission):
|
|
|
|
def has_permission(self, request, view):
|
|
|
|
from authentication.models import AccessKey
|
|
|
|
from common.utils.crypto import get_aes_crypto
|
|
|
|
signature = request.META.get('HTTP_X_JMS_SVC', '')
|
|
|
|
if not signature or not signature.startswith('Sign'):
|
|
|
|
return False
|
|
|
|
data = signature[4:].strip()
|
|
|
|
if not data or ':' not in data:
|
|
|
|
return False
|
|
|
|
ak_id, time_sign = data.split(':', 1)
|
|
|
|
if not ak_id or not time_sign:
|
|
|
|
return False
|
|
|
|
ak = AccessKey.objects.filter(id=ak_id).first()
|
|
|
|
if not ak or not ak.is_active:
|
|
|
|
return False
|
|
|
|
if not ak.user or not ak.user.is_active or not ak.user.is_service_account:
|
|
|
|
return False
|
|
|
|
aes = get_aes_crypto(str(ak.secret).replace('-', ''), mode='ECB')
|
|
|
|
try:
|
|
|
|
timestamp = aes.decrypt(time_sign)
|
|
|
|
if not timestamp or not timestamp.isdigit():
|
|
|
|
return False
|
|
|
|
timestamp = int(timestamp)
|
|
|
|
interval = abs(int(time.time()) - timestamp)
|
|
|
|
if interval > 30:
|
|
|
|
return False
|
|
|
|
return True
|
|
|
|
except Exception:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def has_object_permission(self, request, view, obj):
|
|
|
|
return False
|