summaryrefslogtreecommitdiff
path: root/www/js
diff options
context:
space:
mode:
authorPliablePixels <pliablepixels@gmail.com>2015-08-11 15:47:11 -0400
committerPliablePixels <pliablepixels@gmail.com>2015-08-11 15:47:11 -0400
commita09333e8b97a5f5c71331ce18777889510aed302 (patch)
tree9f6b69e4996088ee13004c3bee6ea9d5f57e89db /www/js
parent68af457b04b26e26f0414b821dabe7359e18a0ad (diff)
API auth tied into ZM auth, enabled client to work in both auth and non-auth mode honoring ZM's auth system
Diffstat (limited to 'www/js')
-rw-r--r--www/js/DataModel.js21
-rw-r--r--www/js/LoginCtrl.js30
-rw-r--r--www/js/PortalLoginCtrl.js15
-rw-r--r--www/js/app.js146
4 files changed, 177 insertions, 35 deletions
diff --git a/www/js/DataModel.js b/www/js/DataModel.js
index 0d9d6ca8..e61ef052 100644
--- a/www/js/DataModel.js
+++ b/www/js/DataModel.js
@@ -24,7 +24,8 @@ angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ion
'maxFPS': "3", // image streaming FPS
'montageQuality': "50", // montage streaming quality in %
'useSSL':false, // "1" if HTTPS
- 'keepAwake':true // don't dim/dim during live view
+ 'keepAwake':true, // don't dim/dim during live view
+ 'isUseAuth':true // true if user wants ZM auth
};
var configParams = {
'ZM_EVENT_IMAGE_DIGITS':'-1'
@@ -64,6 +65,19 @@ angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ion
console.log("****** DATAMODEL INIT SERVICE CALLED ********");
zmLog("ZMData init: checking for stored variables & setting up log file");
+
+ if (window.localStorage.getItem("isUseAuth") != undefined) {
+ loginData.isUseAuth =
+ window.localStorage.getItem("isUseAuth");
+
+ }
+ else
+ {
+ loginData.isUseAuth = "1"; // lets assume there is auth
+ zmLog("I did not find isUseAuth. Older version of app, maybe.");
+ }
+
+
if (window.localStorage.getItem("username") != undefined) {
loginData.username =
@@ -142,7 +156,7 @@ angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ion
},
isLoggedIn: function () {
- if (loginData.username != "" && loginData.password != "" && loginData.url != "" && loginData.apiurl != "")
+ if ( (loginData.username != "" && loginData.password != "" && loginData.url != "" && loginData.apiurl != "") || (loginData.isUseAuth == '0'))
{
return 1;
} else
@@ -218,6 +232,9 @@ angular.module('zmApp.controllers').service('ZMDataModel', ['$http', '$q', '$ion
window.localStorage.setItem("keepAwake", loginData.keepAwake?"1":"0");
window.localStorage.setItem("maxMontage", loginData.maxMontage);
window.localStorage.setItem("montageQuality", loginData.montageQuality);
+ window.localStorage.setItem("isUseAuth", loginData.isUseAuth);
+
+ console.log ("***** SETTING ISUSEAUTH TO " + loginData.isUseAuth);
if (loginData.maxFPS > 30) {
diff --git a/www/js/LoginCtrl.js b/www/js/LoginCtrl.js
index b3c052d5..082930be 100644
--- a/www/js/LoginCtrl.js
+++ b/www/js/LoginCtrl.js
@@ -8,7 +8,11 @@ angular.module('zmApp.controllers').controller('zmApp.LoginCtrl', ['$scope', '$r
};
$scope.loginData = ZMDataModel.getLogin();
+
+ $scope.auth={isUseAuth:""};
+ $scope.auth.isUseAuth = ($scope.loginData.isUseAuth == '1') ? true:false;
+
//-------------------------------------------------------------------------
@@ -78,6 +82,8 @@ function addhttp(url) {
$scope.loginData.apiurl = $scope.loginData.apiurl.trim();
$scope.loginData.username = $scope.loginData.username.trim();
$scope.loginData.streamingurl = $scope.loginData.streamingurl.trim();
+
+ $scope.loginData.isUseAuth = ($scope.auth.isUseAuth) ? "1": "0";
if ($scope.loginData.url.slice(-1) == '/') {
$scope.loginData.url = $scope.loginData.url.slice(0, -1);
@@ -122,10 +128,6 @@ function addhttp(url) {
$scope.loginData.streamingurl = $scope.loginData.streamingurl.replace("https:","http:");
}
-
-
- // FIXME:: Do a login id check too
-
var apiurl = $scope.loginData.apiurl + '/host/getVersion.json';
var portalurl = $scope.loginData.url + '/index.php';
var streamingurl = $scope.loginData.streamingurl +
@@ -200,15 +202,31 @@ function addhttp(url) {
}
);*/
+
+ // Check if isUseAuth is set make sure u/p have a dummy value
+ if ($scope.isUseAuth)
+ {
+ if (!$scope.loginData.username) $scope.loginData.username="x";
+ if (!$scope.loginData.password) $scope.loginData.password="x";
+ ZMDataModel.zmLog("Authentication is disabled, setting dummy user & pass");
+ }
+
ZMDataModel.setLogin($scope.loginData);
- zmAutoLogin.doLogin("Logging into ZoneMinder")
+ zmAutoLogin.doLogin("authenticating...")
+ // Do the happy menu only if authentication works
+ // if it does not work, there is an emitter for auth
+ // fail in app.js that will be called to show an error
+ // box
+
+ // Note that API auth ties into ZM Auth, so in one fell
+ // swoop we've just validated both. Happy?
.then( function(data)
{
console.log ("THE DATA WAS " + data);
console.log ("SHOWING POPUP ");
$ionicPopup.alert({
- title: 'Settings Saved',
+ title: 'Login validated',
template: 'Please explore the menu and enjoy zmNinja!'
}).then(function(res) { $ionicSideMenuDelegate.toggleLeft();});
});
diff --git a/www/js/PortalLoginCtrl.js b/www/js/PortalLoginCtrl.js
index ae75d197..422c18ab 100644
--- a/www/js/PortalLoginCtrl.js
+++ b/www/js/PortalLoginCtrl.js
@@ -18,12 +18,21 @@ console.log ("***** INSIDE LOGIN CONTROLLER");
if (ZMDataModel.isLoggedIn()) {
ZMDataModel.zmLog ("User credentials are provided");
// console.log("VALID CREDENTIALS. Grabbing Monitors");
- zmAutoLogin.doLogin("Logging into ZoneMinder...")
- .then (function(data)
+ zmAutoLogin.doLogin("authenticating...")
+ .then (function(data) // success
{
ZMDataModel.getKeyConfigParams(1);
$state.go('montage');
- });
+ },
+ // coming here means auth error
+ // so go back to login
+ function (error)
+ {
+ $state.go('login');
+ }
+
+
+ );
}
else
{
diff --git a/www/js/app.js b/www/js/app.js
index 8737a3d0..a9a47f6d 100644
--- a/www/js/app.js
+++ b/www/js/app.js
@@ -47,7 +47,8 @@ angular.module('zmApp', [
monitorErrorColor: '#795548',
montageScaleFrequency:300,
eventsListDetailsHeight:200,
- eventsListScrubHeight:300
+ eventsListScrubHeight:300,
+ loginScreenString:"var currentView = 'login'" // oh shit. Isn't there a better way?
@@ -227,12 +228,59 @@ angular.module('zmApp', [
// This service automatically logs into ZM at periodic intervals
//------------------------------------------------------------------
-.factory('zmAutoLogin', function($interval, ZMDataModel, $http,zm, $browser,$timeout,$q, $rootScope, $ionicLoading) {
+.factory('zmAutoLogin', function($interval, ZMDataModel, $http,zm, $browser,$timeout,$q, $rootScope, $ionicLoading, $ionicPopup, $state) {
var zmAutoLoginHandle;
-
+
+//------------------------------------------------------------------
+// doLogin() emits this when there is an auth error in the portal
+//------------------------------------------------------------------
+
+ $rootScope.$on ("auth-error", function()
+ {
+ console.log ("**** ZM LOGIN ERROR INTERCEPT");
+
+ var alertPopup = $ionicPopup.alert(
+ {
+ title: 'Zoneminder authentication failed',
+ template: 'This might be a temporary situation and may result in zmNinja not working properly. Please try to log in again.'
+ });
+ alertPopup.then (function(data){
+ //$state.transitionTo('login');
+ });
+
+
+ });
+
+//------------------------------------------------------------------
+// doLogin() emits this when our auth credentials work
+//------------------------------------------------------------------
+
+
+ $rootScope.$on ("auth-success", function()
+ {
+ console.log ("**** ZM LOGIN SUCCESS INTERCEPT");
+ });
+
+
+//------------------------------------------------------------------
+// doLogin() is the function that tries to login to ZM
+// it also makes sure we are not back to the same page
+// which actually means auth failed, but ZM treats it as a success
+//------------------------------------------------------------------
+
function doLogin(str)
{
- var d = $q.defer();
+ var d = $q.defer();
+ var ld = ZMDataModel.getLogin();
+ if (ld.isUseAuth == "0")
+ {
+ $ionicLoading.hide();
+ ZMDataModel.zmLog ("Authentication is disabled. Skipping login");
+ d.resolve("Login success - no auth");
+ return d.promise;
+ }
+
+
/* if ($rootScope.loggedIntoZm == 1)
{
d.resolve("Already logged in");
@@ -281,16 +329,39 @@ angular.module('zmApp', [
{
$ionicLoading.hide();
- $rootScope.loggedIntoZm = 1;
- console.log ("**** ZM Login OK");
- ZMDataModel.zmLog("zmAutologin successfully logged into Zoneminder");
+ // Coming here does not mean success
+ // it could also be a bad login, but
+ // ZM returns you to login.php and returns 200 OK
+ // so we will check if the data has
+ // <title>ZM - Login</title> -- it it does then its the login page
+
+
+ if (data.indexOf(zm.loginScreenString) == -1 )
+ {
+
+ $rootScope.loggedIntoZm = 1;
+ console.log ("**** ZM Login OK");
+ ZMDataModel.zmLog("zmAutologin successfully logged into Zoneminder");
+
+ d.resolve("Login Success");
+
+ $rootScope.$emit('auth-success', data);
+ $rootScope.rand = Math.floor((Math.random() * 100000) + 1);
+ }
+ else // this means login error
+ {
+ $rootScope.loggedIntoZm = -1;
+ console.log ("**** ZM Login FAILED");
+ ZMDataModel.zmLog ("zmAutologin Error: Bad Credentials ", "error");
+ $rootScope.$emit('auth-error', "incorrect credentials");
+
+ d.reject("Login Error");
+ }
+
//$timeout( function() {console.log ("***** ALL COOKIES:" + JSON.stringify( $browser.cookies()));},1000);
- d.resolve("Login Success");
- $rootScope.$broadcast('event:auth-login-success', data);
- $rootScope.rand = Math.floor((Math.random() * 100000) + 1);
return (d.promise);
@@ -301,8 +372,9 @@ angular.module('zmApp', [
$rootScope.loggedIntoZm = -1;
console.log ("**** ZM Login FAILED");
ZMDataModel.zmLog ("zmAutologin Error " + JSON.stringify(error), "error");
- d.resolve("Login Error");
- $rootScope.$broadcast('event:auth-login-failed', error);
+ $rootScope.$emit('auth-error', error);
+
+ d.reject("Login Error");
return d.promise;
});
return d.promise;
@@ -311,23 +383,38 @@ angular.module('zmApp', [
function start()
{
- $rootScope.loggedIntoZm = 0;
- $interval.cancel(zmAutoLoginHandle);
- //doLogin();
- zmAutoLoginHandle = $interval(function()
+ var ld = ZMDataModel.getLogin();
+ if (ld.isUseAuth == '1')
{
- doLogin("");
+ $rootScope.loggedIntoZm = 0;
+ $interval.cancel(zmAutoLoginHandle);
+ //doLogin();
+ zmAutoLoginHandle = $interval(function()
+ {
+ doLogin("");
- },zm.loginInterval); // Auto login every 5 minutes
- // PHP timeout is around 10 minutes
- // should be ok?
+ },zm.loginInterval); // Auto login every 5 minutes
+ // PHP timeout is around 10 minutes
+ // should be ok?
+ }
+ else
+ {
+ ZMDataModel.zmLog ("Authentication not enabled. Skipping Timer");
+ }
}
function stop()
{
- $interval.cancel(zmAutoLoginHandle);
- $rootScope.loggedIntoZm = 0;
- ZMDataModel.zmLog("Cancelling zmAutologin timer");
-
+ var ld = ZMDataModel.getLogin();
+ if (ld.isUseAuth == '1')
+ {
+ $interval.cancel(zmAutoLoginHandle);
+ $rootScope.loggedIntoZm = 0;
+ ZMDataModel.zmLog("Cancelling zmAutologin timer");
+ }
+ else
+ {
+ ZMDataModel.zmLog ("No need to cancel zmAutologin timer. Auth is off");
+ }
}
return {
@@ -337,6 +424,13 @@ angular.module('zmApp', [
};
})
+
+
+
+
+
+
+
/* For future use - does not work with img src intercepts
.factory ('httpAuthIntercept', function ($rootScope, $q)
{
@@ -445,6 +539,8 @@ angular.module('zmApp', [
});
+
+
$ionicPlatform.ready(function () {
// generates and error in desktops but works fine
@@ -472,6 +568,8 @@ angular.module('zmApp', [
// that allows you to manage log files without worrying about
// paths etc.https://github.com/pbakondy/filelogger
$fileLogger.setStorageFilename(zm.logFile);
+ // easier tz reading
+ $fileLogger.setTimestampFormat('medium');
if (window.cordova)
{