Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from functools import wraps
- def basic_auth_required(func):
- @wraps(func)
- def _decorator(request, *args, **kwargs):
- from django.conf import settings
- from django.core.exceptions import PermissionDenied
- from django.http import HttpResponse
- from newjazzwebservice import NewJazzConnector
- import base64, binascii
- HTTP_HEADER_ENCODING = 'iso-8859-1'
- if request.META.has_key('HTTP_AUTHORIZATION'):
- auth = request.META['HTTP_AUTHORIZATION'].split()
- if not auth or auth[0].lower() != b'basic':
- return None
- if len(auth) == 1:
- # 'Invalid basic header. No credentials provided.'
- raise PermissionDenied
- elif len(auth) > 2:
- # 'Invalid basic header. Credentials string should not contain spaces.'
- raise PermissionDenied
- try:
- auth_parts = base64.b64decode(auth[1]).decode(HTTP_HEADER_ENCODING).partition(':')
- except (TypeError, UnicodeDecodeError, binascii.Error):
- # 'Invalid basic header. Credentials not correctly base64 encoded.'
- raise PermissionDenied
- userid, password = auth_parts[0], auth_parts[2]
- nwjz = NewJazzConnector(userid, password, settings.ENTORNO_NJ)
- valid_user = nwjz.authenticate()
- if valid_user:
- return func(request, *args, **kwargs)
- else:
- return PermissionDenied
- res = HttpResponse()
- res.status_code = 401
- res['WWW-Authenticate'] = 'Basic'
- return res
- return _decorator
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement