Projet

Général

Profil

Télécharger (1,9 ko) Statistiques
| Branche: | Tag: | Révision:

mandayejs / mandayejs / mandaye / utils.py @ a9a12993

1
# mandayejs - saml reverse proxy
2
# Copyright (C) 2015  Entr'ouvert
3
#
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License as published
6
# by the Free Software Foundation, either version 3 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU Affero General Public License for more details.
13
#
14
# You should have received a copy of the GNU Affero General Public License
15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
import os
17
import json
18
import subprocess
19
from django.conf import settings
20

    
21
import urlparse
22
from Cookie import SimpleCookie
23

    
24

    
25
def exec_phantom(data):
26
    phantom = subprocess.Popen(['/usr/bin/phantomjs',
27
        '--ignore-ssl-errors=yes',
28
        '--ssl-protocol=any',
29
        '--cookies-file=cookies.txt',
30
        os.path.join(settings.BASE_DIR, 'mandayejs', 'do_login.js')],
31
        close_fds=True,
32
        stdin=subprocess.PIPE,
33
        stdout=subprocess.PIPE)
34
    stdout, stderr = phantom.communicate(json.dumps(data))
35
    result = json.loads(stdout)
36
    return result
37

    
38
def cookie_builder(headers):
39
    """Build Cookies from list of headers
40
    """
41
    cookie = SimpleCookie()
42
    for header in headers:
43
        cookie.load('; '.join(header.values()).encode('ascii'))
44

    
45
    return cookie
46

    
47
def get_location(url):
48
    """Return path
49
    """
50
    url = urlparse.urlparse(url)
51
    url = url._replace(netloc=settings.SITE_DOMAIN)
52
    return url.path
53

    
54
def get_password_field():
55
    """Return name of the password field
56
    """
57
    try:
58
        field_name = [ field.get('name') for field in settings.SITE_LOCATORS if field.get('kind') == 'password' ]
59
        return field_name[0]
60
    except (IndexError,):
61
        return None
62

    
(5-5/6)