summaryrefslogtreecommitdiff
path: root/www
diff options
context:
space:
mode:
authorPliable Pixels <pliablepixels@gmail.com>2019-05-15 09:57:02 -0400
committerPliable Pixels <pliablepixels@gmail.com>2019-05-15 09:57:02 -0400
commiteb327c9b7cbca93c7fee43b3a25e47112ba7c595 (patch)
tree62bfe43967d4e722f6751cd6bb5b5c7ef3e49245 /www
parent151b26e60a18ccdc4231b2ce52abf864848eaa90 (diff)
add token support to PTZ and stream control #817
Diffstat (limited to 'www')
-rw-r--r--www/js/MonitorModalCtrl.js37
-rw-r--r--www/js/MontageHistoryCtrl.js50
-rw-r--r--www/js/NVR.js46
-rwxr-xr-xwww/js/app.js59
4 files changed, 89 insertions, 103 deletions
diff --git a/www/js/MonitorModalCtrl.js b/www/js/MonitorModalCtrl.js
index bb8b3f8f..ed86d088 100644
--- a/www/js/MonitorModalCtrl.js
+++ b/www/js/MonitorModalCtrl.js
@@ -793,6 +793,11 @@ angular.module('zmApp.controllers').controller('MonitorModalCtrl', ['$scope', '$
};
}
+ if ($rootScope.authSession.indexOf("&token=")!=-1) {
+ ptzData['token']=$rootScope.authSession.match(/&token=([^&]*)/)[1];
+ }
+
+
//console.log("Command value " + cmd + " with MID=" + monitorId);
//console.log("PTZDATA is " + JSON.stringify(ptzData));
$ionicLoading.hide();
@@ -824,7 +829,7 @@ angular.module('zmApp.controllers').controller('MonitorModalCtrl', ['$scope', '$
str.push(encodeURIComponent(p) + "=" +
encodeURIComponent(obj[p]));
var foo = str.join("&");
- //console.log("****RETURNING " + foo);
+ console.log("****PTZ RETURNING " + foo);
return foo;
},
@@ -1411,9 +1416,22 @@ angular.module('zmApp.controllers').controller('MonitorModalCtrl', ['$scope', '$
var CMD_QUERY = 99;
*/
- var myauthtoken = $rootScope.authSession.replace("&auth=", "");
-
- //&auth=
+ // var myauthtoken='';
+
+ var data_payload = {
+ view: "request",
+ request: "stream",
+ connkey: connkey,
+ command: cmd
+ };
+
+ if ($rootScope.authSession.indexOf("&auth=")!=-1) {
+ data_payload['auth']=$rootScope.authSession.match(/&auth=([^&]*)/)[1];
+ }
+ else if ($rootScope.authSession.indexOf("&token=")!=-1) {
+ data_payload['token']=$rootScope.authSession.match(/&token=([^&]*)/)[1];
+ }
+
var req = $http({
method: 'POST',
/*timeout: 15000,*/
@@ -1428,18 +1446,11 @@ angular.module('zmApp.controllers').controller('MonitorModalCtrl', ['$scope', '$
str.push(encodeURIComponent(p) + "=" +
encodeURIComponent(obj[p]));
var foo = str.join("&");
- //console.log("****RETURNING " + foo);
+ console.log("****CONTROL RETURNING " + foo);
return foo;
},
- data: {
- view: "request",
- request: "stream",
- connkey: connkey,
- command: cmd,
- auth: myauthtoken,
-
- }
+ data: data_payload
});
req.then(function (resp) {
diff --git a/www/js/MontageHistoryCtrl.js b/www/js/MontageHistoryCtrl.js
index 567c8590..1f42e98e 100644
--- a/www/js/MontageHistoryCtrl.js
+++ b/www/js/MontageHistoryCtrl.js
@@ -531,7 +531,21 @@ angular.module('zmApp.controllers').controller('zmApp.MontageHistoryCtrl', ['$sc
function subControlStream(cmd, connkey) {
var loginData = NVR.getLogin();
- var myauthtoken = $rootScope.authSession.replace("&auth=", "");
+
+ var data_payload = {
+ view: "request",
+ request: "stream",
+ connkey: connkey,
+ command: cmd
+ };
+
+ if ($rootScope.authSession.indexOf("&auth=")!=-1) {
+ data_payload['auth']=$rootScope.authSession.match(/&auth=([^&]*)/)[1];
+ }
+ else if ($rootScope.authSession.indexOf("&token=")!=-1) {
+ data_payload['token']=$rootScope.authSession.match(/&token=([^&]*)/)[1];
+ }
+
//&auth=
var req = qHttp({
method: 'POST',
@@ -544,17 +558,10 @@ angular.module('zmApp.controllers').controller('zmApp.MontageHistoryCtrl', ['$sc
var str = [];
for (var p in obj) str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
var foo = str.join("&");
- //console.log("****SUB RETURNING " + foo);
+ console.log("****HISTORY CONTROL RETURNING " + foo);
return foo;
},
- data: {
- view: "request",
- request: "stream",
- connkey: connkey,
- command: cmd,
- auth: myauthtoken, // user: loginData.username,
- // pass: loginData.password
- }
+ data: data_payload
});
req.then(function (succ) {
NVR.debug("subControl success:" + JSON.stringify(succ));
@@ -614,28 +621,7 @@ angular.module('zmApp.controllers').controller('zmApp.MontageHistoryCtrl', ['$sc
url:cmdUrl
});
- /* var req = qHttp({
- method: 'POST',
- url: loginData.url + '/index.php?view=console',
- headers: {
- 'Content-Type': 'application/x-www-form-urlencoded',
- },
- transformRequest: function (obj) {
- var str = [];
- for (var p in obj) str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
- var foo = str.join("&");
- if (extras) foo = foo + extras;
- return foo;
- },
- data: {
- view: "request",
- request: "stream",
- connkey: connkey,
- command: cmd,
- auth: myauthtoken, // user: loginData.username,
- // pass: loginData.password
- }
- });*/
+
req.then(function (succ) {
var resp = succ.data;
diff --git a/www/js/NVR.js b/www/js/NVR.js
index 4f888c21..6c4736e4 100644
--- a/www/js/NVR.js
+++ b/www/js/NVR.js
@@ -2143,49 +2143,7 @@ angular.module('zmApp.controllers')
);
},
- /*killStream: function (ck) {
- debug ("Killing connKey: "+ck);
- var myauthtoken = $rootScope.authSession.replace("&auth=", "");
- var req = $http(
- {
- method: 'POST',
-
- url: loginData.url + '/index.php',
- headers:
- {
- 'Content-Type': 'application/x-www-form-urlencoded',
- ,
- },
- transformRequest: function(obj)
- {
- var str = [];
- for (var p in obj)
- str.push(encodeURIComponent(p) + "=" +
- encodeURIComponent(obj[p]));
- var foo = str.join("&");
- //console.log("****RETURNING " + foo);
- return foo;
- },
-
- data:
- {
- view: "request",
- request: "stream",
- connkey: ck,
- command: 3,
- auth: myauthtoken,
-
- }
- })
- .then (function (succ) {
- console.log ("STOP/KILL OK WITH: " + JSON.stringify(succ));
- },
- function (err) {
- console.log ("KILL ERROR WITH: " + JSON.stringify(err));
- });
-
-
- },*/
+
getMultiServersCached: function () {
return multiservers;
@@ -2626,7 +2584,7 @@ angular.module('zmApp.controllers')
} catch (e) {
debug("Login API approach did not work...");
- d.reject("API based login failed")
+ d.reject("API based login failed");
return d.promise;
}
diff --git a/www/js/app.js b/www/js/app.js
index 3cdf49da..cf95a600 100755
--- a/www/js/app.js
+++ b/www/js/app.js
@@ -111,7 +111,9 @@ angular.module('zmApp', [
zmVersionCheckNag: 60 * 24, // in hrs
waitTimeTillResume: 5, // in sec, for ES error
versionWithLoginAPI: "1.31.47",
- androidBackupKey: "AEdPqrEAAAAIqF-OaHdwIzZhx2L1WOfAGTagBxm5a1R4wBW_Uw"
+ androidBackupKey: "AEdPqrEAAAAIqF-OaHdwIzZhx2L1WOfAGTagBxm5a1R4wBW_Uw",
+ accessTokenLeewayMin: 5,
+ refreshTokenLeewayMin: 30
})
@@ -685,15 +687,17 @@ angular.module('zmApp', [
responseError: function (rejection) {
if (rejection.status == 401) {
nvr = $injector.get('NVR');
- nvr.log ("******** INTERCEPTOR CAUGHT ERROR, RE-LOGIN NEEDED");
-
+ nvr.log ("Browser Http intecepted 401, will try reauth");
return nvr.recreateTokens()
- .then (function() {
+ .then (function(succ) {
+
+ rejection.config.url = rejection.config.url.replace(/&token=([^&]*)/, $rootScope.authSession);
return $injector.get('$http')(rejection.config);
- })
+ }, function (err) {
+ return response;
+ });
}
else {
- nvr.log ("NOT 401");
return response;
}
@@ -1084,7 +1088,7 @@ angular.module('zmApp', [
var diff_refresh = moment.utc(ld.refreshTokenExpires).diff(now, 'minutes');
// first see if we can work with access token
- if (moment.utc(ld.accessTokenExpires).isAfter(now) && diff_access >=5) {
+ if (moment.utc(ld.accessTokenExpires).isAfter(now) && diff_access >=zm.accessTokenLeewayMin) {
NVR.log ("Access token still has "+diff_access+" minutes left, using it");
$rootScope.authSession = '&token='+ld.accessToken;
d.resolve("Login success via access token");
@@ -1092,7 +1096,7 @@ angular.module('zmApp', [
return d.promise;
}
// then see if we have at least 30 mins left for refresh token
- else if (moment.utc(ld.refreshTokenExpires).isAfter(now) && diff_refresh >=30) {
+ else if (moment.utc(ld.refreshTokenExpires).isAfter(now) && diff_refresh >=zm.refreshTokenLeewayMin) {
NVR.log ("Refresh token still has "+diff_refresh+" minutes left, using it");
var loginAPI = ld.apiurl + '/host/login.json?token='+ld.refreshToken;
$http.get(loginAPI)
@@ -1601,6 +1605,10 @@ angular.module('zmApp', [
NVR.debug(msg);
};
+ $rootScope.recreateTokens = function() {
+ return NVR.recreateTokens();
+ };
+
// DPAD Handler - disabled for now
// when ready add ionic cordova plugin add https://github.com/pliablepixels/cordova-plugin-android-tv.git
@@ -2436,7 +2444,8 @@ angular.module('zmApp', [
$provide.decorator('$http', ['$delegate', '$q', '$injector', function ($delegate, $q, $injector) {
// create function which overrides $http function
var $http = $delegate;
- var logger = $injector.get("$rootScope");
+ var nvr = $injector.get("$rootScope");
+
var wrapper = function () {
var url;
@@ -2475,7 +2484,7 @@ angular.module('zmApp', [
// console.log ("HTTP RESPONSE:" + JSON.stringify(succ.data));
if (succ.data && (succ.data.indexOf("<pre class=\"cake-error\">") == 0) ) {
- logger.debug ("**** Native: cake-error in message, trying fix...");
+ nvr.debug ("**** Native: cake-error in message, trying fix...");
succ.data = JSON.parse(succ.data.replace(/<pre class=\"cake-error\">[\s\S]*<\/pre>/,''));
}
@@ -2506,10 +2515,32 @@ angular.module('zmApp', [
}
},
function (err) {
- logger.debug("*** Inside native HTTP error: " + JSON.stringify(err));
-
- d.reject(err);
- return d.promise;
+ var d = $q.defer();
+ nvr.debug("*** Inside native HTTP error: " + JSON.stringify(err));
+ if (err.status == 401) {
+ nvr.debug ("** Native intercept: Got 401, going to try recreating tokens");
+ return nvr.recreateTokens()
+ .then (function() {
+ nvr.debug ("** Native, tokens generated, retrying old request");
+ url = url.replace(/&token=([^&]*)/, nvr.authSession);
+ cordova.plugin.http.sendRequest(encodeURI(url), options,
+ function (succ) {
+ d.resolve(succ);
+ return d.promise;
+ },
+ function (err) {
+ d.resolve(err);
+ return d.promise;
+ });
+ return d.promise;
+ }, function (err) {d.reject(err); return d.promise;});
+ }
+ else {
+ // not a 401, so pass on rejection
+ d.reject(err);
+ return d.promise;
+ }
+
});
return d.promise;