Projet

Général

Profil

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

Josué Kouka, 20 janvier 2016 19:00

Télécharger (8 ko)

Voir les différences:

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

 mandayejs/mandaye/utils.py        | 12 ++++++
 mandayejs/mandaye/views.py        | 11 ++----
 mandayejs/settings.py             |  1 +
 mandayejs/sites/archimed/urls.py  | 23 ++++++++++++
 mandayejs/sites/archimed/views.py | 77 +++++++++++++++++++++++++++++++++++++++
 mandayejs/urls.py                 |  6 +++
 6 files changed, 122 insertions(+), 8 deletions(-)
 create mode 100644 mandayejs/sites/archimed/urls.py
 create mode 100644 mandayejs/sites/archimed/views.py
mandayejs/mandaye/utils.py
54 54
    except (IndexError,):
55 55
        return None
56 56

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

  
62
    return {
63
        'address': request.build_absolute_uri(app_settings.SITE_LOGIN_PATH),
64
        'cookies': [],
65
        'locators': [ credentials.to_login_info() ],
66
        'auth_checker': app_settings.SITE_AUTH_CHECKER,
67
        'form_submit_element': app_settings.SITE_FORM_SUBMIT_ELEMENT
68
    }
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 requests.cookies import RequestsCookieJar
22

  
23
from django.contrib.auth.models import User
24
from django.shortcuts import get_object_or_404
25

  
26
from rest_framework import status
27
from rest_framework.views import APIView
28
from rest_framework.response import Response
29

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

  
34

  
35
class AccountDetails(APIView):
36
    """Archimed user's account details
37
    """
38

  
39
    def get(self, request, *args, **kwargs):
40
        app_settings = get_app_settings()
41

  
42
        username = kwargs['username']
43
        user = get_object_or_404(User, username=username)
44
        credentials = get_object_or_404(UserCredentials, user=user)
45

  
46
        login_info = get_login_info(request, credentials)
47
        login_info['locators'] = [ credentials.to_login_info(decrypt=True)]
48
        result = exec_phantom(login_info)
49

  
50
        if result.get('result') != 'ok':
51
            return Response(status=status.HTTP_401_UNAUTHORIZED)
52

  
53
        session = requests.session()
54
        r_cookies = RequestsCookieJar()
55

  
56
        for cookie in result.get('cookies'):
57
            r_cookies.set(
58
                cookie['name'],
59
                cookie['value'],
60
                domain=cookie['domain'],
61
                path=cookie['path'],
62
                secure=cookie['secure']
63
            )
64
        session.cookies = r_cookies
65

  
66
        headers = {
67
            'Content-Type': 'application/json',
68
        }
69
        content = '{"codeConfig":"", "xslPath":"Services/LectorShortAccount.xslt"}'
70
        url = request.build_absolute_uri('/DEFAULT/Ermes/Services/ILSClient.svc/RetrieveAccount')
71

  
72
        request_response = session.post(url, headers=headers, data=content, verify=False) 
73
        
74
        return Response(request_response)
75

  
76

  
77
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
-