Projet

Général

Profil

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

mandayejs / mandayejs / applications / views.py @ c23f4a3b

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

    
22
from django.contrib.auth.models import User
23
from rest_framework import status
24
from rest_framework.views import APIView
25
from rest_framework.response import Response
26

    
27
from mandayejs.mandaye.models import UserCredentials
28

    
29

    
30
class ArchimedAccountDetails(APIView):
31
    """Archimed user's account details
32
    """
33

    
34
    def dispatch(self, request, *args, **kwargs):
35
        response = super(ArchimedAccountDetails, self).dispatch(request, *args, **kwargs)
36
        if response.status_code == 200:
37
            response.data = {'data': response.data, 'err': 0}
38
        else:
39
            response.data = {'data': None, 'err': 1, 'err_desc': response.data}
40
        return response
41

    
42
    def get(self, request, *args, **kwargs):
43
        from mandayejs.applications import get_app_settings
44
        app_settings = get_app_settings()
45
        logger = logging.getLogger(__name__)
46
        app_settings = get_app_settings()
47
        ws_uri = request.build_absolute_uri(
48
            app_settings.SITE_WS_ENDPOINT['account_details'])
49

    
50
        # mellon truncates username to 30 characters
51
        # thus the passed username must be truncated to 30 characters
52
        # for searching purpose.
53
        username = kwargs['username'][:30]
54

    
55
        try:
56
            user = User.objects.get(username=username)
57
        except (User.DoesNotExist,):
58
            return Response('User %s does not exist' % username, status=status.HTTP_404_NOT_FOUND)
59

    
60
        try:
61
            credentials = UserCredentials.objects.get(user=user)
62
        except (UserCredentials.DoesNotExist,):
63
            return Response('User %s is not associated' % username, status=status.HTTP_404_NOT_FOUND)
64

    
65
        login_url = request.build_absolute_uri(
66
            '/DEFAULT/Ermes/Recherche/logon.svc/logon')
67

    
68
        with requests.Session() as session:
69
            login_info = credentials.to_login_info(decrypt=True)
70
            login_info = {'username': login_info['#carte'], 'password': login_info['#code']}
71
            response = session.post(login_url, data=login_info)
72
            logger.debug("Archimed login response {}".format(response.json()))
73
            if not response.json()['success']:
74
                return Response('Authentication failed', status=status.HTTP_401_UNAUTHORIZED)
75

    
76
            content = {
77
                'codeConfig': '',
78
                'xslPath': 'Services/LectorShortAccount.xslt'
79
            }
80
            response = session.post(ws_uri, json=content)
81
            logger.debug("Archimed ws response  {}".format(response.json()))
82
        return Response(response.json())
83

    
84

    
85
archimed_account_details = ArchimedAccountDetails.as_view()
(2-2/2)