1 | /** |
||
2 | * Nextcloud - passman |
||
3 | * |
||
4 | * @copyright Copyright (c) 2016, Sander Brand ([email protected]) |
||
5 | * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected]) |
||
6 | * @license GNU AGPL version 3 or any later version |
||
7 | * |
||
8 | * This program is free software: you can redistribute it and/or modify |
||
9 | * it under the terms of the GNU Affero General Public License as |
||
10 | * published by the Free Software Foundation, either version 3 of the |
||
11 | * License, or (at your option) any later version. |
||
12 | * |
||
13 | * This program is distributed in the hope that it will be useful, |
||
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
16 | * GNU Affero General Public License for more details. |
||
17 | * |
||
18 | * You should have received a copy of the GNU Affero General Public License |
||
19 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
||
20 | * |
||
21 | */ |
||
22 | |||
23 | (function () { |
||
24 | 'use strict'; |
||
25 | |||
26 | /** |
||
27 | * @ngdoc directive |
||
28 | * @name passmanApp.directive:passwordGen |
||
29 | * @description |
||
30 | * # passwordGen |
||
31 | */ |
||
32 | angular.module('passmanExtension') |
||
33 | .directive('otpGenerator', ['$compile', '$timeout', |
||
34 | function ($compile, $timeout) { |
||
35 | function dec2hex(s) { |
||
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
36 | return (s < 15.5 ? '0' : '') + Math.round(s).toString(16); |
||
37 | } |
||
38 | |||
39 | function hex2dec(s) { |
||
0 ignored issues
–
show
|
|||
40 | return parseInt(s, 16); |
||
41 | } |
||
42 | |||
43 | function base32tohex(base32) { |
||
0 ignored issues
–
show
|
|||
44 | if (!base32) { |
||
45 | return; |
||
46 | } |
||
47 | var base32chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567"; |
||
48 | var bits = ""; |
||
49 | var hex = ""; |
||
50 | var i; |
||
51 | for (i = 0; i < base32.length; i++) { |
||
52 | var val = base32chars.indexOf(base32.charAt(i).toUpperCase()); |
||
53 | bits += leftpad(val.toString(2), 5, '0'); |
||
54 | } |
||
55 | |||
56 | for (i = 0; i + 4 <= bits.length; i += 4) { |
||
57 | var chunk = bits.substr(i, 4); |
||
58 | hex = hex + parseInt(chunk, 2).toString(16); |
||
59 | } |
||
60 | return hex; |
||
61 | |||
62 | } |
||
63 | |||
64 | function leftpad(str, len, pad) { |
||
65 | if (len + 1 >= str.length) { |
||
66 | str = Array(len + 1 - str.length).join(pad) + str; |
||
67 | } |
||
68 | return str; |
||
69 | } |
||
70 | |||
71 | return { |
||
72 | restrict: 'A', |
||
73 | // template: '<span class="otp_generator">{{otp}} <span ng-bind="timeleft"></span></span>', |
||
74 | template: '<span class="otp_generator"><span class="code">{{otp}}</span> <copy-text text="otp"></copy-text> <div class="radial-progress"><div class="circle"><div class="mask full"><div class="fill"></div></div><div class="mask half"><div class="fill"></div><div class="fill fix"></div></div></div></div></span>', |
||
75 | transclude: false, |
||
76 | scope: { |
||
77 | secret: '=' |
||
78 | }, |
||
79 | replace: true, |
||
80 | link: function (scope, el) { |
||
81 | scope.otp = null; |
||
82 | scope.timeleft = null; |
||
83 | scope.timer = null; |
||
84 | var updateOtp = function () { |
||
85 | if (!scope.secret) { |
||
86 | return; |
||
87 | } |
||
88 | window.OTP.secret = scope.secret; |
||
89 | scope.otp = window.OTP.getOTP(); |
||
90 | }; |
||
91 | |||
92 | var transform_styles = ['-webkit-transform', '-ms-transform', 'transform']; |
||
93 | var timer = function () { |
||
94 | var epoch = Math.round(new Date().getTime() / 1000.0); |
||
95 | var countDown = 30 - (epoch % 30); |
||
96 | if (epoch % 30 === 0) { |
||
97 | updateOtp(); |
||
98 | } |
||
99 | |||
100 | var percent = (countDown / 30) * 100 / 100; |
||
101 | for (var i = 0; i < transform_styles.length; i++) { |
||
102 | var rotation = percent * 180; |
||
103 | var fill_rotation = rotation; |
||
104 | var fix_rotation = rotation * 2; |
||
105 | |||
106 | $(el).find('.circle .fill, .circle .mask.full').css(transform_styles[i], 'rotate(' + fill_rotation + 'deg)'); |
||
107 | $(el).find('.circle .fill.fix').css(transform_styles[i], 'rotate(' + fix_rotation + 'deg)'); |
||
108 | |||
109 | } |
||
110 | |||
111 | scope.timeleft = countDown; |
||
112 | scope.timer = $timeout(timer, 100); |
||
113 | |||
114 | }; |
||
115 | scope.$watch("secret", function (n) { |
||
116 | if (n) { |
||
117 | $timeout.cancel(scope.timer); |
||
118 | updateOtp(); |
||
119 | timer(); |
||
120 | } else { |
||
121 | $timeout.cancel(scope.timer); |
||
122 | } |
||
123 | }, true); |
||
124 | scope.$on( |
||
125 | "$destroy", |
||
126 | function () { |
||
127 | $timeout.cancel(scope.timer); |
||
128 | } |
||
129 | ); |
||
130 | } |
||
131 | }; |
||
132 | } |
||
133 | ]); |
||
134 | }()); |