from httplib import HTTPConnection from urlparse import urlparse, urlunparse from SOAPpy import WSDL import amara from django.contrib.auth.models import User AUTH_SERVER_NAME='ip or name' AUTH_SERVER_PORT=port AUTH_APP_NAME='app_name' AUTH_APP_PASSWD='app_pass' class CrowdBackEnd(): def authenticate(self, username=None, password=None): user_token = "" try: user_token = self.authUser(user_name=username,user_password=password) except AttributeError: return None try: user = User.objects.get(username__exact=username) except: temp_pass = User.objects.make_random_password(8) user = User.objects.create_user(username, username + '@nothing.net',temp_pass) user.is_staff = False user.save() # Success. return user def get_user(self, user_id): try: return User.objects.get(pk=user_id) except User.DoesNotExist: return None def connectServer(self, body=None): conn = HTTPConnection(AUTH_SERVER_NAME, AUTH_SERVER_PORT) conn.connect() conn.putrequest('POST', '/crowd/services/SecurityServer') conn.putheader('Content-Length', str(len(body))) conn.putheader('Content-type', 'text/xml; charset="utf-8"') conn.putheader('SOAPAction', 'urn:SecurityServer') conn.endheaders() conn.send(body) return conn.getresponse().read() def authApplication(self): body = u'' body = body + u'' body = body + u'' body = body + u'' body = body + u'' body = body + u''+ AUTH_APP_PASSWD +u'' body = body + u'' body = body + u''+ AUTH_APP_NAME +u'' body = body + u'' body = body + u'' body = body + u'' body = body + u'' body = body + u'' doc = amara.parse(self.connectServer(body)) return doc.Envelope.Body.authenticateApplicationResponse.out.token.__str__() def findAllGroups(self): body = u'' body = body + u'' body = body + u'' body = body + u'' body = body + u''+ AUTH_APP_NAME +u'' body = body + u''+ self.authApplication() +u'' body = body + u'' body = body + u'' body = body + u'' body = body + u'' doc = amara.parse(self.connectServer(body)) s = [] for element in doc.Envelope.Body.findAllPrincipalNamesResponse.out.string: s.append(element.__str__()) return s def authUser(self,user_name=None,user_password=None): body = u'' body = body + u'' body = body + u'' body = body + u'' body = body + u''+ AUTH_APP_NAME +u'' body = body + u''+ self.authApplication() +u'' body = body + u'' body = body + u'' body = body + u''+ AUTH_APP_NAME +u'' body = body + u'' body = body + u''+ user_password +u'' body = body + u'' body = body + u''+ user_name +u'' body = body + u'' body = body + u'' body = body + u'' body = body + u'' body = body + u'' doc = amara.parse(self.connectServer(body)) return doc.Envelope.Body.authenticatePrincipalResponse.out.__str__()