Based on our first version of soaplib service integration, this second one adds Basic Auth with credentials specified in settings.
It can be tested with django soaplib test cliente
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | import base64
from soaplib.wsgi_soap import SimpleWSGISoapApp
from soaplib.service import soapmethod
from soaplib.serializers import primitive as soap_types
soap_types, soapmethod # make pyflakes happy
from django.conf import settings
from django.http import HttpResponse
def extract_basic_credentials(request):
"""Extact authorization basic credentials from request"""
username, password = None, None
if 'HTTP_AUTHORIZATION' in request.META:
auth = request.META['HTTP_AUTHORIZATION'].split()
if len(auth) == 2:
if auth[0].lower() == "basic":
username, password = base64.b64decode(auth[1]).split(':')
return username, password
class Http401(Exception):
pass
class Http403(Exception):
pass
class DjangoSoapApp(SimpleWSGISoapApp):
"""Adaptor class for handling soaplib webservices."""
def authenticate(self, request):
"""Authenticate webservice requesta."""
ws_username = getattr(settings, 'WS_USERNAME', None)
if ws_username:
ws_password = getattr(settings, 'WS_PASSWORD', None)
username, password = extract_basic_credentials(request)
if username == None:
raise Http401("401 Authentication required.")
if username != ws_username or password != ws_password:
raise Http403("403 You're not authorized to use this webservice!")
def __call__(self, request):
"""Invoke a webservice request."""
self.authenticate(request)
django_resp = HttpResponse()
def start_response(status, headers):
status, reason = status.split(' ', 1)
django_resp.status_code = int(status)
for header, value in headers:
django_resp[header] = value
response = super(SimpleWSGISoapApp, self).__call__(
request.META, start_response)
if django_resp.status_code == 405:
return HttpResponse("405 Method not allowed for this webservice.",
'text/plain', status=405)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.