Projet

Général

Profil

Télécharger (3,71 ko) Statistiques
| Branche: | Révision:

root / larpe / tags / release-1.1.1 / larpe / plugins / site_authentication / egroupware.py @ d03cb81c

1
import re
2
import urlparse
3

    
4
from quixote import get_request, get_response, get_session
5

    
6
from qommon.misc import http_post_request, http_get_page
7
from qommon import get_logger
8

    
9
from larpe.plugins import site_authentication_plugins
10
from larpe.site_authentication import SiteAuthentication
11

    
12
class EgroupwareSiteAuthentication(SiteAuthentication):
13
    plugin_name = 'egroupware'
14

    
15
    def auto_detect_site(cls, html_doc):
16
        if re.search("""<meta name="description" content="eGroupWare" />""", html_doc):
17
            return True
18
        return False
19
    auto_detect_site = classmethod(auto_detect_site)
20

    
21
    def local_auth_check_post(self, username, password, select=None):
22
        select = select or {}
23
        url = self.host.auth_check_url
24

    
25
        # Build request body
26
        body = '%s=%s&%s=%s' % (
27
            self.host.login_field_name, username, self.host.password_field_name, password)
28
        # Add select fields to the body
29
        for name, value in select.iteritems():
30
            body += '&%s=%s' % (name, value)
31
        # Add hidden fields to the body
32
        if self.host.send_hidden_fields:
33
            for key, value in self.host.other_fields.iteritems():
34
                body += '&%s=%s' % (key, value)
35

    
36
        # Build request HTTP headers
37
        headers = {'Content-Type': 'application/x-www-form-urlencoded',
38
                   'X-Forwarded-For': get_request().get_environ('REMOTE_ADDR', '-'),
39
                   'X-Forwarded-Host': self.host.reversed_hostname}
40

    
41
        # Send request
42
        response, status, data, auth_headers = http_post_request(
43
            url, body, headers, self.host.use_proxy)
44

    
45
        # The specific code is these 2 lines and the called function
46
        if self.host.name.startswith('egroupware'):
47
            data = self.get_data_after_redirects(response, data)
48

    
49
        cookies = response.getheader('Set-Cookie', None)
50
        self.host.cookies = []
51
        if cookies is not None:
52
            cookies_list = []
53
            cookies_set_list = []
54
            for cookie in cookies.split(', '):
55
                # Drop the path and other attributes
56
                cookie_only = cookie.split('; ')[0]
57
                regexp = re.compile('=')
58
                if regexp.search(cookie_only) is None:
59
                    continue
60
                # Split name and value
61
                cookie_split = cookie_only.split('=')
62
                cookie_name = cookie_split[0]
63
                cookie_value = cookie_split[1]
64
                cookies_list.append('%s=%s' % (cookie_name, cookie_value))
65
                set_cookie = '%s=%s; path=/' % (cookie_name, cookie_value)
66
                cookies_set_list.append(set_cookie)
67
                self.host.cookies.append(cookie_name)
68
            cookies_headers = '\r\nSet-Cookie: '.join(cookies_set_list)
69
            get_response().set_header('Set-Cookie', cookies_headers)
70
            self.host.store()
71
            get_session().cookies = '; '.join(cookies_list)
72
        else:
73
            get_logger().warn('No cookie from local authentication')
74

    
75
        return response.status, data
76

    
77
    def get_data_after_redirects(self, response, data):
78
        status = response.status
79
        headers = {'X-Forwarded-For': get_request().get_environ('REMOTE_ADDR', '-'),
80
                   'X-Forwarded-Host': self.host.reversed_hostname}
81
        while status == 302:
82
            location = response.getheader('Location', None)
83
            if location is not None:
84
                url_tokens = urlparse.urlparse(self.host.auth_check_url)
85
                url = '%s://%s%s'% (url_tokens[0], url_tokens[1], location)
86
                response, status, data, auth_headers = http_get_page(
87
                    url, headers, self.host.use_proxy)
88
        return data
89

    
90
site_authentication_plugins.register(EgroupwareSiteAuthentication)
91

    
(5-5/6)