From 450b3865a8df00c146337ba903ec56e531d1591f Mon Sep 17 00:00:00 2001 From: JonnyWong16 <9099342+JonnyWong16@users.noreply.github.com> Date: Sat, 20 Jun 2020 10:59:55 -0700 Subject: [PATCH] Validate OneSignal Player ID when registering device --- plexpy/api2.py | 11 ++++++++--- plexpy/notifiers.py | 8 ++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/plexpy/api2.py b/plexpy/api2.py index fc2b40a2..37f31a8c 100644 --- a/plexpy/api2.py +++ b/plexpy/api2.py @@ -407,12 +407,17 @@ class API2(object): ``` """ if not device_id: - self._api_msg = 'Device registartion failed: no device id provided.' + self._api_msg = 'Device registration failed: no device id provided.' self._api_result_type = 'error' return elif not device_name: - self._api_msg = 'Device registartion failed: no device name provided.' + self._api_msg = 'Device registration failed: no device name provided.' + self._api_result_type = 'error' + return + + elif not notifiers.ANDROIDAPP().validate_device_id(device_id=device_id): + self._api_msg = 'Device registration failed: invalid OneSignal Player ID.' self._api_result_type = 'error' return @@ -426,7 +431,7 @@ class API2(object): self._api_result_type = 'success' mobile_app.set_temp_device_token(None) else: - self._api_msg = 'Device registartion failed: database error.' + self._api_msg = 'Device registration failed: database error.' self._api_result_type = 'error' return diff --git a/plexpy/notifiers.py b/plexpy/notifiers.py index cd26d37d..aa46a08e 100644 --- a/plexpy/notifiers.py +++ b/plexpy/notifiers.py @@ -884,6 +884,14 @@ class ANDROIDAPP(Notifier): _ONESIGNAL_APP_ID = '3b4b666a-d557-4b92-acdf-e2c8c4b95357' + def validate_device_id(self, device_id): + headers = {'Content-Type': 'application/json'} + payload = {'app_id': self._ONESIGNAL_APP_ID} + + r = requests.get('https://onesignal.com/api/v1/players/{}'.format(device_id), headers=headers, json=payload) + if r.status_code == 200: + return r.json() + def agent_notify(self, subject='', body='', action='', notification_id=None, **kwargs): # Check mobile device is still registered device = mobile_app.get_mobile_devices(device_id=self.config['device_id'])