passman/js/app/directives/otp.js

105 lines
2.9 KiB
JavaScript
Raw Normal View History

2016-10-19 23:44:19 +08:00
/**
* Nextcloud - passman
*
* @copyright Copyright (c) 2016, Sander Brand (brantje@gmail.com)
* @copyright Copyright (c) 2016, Marcos Zuriaga Miguel (wolfi@wolfi.es)
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
2016-10-08 01:56:29 +08:00
(function () {
'use strict';
2016-09-14 05:03:12 +08:00
2016-10-08 01:56:29 +08:00
/**
* @ngdoc directive
* @name passmanApp.directive:passwordGen
* @description
* # passwordGen
*/
angular.module('passmanApp')
.directive('otpGenerator', ['$compile', '$interval',
function ($compile, $interval) {
function mergeDefaultOTPConfig(otp) {
const defaults = {
algorithm: "SHA1",
period: 30,
digits: 6,
};
2016-09-14 05:03:12 +08:00
for (const key in defaults) {
if (otp[key] === undefined || otp[key] == null) {
otp[key] = defaults[key];
}
2016-10-08 01:56:29 +08:00
}
}
2016-09-14 05:03:12 +08:00
2016-10-08 01:56:29 +08:00
return {
restrict: 'A',
template: '<span class="otp_generator"><span credential-field value="token" secret="\'true\'"></span> <span ng-bind="timeleft"></span></span>',
2016-10-08 01:56:29 +08:00
transclude: false,
scope: {
otp: '='
2016-10-08 01:56:29 +08:00
},
replace: true,
2016-10-13 02:12:28 +08:00
link: function (scope) {
scope.token = null;
2016-10-08 01:56:29 +08:00
scope.timeleft = null;
scope.timer = null;
var updateOtp = function () {
if (!scope.otp || !scope.otp.secret || scope.otp.secret === "") {
2016-10-08 01:56:29 +08:00
return;
}
if (scope.otp.secret.includes(' ')) {
scope.otp.secret = scope.otp.secret.replaceAll(' ', '');
}
mergeDefaultOTPConfig(scope.otp);
var totp = new OTPAuth.TOTP({
issuer: scope.otp.issuer,
label: scope.otp.label,
algorithm: scope.otp.algorithm,
digits: scope.otp.digits,
period: scope.otp.period,
secret: scope.otp.secret
});
scope.token = totp.generate();
2016-10-08 01:56:29 +08:00
};
var timer = function () {
if (scope.otp) {
var epoch = Math.round(new Date().getTime() / 1000.0);
scope.timeleft = scope.otp.period - (epoch % scope.otp.period);
if (epoch % scope.otp.period === 1) updateOtp();
}
2016-10-08 01:56:29 +08:00
};
scope.$watch("otp", function (n) {
2016-10-08 01:56:29 +08:00
if (n) {
$interval.cancel(scope.timer);
2016-10-08 01:56:29 +08:00
updateOtp();
scope.timer = $interval(timer, 1000);
2016-10-08 01:56:29 +08:00
}
}, true);
scope.$on(
"$destroy",
2016-10-13 02:12:28 +08:00
function () {
$interval.cancel(scope.timer);
2016-10-08 01:56:29 +08:00
}
);
}
};
2016-09-14 05:03:12 +08:00
}
2016-10-08 01:56:29 +08:00
]);
}());