From 3ec0e1bdf394553f464bf0c795235e3571e34b44 Mon Sep 17 00:00:00 2001 From: Savidude Date: Mon, 3 Jul 2017 15:55:34 +0530 Subject: [PATCH] Auto renewal of auth token for RaspberryPi agent --- .../src/main/resources/agent/src/mqttConnector.py | 7 +++++++ .../src/main/resources/agent/src/token_updater.py | 15 ++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.backend.feature/src/main/resources/agent/src/mqttConnector.py b/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.backend.feature/src/main/resources/agent/src/mqttConnector.py index 7502ed8689..7957656194 100644 --- a/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.backend.feature/src/main/resources/agent/src/mqttConnector.py +++ b/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.backend.feature/src/main/resources/agent/src/mqttConnector.py @@ -100,6 +100,13 @@ def on_disconnect(client, userdata, rc): agent_connected = False print ("Agent disconnected from broker") + print("Obtaining new access token") + token = RefreshToken() + response = token.updateTokens() + newAccessToken = response['access_token'] + client.username_pw_set(newAccessToken, password="") + + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # The Main method of the server script # This method is invoked from RaspberryStats.py on a new thread diff --git a/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.backend.feature/src/main/resources/agent/src/token_updater.py b/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.backend.feature/src/main/resources/agent/src/token_updater.py index 219d07b315..bd08ce2666 100644 --- a/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.backend.feature/src/main/resources/agent/src/token_updater.py +++ b/features/device-types-feature/raspberrypi-plugin-feature/org.wso2.carbon.device.mgt.iot.raspberrypi.backend.feature/src/main/resources/agent/src/token_updater.py @@ -1,7 +1,8 @@ -import httplib import json import urllib import iotUtils +import requests + applicationKey = None refreshToken = None @@ -10,11 +11,11 @@ filename = "deviceConfig.properties" class RefreshToken(): - def post(self, base, port, url, payload, appKey): - conn = httplib.HTTPConnection(base, port, timeout=60) - conn.request('POST', url, payload, { 'Authorization' : 'Basic '+appKey, 'Content-Type' : 'application/x-www-form-urlencoded' }) - r = conn.getresponse() - return r.read() + def post(self, url, payload, appKey): + headers = { 'Authorization' : 'Basic ' + appKey, 'Content-Type' : 'application/x-www-form-urlencoded' } + baseUrl = iotUtils.HTTP_EP + url + response = requests.post(baseUrl, params=payload, headers=headers); + return response def read_server_conf(self): with open(filename, 'r') as outfile: @@ -46,7 +47,7 @@ class RefreshToken(): params = urllib.urlencode({"grant_type": "refresh_token", "refresh_token": refreshToken, "scope": "Enroll device"}) - data = self.post("localhost", 8280, "/token", params,applicationKey) + data = self.post("/token", params, applicationKey) response = json.loads(data) self.updateFile(response) return response \ No newline at end of file