def http_basic_auth(func):
@wraps(func)
def _decorator(request, *args, **kwargs):
from django.contrib.auth import authenticate, login
if "HTTP_AUTHORIZATION" in request.META:
authmeth, auth = request.META["HTTP_AUTHORIZATION"].split(b" ", 1)
if authmeth.lower() == b"basic":
auth = codecs.decode(auth.strip(), "base64")
username, password = auth.split(b":", 1)
user = authenticate(username=username, password=password)
if user and is_authorized(user):
login(request, user)
else:
raise PermissionDenied()
return func(request, *args, **kwargs)
return _decorator
评论列表
文章目录