-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathdecorators.py
More file actions
39 lines (34 loc) · 1.46 KB
/
Copy pathdecorators.py
File metadata and controls
39 lines (34 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
from functools import wraps
from django.http import HttpResponse
from django.contrib.auth import authenticate, login
from django.conf import settings
from ping.defaults import PING_BASIC_AUTH
def http_basic_auth(func):
"""
Attempts to login user with u/p provided in HTTP_AUTHORIZATION header.
If successful, returns the view, otherwise returns a 401.
If PING_BASIC_AUTH is False, then just return the view function
Modified code by:
http://djangosnippets.org/users/bthomas/
from
http://djangosnippets.org/snippets/1304/
"""
@wraps(func)
def _decorator(request, *args, **kwargs):
if getattr(settings, 'PING_BASIC_AUTH', PING_BASIC_AUTH):
if 'HTTP_AUTHORIZATION' in request.META:
authmeth, auth = request.META['HTTP_AUTHORIZATION'].split(' ', 1)
if authmeth.lower() == 'basic':
auth = auth.strip().decode('base64')
username, password = auth.split(':', 1)
user = authenticate(username=username, password=password)
if user:
login(request, user)
return func(request, *args, **kwargs)
else:
return HttpResponse("Invalid Credentials", status=401)
else:
return HttpResponse("No Credentials Provided", status=401)
else:
return func(request, *args, **kwargs)
return _decorator