Skip to content

Logins #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 24, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 28 additions & 8 deletions avRegistration/auth-method-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,33 @@

angular.module('avRegistration')

.factory('Authmethod', function($http, $cookies, ConfigService, $interval) {
.factory('Authmethod', function($http, $cookies, ConfigService, $interval, $location) {
var backendUrl = ConfigService.authAPI;
var authId = ConfigService.freeAuthId;
var authmethod = {};
authmethod.captcha_code = null;
authmethod.captcha_image_url = "";
authmethod.captcha_status = "";
authmethod.admin = false;

authmethod.getAuthevent = function() {
var adminId = ConfigService.freeAuthId + '';
var href = $location.path();
var authevent = '';

var adminMatch = href.match(/^\/admin\//);
var boothMatch = href.match(/^\/booth\/([0-9]+)\//);
var electionsMatch = href.match(/^\/elections\/([0-9]+)\//);

if (_.isArray(adminMatch)) {
authevent = adminId;
} else if(_.isArray(boothMatch) && 2 === boothMatch.length) {
authevent = boothMatch[1];
} else if(_.isArray(electionsMatch) && 2 === electionsMatch.length) {
authevent = electionsMatch[1];
}
return authevent;
};

authmethod.isAdmin = function() {
return authmethod.isLoggedIn() && authmethod.admin;
Expand Down Expand Up @@ -245,14 +264,14 @@ angular.module('avRegistration')
return $http.get(backendUrl);
};

authmethod.setAuth = function(auth, isAdmin) {
authmethod.setAuth = function(auth, isAdmin, autheventid) {
authmethod.admin = isAdmin;
$http.defaults.headers.common.Authorization = auth;
if (!authmethod.pingTimeout) {
$interval.cancel(authmethod.pingTimeout);
authmethod.launchPingDaemon();
authmethod.launchPingDaemon(autheventid);
authmethod.pingTimeout = $interval(
function() { authmethod.launchPingDaemon(); },
function() { authmethod.launchPingDaemon(autheventid); },
ConfigService.timeoutSeconds*500 // ms * 500 mean seconds * 1/2
);
}
Expand Down Expand Up @@ -311,15 +330,16 @@ angular.module('avRegistration')
return $http.post(url, data);
};

authmethod.launchPingDaemon = function() {
authmethod.launchPingDaemon = function(autheventid) {
var postfix = "_authevent_" + autheventid;
// only needed if it's an admin and daemon has not been launched
if (!$cookies.isAdmin) {
if (!$cookies["isAdmin" + postfix]) {
return;
}
authmethod.ping()
.success(function(data) {
$cookies.auth = data['auth-token'];
authmethod.setAuth($cookies.auth, $cookies.isAdmin);
$cookies["auth" + postfix] = data['auth-token'];
authmethod.setAuth($cookies["auth" + postfix], $cookies["isAdmin" + postfix], autheventid);
});
};

Expand Down
19 changes: 10 additions & 9 deletions avRegistration/login-directive/login-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ angular.module('avRegistration')
scope.orgName = ConfigService.organization.orgName;

// redirect from admin login to admin elections if login is not needed
if ($cookies.authevent && $cookies.authevent === adminId &&
autheventid === adminId)
if (!!$cookies["authevent_" + adminId] && $cookies["authevent_" + adminId] === adminId &&
autheventid === adminId && !!$cookies["auth_authevent_" + adminId])
{
$window.location.href = '/admin/elections';
}
Expand Down Expand Up @@ -125,16 +125,17 @@ angular.module('avRegistration')
.success(function(rcvData) {
if (rcvData.status === "ok") {
scope.khmac = rcvData.khmac;
$cookies.authevent = autheventid;
$cookies.userid = rcvData.username;
$cookies.user = scope.email;
$cookies.auth = rcvData['auth-token'];
$cookies.isAdmin = scope.isAdmin;
Authmethod.setAuth($cookies.auth, scope.isAdmin);
var postfix = "_authevent_" + autheventid;
$cookies["authevent_" + autheventid] = autheventid;
$cookies["userid" + postfix] = rcvData.username;
$cookies["user" + postfix] = scope.email;
$cookies["auth" + postfix] = rcvData['auth-token'];
$cookies["isAdmin" + postfix] = scope.isAdmin;
Authmethod.setAuth($cookies["auth" + postfix], scope.isAdmin, autheventid);
if (scope.isAdmin)
{
Authmethod.getUserInfo().success(function(d) {
$cookies.user = d.email;
$cookies["user" + postfix] = d.email;
$window.location.href = '/admin/elections';
}).error(function(error) {
$window.location.href = '/admin/elections';
Expand Down
17 changes: 9 additions & 8 deletions avRegistration/logout-controller/logout-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,19 @@
**/

angular.module('avRegistration').controller('LogoutController',
function($scope, $stateParams, $filter, ConfigService, $i18next, $state, $cookies) {
function($scope, $stateParams, $filter, ConfigService, $i18next, $state, $cookies, Authmethod) {
var adminId = ConfigService.freeAuthId;
var authevent = $cookies.authevent;
$cookies.user = '';
$cookies.auth = '';
$cookies.authevent = '';
$cookies.userid = '';
$cookies.isAdmin = false;
var authevent = Authmethod.getAuthevent();
var postfix = "_authevent_" + authevent;
$cookies["user" + postfix] = '';
$cookies["auth" + postfix] = '';
$cookies["authevent_" + authevent] = '';
$cookies["userid" + postfix] = '';
$cookies["isAdmin" + postfix] = false;
if (authevent === ConfigService.freeAuthId + '' || !authevent) {
$state.go("admin.login");
} else {
$state.go("registration.login", {id: $cookies.authevent});
$state.go("registration.login", {id: $cookies["authevent_" + authevent]});
}
}
);
61 changes: 35 additions & 26 deletions dist/appCommon-v103111.1.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
angular.module("avRegistration", [ "ui.bootstrap", "ui.utils", "ui.router" ]), angular.module("avRegistration").config(function() {}),
angular.module("avRegistration").factory("Authmethod", [ "$http", "$cookies", "ConfigService", "$interval", function($http, $cookies, ConfigService, $interval) {
angular.module("avRegistration").factory("Authmethod", [ "$http", "$cookies", "ConfigService", "$interval", "$location", function($http, $cookies, ConfigService, $interval, $location) {
var backendUrl = ConfigService.authAPI, authId = ConfigService.freeAuthId, authmethod = {};
return authmethod.captcha_code = null, authmethod.captcha_image_url = "", authmethod.captcha_status = "",
authmethod.admin = !1, authmethod.isAdmin = function() {
authmethod.admin = !1, authmethod.getAuthevent = function() {
var adminId = ConfigService.freeAuthId + "", href = $location.path(), authevent = "", adminMatch = href.match(/^\/admin\//), boothMatch = href.match(/^\/booth\/([0-9]+)\//), electionsMatch = href.match(/^\/elections\/([0-9]+)\//);
return _.isArray(adminMatch) ? authevent = adminId : _.isArray(boothMatch) && 2 === boothMatch.length ? authevent = boothMatch[1] : _.isArray(electionsMatch) && 2 === electionsMatch.length && (authevent = electionsMatch[1]),
authevent;
}, authmethod.isAdmin = function() {
return authmethod.isLoggedIn() && authmethod.admin;
}, authmethod.isLoggedIn = function() {
var auth = $http.defaults.headers.common.Authorization;
Expand Down Expand Up @@ -140,11 +144,11 @@ angular.module("avRegistration").factory("Authmethod", [ "$http", "$cookies", "C
});
}, authmethod.test = function() {
return $http.get(backendUrl);
}, authmethod.setAuth = function(auth, isAdmin) {
}, authmethod.setAuth = function(auth, isAdmin, autheventid) {
return authmethod.admin = isAdmin, $http.defaults.headers.common.Authorization = auth,
authmethod.pingTimeout || ($interval.cancel(authmethod.pingTimeout), authmethod.launchPingDaemon(),
authmethod.pingTimeout || ($interval.cancel(authmethod.pingTimeout), authmethod.launchPingDaemon(autheventid),
authmethod.pingTimeout = $interval(function() {
authmethod.launchPingDaemon();
authmethod.launchPingDaemon(autheventid);
}, 500 * ConfigService.timeoutSeconds)), !1;
}, authmethod.electionsIds = function(page) {
return page || (page = 1), $http.get(backendUrl + "acl/mine/?object_type=AuthEvent&perm=edit|view&order=-pk&page=" + page);
Expand All @@ -171,17 +175,18 @@ angular.module("avRegistration").factory("Authmethod", [ "$http", "$cookies", "C
}, authmethod.changeAuthEvent = function(eid, st) {
var url = backendUrl + "auth-event/" + eid + "/" + st + "/", data = {};
return $http.post(url, data);
}, authmethod.launchPingDaemon = function() {
$cookies.isAdmin && authmethod.ping().success(function(data) {
$cookies.auth = data["auth-token"], authmethod.setAuth($cookies.auth, $cookies.isAdmin);
}, authmethod.launchPingDaemon = function(autheventid) {
var postfix = "_authevent_" + autheventid;
$cookies["isAdmin" + postfix] && authmethod.ping().success(function(data) {
$cookies["auth" + postfix] = data["auth-token"], authmethod.setAuth($cookies["auth" + postfix], $cookies["isAdmin" + postfix], autheventid);
});
}, authmethod;
} ]), angular.module("avRegistration").controller("LoginController", [ "$scope", "$stateParams", "$filter", "ConfigService", "$i18next", function($scope, $stateParams, $filter, ConfigService, $i18next) {
$scope.event_id = $stateParams.id, $scope.code = $stateParams.code, $scope.email = $stateParams.email;
} ]), angular.module("avRegistration").directive("avLogin", [ "Authmethod", "StateDataService", "$parse", "$state", "$cookies", "$i18next", "$window", "$timeout", "ConfigService", function(Authmethod, StateDataService, $parse, $state, $cookies, $i18next, $window, $timeout, ConfigService) {
function link(scope, element, attrs) {
var adminId = ConfigService.freeAuthId + "", autheventid = attrs.eventId;
scope.orgName = ConfigService.organization.orgName, $cookies.authevent && $cookies.authevent === adminId && autheventid === adminId && ($window.location.href = "/admin/elections"),
scope.orgName = ConfigService.organization.orgName, $cookies["authevent_" + adminId] && $cookies["authevent_" + adminId] === adminId && autheventid === adminId && $cookies["auth_authevent_" + adminId] && ($window.location.href = "/admin/elections"),
scope.sendingData = !1, scope.currentFormStep = 0, scope.stateData = StateDataService.getData(),
scope.code = null, attrs.code && attrs.code.length > 0 && (scope.code = attrs.code),
scope.email = null, attrs.email && attrs.email.length > 0 && (scope.email = attrs.email),
Expand All @@ -207,19 +212,23 @@ angular.module("avRegistration").factory("Authmethod", [ "$http", "$cookies", "C
"email" === field.name ? scope.email = field.value : "code" === field.name && (field.value = field.value.trim().replace(/ |\n|\t|-|_/g, "").toUpperCase()),
data[field.name] = field.value;
}), scope.sendingData = !0, Authmethod.login(data, autheventid).success(function(rcvData) {
"ok" === rcvData.status ? (scope.khmac = rcvData.khmac, $cookies.authevent = autheventid,
$cookies.userid = rcvData.username, $cookies.user = scope.email, $cookies.auth = rcvData["auth-token"],
$cookies.isAdmin = scope.isAdmin, Authmethod.setAuth($cookies.auth, scope.isAdmin),
scope.isAdmin ? Authmethod.getUserInfo().success(function(d) {
$cookies.user = d.email, $window.location.href = "/admin/elections";
}).error(function(error) {
$window.location.href = "/admin/elections";
}) : angular.isDefined(rcvData["redirect-to-url"]) ? $window.location.href = rcvData["redirect-to-url"] : Authmethod.getPerm("vote", "AuthEvent", autheventid).success(function(rcvData2) {
var khmac = rcvData2["permission-token"], path = khmac.split(";")[1], hash = path.split("/")[0], msg = path.split("/")[1];
$window.location.href = "/booth/" + autheventid + "/vote/" + hash + "/" + msg;
})) : (scope.sendingData = !1, scope.status = "Not found", scope.error = $i18next("avRegistration.invalidCredentials", {
if ("ok" === rcvData.status) {
scope.khmac = rcvData.khmac;
var postfix = "_authevent_" + autheventid;
$cookies["authevent_" + autheventid] = autheventid, $cookies["userid" + postfix] = rcvData.username,
$cookies["user" + postfix] = scope.email, $cookies["auth" + postfix] = rcvData["auth-token"],
$cookies["isAdmin" + postfix] = scope.isAdmin, Authmethod.setAuth($cookies["auth" + postfix], scope.isAdmin, autheventid),
scope.isAdmin ? Authmethod.getUserInfo().success(function(d) {
$cookies["user" + postfix] = d.email, $window.location.href = "/admin/elections";
}).error(function(error) {
$window.location.href = "/admin/elections";
}) : angular.isDefined(rcvData["redirect-to-url"]) ? $window.location.href = rcvData["redirect-to-url"] : Authmethod.getPerm("vote", "AuthEvent", autheventid).success(function(rcvData2) {
var khmac = rcvData2["permission-token"], path = khmac.split(";")[1], hash = path.split("/")[0], msg = path.split("/")[1];
$window.location.href = "/booth/" + autheventid + "/vote/" + hash + "/" + msg;
});
} else scope.sendingData = !1, scope.status = "Not found", scope.error = $i18next("avRegistration.invalidCredentials", {
support: ConfigService.contact.email
}));
});
}).error(function(error) {
scope.sendingData = !1, scope.status = "Registration error: " + error.message, scope.error = $i18next("avRegistration.invalidCredentials", {
support: ConfigService.contact.email
Expand Down Expand Up @@ -262,11 +271,11 @@ angular.module("avRegistration").factory("Authmethod", [ "$http", "$cookies", "C
link: link,
templateUrl: "avRegistration/login-directive/login-directive.html"
};
} ]), angular.module("avRegistration").controller("LogoutController", [ "$scope", "$stateParams", "$filter", "ConfigService", "$i18next", "$state", "$cookies", function($scope, $stateParams, $filter, ConfigService, $i18next, $state, $cookies) {
var authevent = (ConfigService.freeAuthId, $cookies.authevent);
$cookies.user = "", $cookies.auth = "", $cookies.authevent = "", $cookies.userid = "",
$cookies.isAdmin = !1, authevent !== ConfigService.freeAuthId + "" && authevent ? $state.go("registration.login", {
id: $cookies.authevent
} ]), angular.module("avRegistration").controller("LogoutController", [ "$scope", "$stateParams", "$filter", "ConfigService", "$i18next", "$state", "$cookies", "Authmethod", function($scope, $stateParams, $filter, ConfigService, $i18next, $state, $cookies, Authmethod) {
var authevent = (ConfigService.freeAuthId, Authmethod.getAuthevent()), postfix = "_authevent_" + authevent;
$cookies["user" + postfix] = "", $cookies["auth" + postfix] = "", $cookies["authevent_" + authevent] = "",
$cookies["userid" + postfix] = "", $cookies["isAdmin" + postfix] = !1, authevent !== ConfigService.freeAuthId + "" && authevent ? $state.go("registration.login", {
id: $cookies["authevent_" + authevent]
}) : $state.go("admin.login");
} ]), angular.module("avRegistration").controller("RegisterController", [ "$scope", "$stateParams", "$filter", "ConfigService", "$i18next", function($scope, $stateParams, $filter, ConfigService, $i18next) {
$scope.event_id = $stateParams.id, $scope.email = $stateParams.email;
Expand Down