From 7834f1947f900742f06e011cf74710e8a18544f0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= <fpeters@entrouvert.com>
Date: Mon, 30 Mar 2020 10:08:52 +0200
Subject: [PATCH] strasbourg eu: protect against service errors (#41149)

---
 passerelle/contrib/strasbourg_eu/models.py | 45 +++++++++++++++++-----
 1 file changed, 36 insertions(+), 9 deletions(-)

diff --git a/passerelle/contrib/strasbourg_eu/models.py b/passerelle/contrib/strasbourg_eu/models.py
index 5855b07e..889283be 100644
--- a/passerelle/contrib/strasbourg_eu/models.py
+++ b/passerelle/contrib/strasbourg_eu/models.py
@@ -56,17 +56,26 @@ class StrasbourgEu(BaseResource):
             if interests is None:
                 interests = []  # reset
             url = urlparse.urljoin(self.liferay_api_url, 'jsonws/interest.interest/set-user-interests')
-            response = self.requests.post(url, data={'userId': name_id, 'interestIds': ','.join(interests)}).json()
+            try:
+                response = self.requests.post(url, data={'userId': name_id, 'interestIds': ','.join(interests)}).json()
+            except ValueError:
+                return {'err': 2, 'err_desc': 'invalid service answer'}
             if 'error' in response:
                 return {'err': 1, 'err_desc': response.get('error')}
         url = urlparse.urljoin(self.liferay_api_url, 'jsonws/interest.interest/get-interests')
-        response = self.requests.get(url).json()
+        try:
+            response = self.requests.get(url).json()
+        except ValueError:
+            return {'err': 2, 'err_desc': 'invalid service answer'}
         interests = response.get('interests')
         if interests is None:
             return {'err': 1, 'err_desc': response.get('error')}
         if name_id is not None:
             url = urlparse.urljoin(self.liferay_api_url, 'jsonws/interest.interest/get-user-interests')
-            user_choices = self.requests.post(url, data={'userId': name_id}).json().get('interests')
+            try:
+                user_choices = self.requests.post(url, data={'userId': name_id}).json().get('interests')
+            except ValueError:
+                return {'err': 2, 'err_desc': 'invalid service answer'}
             interests = [x for x in interests if x['id'] in user_choices]
         for interest in interests:
             interest['text'] = '%s / %s' % (interest['type'], interest['name'])
@@ -80,7 +89,10 @@ class StrasbourgEu(BaseResource):
     def notifications(self, request, name_id, **kwargs):
         if request.method == 'GET':
             url = urlparse.urljoin(self.liferay_api_url, 'jsonws/notification.notification/get-user-notifications')
-            notifications = self.requests.post(url, data={'userId': name_id}).json()
+            try:
+                notifications = self.requests.post(url, data={'userId': name_id}).json()
+            except ValueError:
+                return {'err': 2, 'err_desc': 'invalid service answer'}
             if 'error' in notifications:
                 return {'err': 1, 'err_desc': notifications.get('error')}
             for notification in notifications['notifications']:
@@ -105,7 +117,10 @@ class StrasbourgEu(BaseResource):
             notification = json_loads(request.body)
             notification['userId'] = name_id
             url = urlparse.urljoin(self.liferay_api_url, 'jsonws/notification.notification/add-notification')
-            response = self.requests.post(url, data=notification).json()
+            try:
+                response = self.requests.post(url, data=notification).json()
+            except ValueError:
+                return {'err': 2, 'err_desc': 'invalid service answer'}
             if response.get('success'):
                 return {'err': 0, 'err_desc': response.get('success')}
             else:
@@ -118,7 +133,10 @@ class StrasbourgEu(BaseResource):
     def favorites(self, request, name_id, url_filter=None, **kwargs):
         if request.method == 'GET':
             url = urlparse.urljoin(self.liferay_api_url, 'jsonws/favorite.favorite/get-user-favorites')
-            response = self.requests.post(url, data={'userId': name_id}).json()
+            try:
+                response = self.requests.post(url, data={'userId': name_id}).json()
+            except ValueError:
+                return {'err': 2, 'err_desc': 'invalid service answer'}
             if 'error' in response:
                 return {'err': 1, 'err_desc': response.get('error')}
             favorites = response['favorites']
@@ -131,14 +149,20 @@ class StrasbourgEu(BaseResource):
             favorite = json_loads(request.body)
             # change type to typeId
             types_url = urlparse.urljoin(self.liferay_api_url, 'jsonws/favorite.favorite/get-types')
-            types = self.requests.get(types_url).json()
+            try:
+                types = self.requests.get(types_url).json()
+            except ValueError:
+                return {'err': 2, 'err_desc': 'invalid service answer'}
             # types: {"types": [{"id": "1", "name": "PLACE"}, ...]}
             types_dict = {x['name']: x['id'] for x in types['types']}
             favorite['typeId'] = types_dict.get(favorite.pop('type'))
             favorite['userId'] = name_id
             # send favorite
             url = urlparse.urljoin(self.liferay_api_url, 'jsonws/favorite.favorite/add-favorite')
-            response = self.requests.post(url, data=favorite).json()
+            try:
+                response = self.requests.post(url, data=favorite).json()
+            except ValueError:
+                return {'err': 2, 'err_desc': 'invalid service answer'}
             if response.get('success'):
                 return {'err': 0, 'err_desc': response.get('success')}
             else:
@@ -155,7 +179,10 @@ class StrasbourgEu(BaseResource):
     def favorite_delete(self, request, name_id, favorite_id, **kwargs):
         url = urlparse.urljoin(self.liferay_api_url, 'jsonws/favorite.favorite/delete-favorite')
         params = {'userId': name_id, 'favoriteId': favorite_id}
-        response = self.requests.post(url, data=params).json()
+        try:
+            response = self.requests.post(url, data=params).json()
+        except ValueError:
+            return {'err': 2, 'err_desc': 'invalid service answer'}
         if response.get('success'):
             return {'err': 0, 'err_desc': response.get('success')}
         else:
-- 
2.26.0

