Skip to content

Commit

Permalink
add token support to PTZ and stream control #817
Browse files Browse the repository at this point in the history
  • Loading branch information
pliablepixels committed May 15, 2019
1 parent 07c80f7 commit 541d180
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 108 deletions.
6 changes: 3 additions & 3 deletions config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@
<plugin name="cordova-plugin-photo-library" spec="https://github.com/nilebma/cordova-plugin-photo-library.git">
<variable name="PHOTO_LIBRARY_USAGE_DESCRIPTION" value=" " />
</plugin>
<plugin name="cordova-plugin-media-pp-fork" spec="^1.0.2-dev" />
<engine name="android" spec="7.1.4" />
<engine name="ios" spec="5.0.0" />
<plugin name="cordova-plugin-ionic-webview" spec="https://github.com/pliablepixels/cordova-plugin-ionic-webview.git">
<variable name="ANDROID_SUPPORT_ANNOTATIONS_VERSION" value="27.+" />
</plugin>
<plugin name="cordova-plugin-media-pp-fork" spec="^1.0.2-dev" />
<engine name="android" spec="7.1.4" />
<engine name="ios" spec="5.0.0" />
</widget>
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@
"cordova-plugin-photo-library": {
"PHOTO_LIBRARY_USAGE_DESCRIPTION": "Save monitor photos to albums "
},
"cordova-plugin-media-pp-fork": {},
"cordova-plugin-ionic-webview": {
"ANDROID_SUPPORT_ANNOTATIONS_VERSION": "27.+"
}
},
"cordova-plugin-media-pp-fork": {}
}
},
"dependencies": {
Expand Down
37 changes: 24 additions & 13 deletions www/js/MonitorModalCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
},

Expand Down Expand Up @@ -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,*/
Expand All @@ -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) {

Expand Down
50 changes: 18 additions & 32 deletions www/js/MontageHistoryCtrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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));
Expand Down Expand Up @@ -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;

Expand Down
46 changes: 2 additions & 44 deletions www/js/NVR.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

}
Expand Down
59 changes: 45 additions & 14 deletions www/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

})

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -1084,15 +1088,15 @@ 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");
$rootScope.$broadcast('auth-success', '' );
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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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>/,''));
}

Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 541d180

Please sign in to comment.