Login

Atlassian Crowd basic Integration

Author:
dgarciaolvera
Posted:
July 16, 2008
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

Is a very simple atlassian crowd integration, I know the client soap requests are not so elegant but they work perfecly! you need to install amara ..

  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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
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'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
        body = body + u'<soap:Body>'
        body = body + u'<authenticateApplication xmlns="urn:SecurityServer">'
        body = body + u'<in0>'
        body = body + u'<credential xmlns="http://authentication.integration.crowd.atlassian.com">'
        body = body + u'<credential>'+ AUTH_APP_PASSWD +u'</credential>'
        body = body + u'</credential>'
        body = body + u'<name xmlns="http://authentication.integration.crowd.atlassian.com">'+ AUTH_APP_NAME +u'</name>'
        body = body + u'<validationFactors xmlns="http://authentication.integration.crowd.atlassian.com" xsi:nil="true" />'
        body = body + u'</in0>'
        body = body + u'</authenticateApplication>'
        body = body + u'</soap:Body>'
        body = body + u'</soap:Envelope>'
        doc = amara.parse(self.connectServer(body))
        return doc.Envelope.Body.authenticateApplicationResponse.out.token.__str__() 

    def findAllGroups(self):
        body = u'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
        body =  body + u'<soap:Body>'
        body =  body + u'<findAllPrincipalNames xmlns="urn:SecurityServer">'
        body =  body + u'<in0>'
        body =  body + u'<name xmlns="http://authentication.integration.crowd.atlassian.com">'+ AUTH_APP_NAME +u'</name>'
        body =  body + u'<token xmlns="http://authentication.integration.crowd.atlassian.com">'+ self.authApplication() +u'</token>'
        body =  body + u'</in0>'
        body =  body + u'</findAllPrincipalNames>'
        body =  body + u'</soap:Body>'
        body =  body + u'</soap:Envelope>'
        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'<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">'
        body =  body + u'<soap:Body>'
        body =  body + u'<authenticatePrincipal xmlns="urn:SecurityServer">'
        body =  body + u'<in0>'
        body =  body + u'<name xmlns="http://authentication.integration.crowd.atlassian.com">'+ AUTH_APP_NAME +u'</name>'
        body =  body + u'<token xmlns="http://authentication.integration.crowd.atlassian.com">'+ self.authApplication() +u'</token>'
        body =  body + u'</in0>'
        body =  body + u'<in1>'
        body =  body + u'<application xmlns="http://authentication.integration.crowd.atlassian.com">'+ AUTH_APP_NAME +u'</application>'
        body =  body + u'<credential xmlns="http://authentication.integration.crowd.atlassian.com">'
        body =  body + u'<credential>'+ user_password +u'</credential>'
        body =  body + u'</credential>'
        body =  body + u'<name xmlns="http://authentication.integration.crowd.atlassian.com">'+ user_name +u'</name>'
        body =  body + u'<validationFactors xmlns="http://authentication.integration.crowd.atlassian.com" />'
        body =  body + u'</in1>'
        body =  body + u'</authenticatePrincipal>'
        body =  body + u'</soap:Body>'
        body =  body + u'</soap:Envelope>'
        doc = amara.parse(self.connectServer(body))
        return doc.Envelope.Body.authenticatePrincipalResponse.out.__str__()    
        

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

sannies (on December 23, 2010):

I created a small django auth backend with SSO middleware. It uses suds instead have hand knitted XML. Have a look at my bitbucket project

#

Please login first before commenting.