h1. SpPythonTutorial h2. Simplest service provider AssertionConsumer ever as a WSGI application The prerequisite for this example is: * you must set the IdP signature and encryption keys in the idp_metata_xml string containing the IdP metadata file, you can replace it completely by the IdP metadata file if you have one, * the AuthnResponse is transmitted using the HTTP-Post binding.
import sys
import lasso
from wsgiref.simple_server import make_server
import logging
import urlparse

logging.basicConfig(level=logging.DEBUG)

sp_metadata_xml = '''

  
  
    
    urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress
  
  
     Example SAML 2.0 metadatas
  
'''

idp_metadata_xml = '''



  
    
      
        
    
        4yalpsp9Sxlsj07PEI8jJxhSJdo4F0iW0H8u1dhwmsW5YQvRUw/yPlmC09q4WjImmnFVNCJarAOYeFgQCxfIoBasKNnUeBQpogo8W0Q/3mCuKl6lNSr/PIuxMVVNPDWmWkhHXJx/MVar2IREKa1P4jHL0Uxl69/idLwc7TtK1h8=
        AQAB
    

      
    
    
      
        
    
        wLu5SdmwyS4o1On/aw4nElLGERFG931exvkzu0ewaM1/oUyD3dO7UC5xMGnPfc6IaH5BcJc3fLr6PJhX55ZrMR98ToPwoUFwuLKK43exwYBEBOOMe1CrCB/Bq+EH6/2sKNXKfgJqj06/3yzafLRiWpMxy2isllxMAvaZXrkpm4c=
        AQAB
    

      
    
  


'''

def app(environ, start_response):
    server = lasso.Server.newFromBuffers(sp_metadata_xml)
    server.addProviderFromBuffer(lasso.PROVIDER_ROLE_IDP, idp_metadata_xml)
    login = lasso.Login(server)
    try:
        data = environ['wsgi.input'].read(int(environ['CONTENT_LENGTH']))
        qs = urlparse.parse_qs(data)
        try:
            login.processAuthnResponseMsg(qs['SAMLResponse'][0])
        except (lasso.DsError, lasso.ProfileCannotVerifySignatureError):
            raise Exception('Invalid signature')
        except lasso.Error:
            raise Exception('Misc error')
        try:
            login.acceptSso()
        except lasso.Error:
            raise Exception('Invalid assertion')
    except Exception, e:
        start_response('500 Internal Error', [('content-type', 'text/plain')],
            sys.exc_info())
        return ['Erreur: ', str(e)]
    else:
        start_response('200 Ok', [('content-type', 'text/plain')], sys.exc_info())
        return ['You are identified as ', login.assertion.subject.nameId.content]

s = make_server('0.0.0.0', 8081, app)
s.serve_forever()