Projet

Général

Profil

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

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

1
import re
2

    
3
from quixote import get_request, get_response, get_session
4

    
5
from qommon.misc import http_post_request
6
from qommon import get_logger
7

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

    
11
class ConcertoSiteAuthentication(SiteAuthentication):
12
    plugin_name = 'concerto'
13

    
14
    def auto_detect_site(cls, html_doc):
15
        if re.search(
16
                """<meta name="description" content="Page d'accueil du site Espace-Famille" />""",
17
                html_doc):
18
            return True
19
        return False
20
    auto_detect_site = classmethod(auto_detect_site)
21

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

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

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

    
42
        # Add session id cookie
43
        if session_cookies is True:
44
            for key, value in self.host.other_fields.iteritems():
45
                headers['Cookie'] = 'JSESSIONID=' + value
46

    
47
        # Send request
48
        response, status, data, auth_headers = http_post_request(
49
            url, body, headers, self.host.use_proxy)
50

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

    
80
        if session_cookies is False:
81
            # Change idSession hidden field with new session id
82
            self.host.other_fields['idSession'] = new_session_id
83
            # Retry the request with the new session id
84
            return self.local_auth_check_post(username, password, select, session_cookies=True)
85
        else:
86
            return response.status, data
87

    
88
site_authentication_plugins.register(ConcertoSiteAuthentication)
89

    
(4-4/6)