-
Notifications
You must be signed in to change notification settings - Fork 258
Expand file tree
/
Copy pathapp.js
More file actions
102 lines (83 loc) · 2.2 KB
/
Copy pathapp.js
File metadata and controls
102 lines (83 loc) · 2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
function getCurrentSeconds() {
return Math.round(new Date().getTime() / 1000.0);
}
function stripSpaces(str) {
return str.replace(/\s/g, '');
}
function truncateTo(str, digits) {
if (str.length <= digits) {
return str;
}
return str.slice(-digits);
}
function parseURLSearch(search) {
const queryParams = search.substr(1).split('&').reduce(function (q, query) {
const chunks = query.split('=');
const key = chunks[0];
let value = decodeURIComponent(chunks[1]);
value = isNaN(Number(value)) ? value : Number(value);
return (q[key] = value, q);
}, {});
return queryParams;
}
const app = Vue.createApp({
data() {
return {
secret_key: 'JBSWY3DPEHPK3PXP',
digits: 6,
period: 30,
algorithm: 'SHA1',
updatingIn: 30,
clipboardButton: null,
};
},
mounted: function () {
this.getKeyFromUrl();
this.getQueryParameters()
this.update();
this.intervalHandle = setInterval(this.update, 1000);
this.clipboardButton = new ClipboardJS('#clipboard-button');
},
destroyed: function () {
clearInterval(this.intervalHandle);
},
computed: {
token: function () {
const totp = new OTPAuth.TOTP({
algorithm: this.algorithm,
digits: this.digits,
period: this.period,
secret: OTPAuth.Secret.fromBase32(stripSpaces(this.secret_key)),
});
return truncateTo(totp.generate(), this.digits);
},
},
methods: {
update: function () {
this.updatingIn = this.period - (getCurrentSeconds() % this.period);
this.$forceUpdate();
},
getKeyFromUrl: function () {
const key = document.location.hash.replace(/[#\/]+/, '');
if (key.length > 0) {
this.secret_key = key;
}
},
getQueryParameters: function () {
const queryParams = parseURLSearch(window.location.search);
if (queryParams.key) {
this.secret_key = queryParams.key;
}
if (queryParams.digits) {
this.digits = queryParams.digits;
}
if (queryParams.period) {
this.period = queryParams.period;
}
if (queryParams.algorithm) {
this.algorithm = queryParams.algorithm;
}
}
}
});
app.mount('#app');