Пример #1
0
    def login(self, environ, start_response):
        req = Request(environ)
        set_template(environ, 'login.xslt')
        sess = environ['beaker.session']
        params = {'base_uri': self.base_url}
        if not sess.get(self.ekey):
            sess[self.ekey] = {}
            sess.save()

        headers = [('Content-Type', 'application/xml')]
        if not (req.params.get('uname') or req.params.get('return_location')):
            message ='There must be a uname and return_location in the query string'
            raise HTTPBadRequest(detail=message)
        
        openid_url = req.params['uname']
        sess[self.ekey]['return_location'] = req.params['return_location']

        if not openid_url:
            # this seems better ...
            # raise HTTPBadRequest("need openid_url")
            params['message'] = "Don't leave your name blank."
            params['status'] = 'failure'
            set_params(environ, params)
            start_response('200 OK', headers)
            return []

        consumer = self.get_consumer(sess[self.ekey])
        try:
            request = consumer.begin(openid_url)
        except Exception, exc:
            params['message'] = 'Error in discovery: %s' % (cgi.escape(str(exc[0])))
            params['status'] = 'failure'
            set_params(environ, params)
            start_response('200 OK', headers)
            return []
Пример #2
0
    def complete(self, environ, start_response):
        req = Request(environ)
        sess = environ['beaker.session']
        set_template(environ, 'login.xslt')
        # params = {'base_uri': self.base_url}
        params = {'base_uri': 'http://dev.amp.fm/'}

        headers = [('Content-Type', 'application/xml')]

        consumer = self.get_consumer(sess[self.ekey])
        info = consumer.complete(req.GET)

        cookies = []
        if info.status == 'success':
            req.cookies['openid'] = req.params['identity']
            guid = req.cookies.get('guid', str(uuid.uuid1()))
            headers.append(make_cookie_header('openid.session', guid, expires=0))
            headers.append(make_cookie_header('openid', req.params['identity']))
            params['status'] = 'complete'
            params['return_location'] = sess[self.ekey]['return_location']
            params['message'] = 'Logged in as %s' % req.params['identity']
        elif info.status == 'failure':
            # Sylvain: I explicitely remove any existing cookie in case of a failure
            headers.append(make_cookie_header('openid.session', expires=0))
            headers.append(make_cookie_header('openid', expires=0))
            params['status']= 'failure'
            if info.identity_url:
                fmt = "Verification of %s failed: %s"                
                params['message'] = fmt % (cgi.escape(info.identity_url),
                                           info.message)
            else:
                params['message'] = "Verification failed"

        elif info.status == 'cancel':
            params['status'] = 'failure'
            params['message'] = 'Verfication cancelled'

        set_params(environ, params)
        start_response('200 OK', headers)
        return []