53 lines
1.9 KiB
Python
53 lines
1.9 KiB
Python
"""
|
|
Dispatcher for basic auth form authentifications
|
|
"""
|
|
import Cookie
|
|
import httplib
|
|
import urllib
|
|
|
|
|
|
from mandaye.dispatchers.default import Dispatcher
|
|
from mandaye.response import _502, _302, _401
|
|
|
|
class AuthFormDispatcher(Dispatcher):
|
|
|
|
def __init__(self, target_url, mapping=None):
|
|
super(AuthFormDispatcher, self).__init__(target_url, mapping)
|
|
self.local_auth = None
|
|
self.dest_auth = None
|
|
|
|
def login(self, values, request):
|
|
""" Automatic login on a site with a form
|
|
"""
|
|
login = self.local_auth.get_current_login()
|
|
if not login:
|
|
return _401('Invalid login')
|
|
if not values.has_key('form_action') \
|
|
or not values.has_key('form_values') \
|
|
or not values.has_key('username_field') \
|
|
or not values.has_key('password_field'):
|
|
logging.warning('Invalid values for AuthFormDispatcher.login')
|
|
return _502
|
|
headers = values.get('form_headers')
|
|
if not headers:
|
|
headers = { 'Content-Type': 'application/x-www-form-urlencoded' }
|
|
params = values['form_values']
|
|
# TODO: find the login / password in the destaut or redirect
|
|
params[values['username_field']] = '754443'
|
|
params[values['password_field']] = 'lecteur'
|
|
params = urllib.urlencode(params)
|
|
conn = httplib.HTTPConnection(self.target.hostname)
|
|
conn.request("POST", values.get('form_action'), params, headers)
|
|
res = conn.getresponse()
|
|
cookies = Cookie.BaseCookie(res.msg.getheader('set-cookie'))
|
|
cookies = self.filter.response_cookies(cookies)
|
|
location = res.msg.getheader('location')
|
|
if location:
|
|
location = location.replace(self.target.hostname, self.env["HTTP_HOST"])
|
|
conn.close()
|
|
# TODO: Test if he is login or not
|
|
return _302(location, cookies)
|
|
|
|
def connection(self, values, response):
|
|
pass
|