Projet

Général

Profil

0001-archimed-user-account-webservice-9517.patch

Josué Kouka, 21 janvier 2016 13:56

Télécharger (9,58 ko)

Voir les différences:

Subject: [PATCH] archimed user account webservice (#9517)

 README                            |  5 +++
 mandayejs/applications.py         |  4 ++
 mandayejs/mandaye/utils.py        | 13 +++++-
 mandayejs/mandaye/views.py        | 11 ++---
 mandayejs/settings.py             |  1 +
 mandayejs/sites/archimed/urls.py  | 23 ++++++++++
 mandayejs/sites/archimed/views.py | 88 +++++++++++++++++++++++++++++++++++++++
 mandayejs/urls.py                 |  6 +++
 8 files changed, 142 insertions(+), 9 deletions(-)
 create mode 100644 mandayejs/sites/archimed/urls.py
 create mode 100644 mandayejs/sites/archimed/views.py
README
147 147
        # if user is connected and already associated
148 148
        SITE_FORCE_REDIRECT_LOCATOR = '#logon-form'
149 149

  
150
        # Application's webservices
151
        SITE_WEB_SERVICES = {
152
            'products': '/products/id',
153
        }
154

  
150 155

  
151 156
About Statics
152 157
-------------
mandayejs/applications.py
137 137
    ]
138 138

  
139 139
    SITE_FORCE_REDIRECT_LOCATOR = '.connectBox'
140

  
141
    SITE_WEB_SERVICES = {
142
        'account_details': '/DEFAULT/Ermes/Services/ILSClient.svc/RetrieveAccount',
143
    }
mandayejs/mandaye/utils.py
26 26
    phantom = subprocess.Popen(['/usr/bin/phantomjs',
27 27
        '--ignore-ssl-errors=yes',
28 28
        '--ssl-protocol=any',
29
        '--cookies-file=cookies.txt',
30 29
        os.path.join(settings.BASE_DIR, 'mandayejs', 'do_login.js')],
31 30
        close_fds=True,
32 31
        stdin=subprocess.PIPE,
......
54 53
    except (IndexError,):
55 54
        return None
56 55

  
56
def get_login_info(request, credentials):
57
    """Returns 
58
    """
59
    app_settings = get_app_settings()
60

  
61
    return {
62
        'address': request.build_absolute_uri(app_settings.SITE_LOGIN_PATH),
63
        'cookies': [],
64
        'locators': [ credentials.to_login_info() ],
65
        'auth_checker': app_settings.SITE_AUTH_CHECKER,
66
        'form_submit_element': app_settings.SITE_FORM_SUBMIT_ELEMENT
67
    }
mandayejs/mandaye/views.py
41 41

  
42 42
from .models import UserCredentials
43 43
from mandayejs.mandaye.forms import FormFactory
44
from mandayejs.mandaye.utils import exec_phantom, cookie_builder
44
from mandayejs.mandaye.utils import exec_phantom, cookie_builder, get_login_info
45 45
from mandayejs.applications import get_app_settings
46 46

  
47 47
app_settings = get_app_settings()
......
140 140
    except (UserCredentials.DoesNotExist,):
141 141
        return HttpResponseRedirect(resolve_url('associate'))
142 142

  
143
    login_info = {
144
        'address': request.build_absolute_uri(app_settings.SITE_LOGIN_PATH),
145
        'cookies': [],
146
        'locators': [ credentials.to_login_info() ],
147
        'auth_checker': getattr(app_settings, 'SITE_AUTH_CHECKER'),
148
        'form_submit_element': getattr(app_settings, 'SITE_FORM_SUBMIT_ELEMENT')
149
    }
143
    login_info = get_login_info(request, credentials)
144

  
150 145
    logger.debug(login_info)
151 146
    login_info['locators'] = [ credentials.to_login_info(decrypt=True)]
152 147
    result = exec_phantom(login_info)
mandayejs/settings.py
54 54
    'django.contrib.sessions',
55 55
    'django.contrib.messages',
56 56
    'django.contrib.staticfiles',
57
    'rest_framework',
57 58
    'mandayejs.mandaye',
58 59
    'mandayejs.sites.duonet',
59 60
    'mandayejs.sites.archimed',
mandayejs/sites/archimed/urls.py
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

  
17
from django.conf.urls import patterns, include, url
18

  
19
from mandayejs.sites.archimed.views import account
20

  
21
urlpatterns = patterns('',
22
    url(r'account/(?P<username>[\w+]*)/$', account, name='archimed-account-detail'),
23
)
mandayejs/sites/archimed/views.py
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

  
17
from __future__ import absolute_import
18

  
19
import logging
20
import requests
21
from pprint import pprint
22
from requests.cookies import RequestsCookieJar
23

  
24
from django.contrib.auth.models import User
25
from django.shortcuts import get_object_or_404
26
from django.core.exceptions import ImproperlyConfigured
27

  
28
from rest_framework import status
29
from rest_framework.views import APIView
30
from rest_framework.response import Response
31

  
32
from mandayejs.mandaye.models import UserCredentials
33
from mandayejs.mandaye.utils import exec_phantom, cookie_builder, get_login_info
34
from mandayejs.applications import get_app_settings
35

  
36

  
37
class AccountDetails(APIView):
38
    """Archimed user's account details
39
    """
40

  
41
    def get(self, request, *args, **kwargs):
42
        logger = logging.getLogger(__name__)
43
        app_settings = get_app_settings()
44
      
45
        try:
46
            ws_uri = app_settings.SITE_WEB_SERVICES['account_details']
47
        except (AttributeError,):
48
            raise ImproperlyConfigured(
49
                    'No SITE_WEB_SERVICES defined in your AppSettings') 
50

  
51
        username = kwargs['username']
52
        user = get_object_or_404(User, username=username)
53
        credentials = get_object_or_404(UserCredentials, user=user)
54

  
55
        login_info = get_login_info(request, credentials)
56
        logger.debug(login_info)
57
        login_info['locators'] = [ credentials.to_login_info(decrypt=True)]
58
        result = exec_phantom(login_info)
59

  
60
        if result.get('result') != 'ok':
61
            return Response(status=status.HTTP_401_UNAUTHORIZED)
62

  
63
        session = requests.session()
64
        r_cookies = RequestsCookieJar()
65

  
66
        for cookie in result.get('cookies'):
67
            r_cookies.set(
68
                cookie['name'],
69
                cookie['value'],
70
                domain=cookie['domain'],
71
                path=cookie['path'],
72
                secure=cookie['secure']
73
            )
74
        session.cookies = r_cookies
75

  
76
        headers = {
77
            'Content-Type': 'application/json',
78
        }
79
        content = '{"codeConfig":"", "xslPath":"Services/LectorShortAccount.xslt"}'
80
        url = request.build_absolute_uri(ws_uri)
81
        logger.debug(url) 
82
        request_response = session.post(url, headers=headers, data=content, verify=False) 
83
        data = request_response.json()
84
        logger.debug(pprint(data))
85
        return Response(data)
86

  
87

  
88
account = AccountDetails.as_view()
mandayejs/urls.py
13 13
#
14 14
# You should have received a copy of the GNU Affero General Public License
15 15
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
from __future__ import absolute_import
16 17

  
17 18
from django.conf.urls import patterns, include, url
18 19
from django.contrib import admin
19 20
from django.conf import settings
20 21

  
22

  
21 23
urlpatterns = patterns('',
22 24
    url(r'^_mandaye/panel$', 'mandayejs.mandaye.views.panel', name='panel'),
23 25
    url(r'^_mandaye/associate/$', 'mandayejs.mandaye.views.associate', name='associate'),
......
30 32
if 'mellon' in settings.INSTALLED_APPS:
31 33
    urlpatterns += patterns('', url(r'^_mandaye/accounts/mellon/', include('mellon.urls')))
32 34

  
35
if 'mandayejs.sites.archimed' in settings.INSTALLED_APPS:
36
    urlpatterns += patterns('',
37
        url(r'^_mandaye/archimed/', include('mandayejs.sites.archimed.urls')),
38
    )
33
-