Repository navigation
Expand file tree
/
Copy pathvue.app.html
More file actions
219 lines (214 loc) · 9.2 KB
/
Copy pathvue.app.html
File metadata and controls
219 lines (214 loc) · 9.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<!DOCTYPE>
<html>
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" type="text/css">
</head>
<body>
<div id="app" class="container">
<div class="row">
<h1>Welcome to CSI</h1>
<p>
<router-link to="/login">Go to login page</router-link>
</p>
<router-view></router-view>
</div>
</div>
</body>
<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/vue-router/dist/vue-router.js"></script>
<script src="https://cdn.jsdelivr.net/npm/vue-resource@1.3.4"></script>
<script>
const NotFound = { template: '<p>Page not found</p>' }
const Login = {
template: `
<div class="col-md-4 col-md-offset-4">
<form>
<div class="form-group">
<label for="email">Email address:</label>
<input v-model="email" type="email" class="form-control" id="email">
</div>
<div class="form-group">
<label for="pwd">Password:</label>
<input v-model="password" type="password" class="form-control" id="pwd">
</div>
<div class="checkbox">
<label><input type="checkbox"> Remember me</label>
</div>
<button v-on:click="login(email, password)" class="btn btn-default">Login</button>
</form>
</div>
`,
methods: {
login: function(email, password){
localStorage.email = email;
localStorage.password = password;
this.$http.post('/login', { email: email, password: password}).then( response =>{
if(response.status === 206){
return router.push('otp');
} else if(response.status === 200) {
localStorage.clear();
localStorage.loggedin = true;
return router.push('setup');
}
}).catch(err => {
alert("Invalid username or password");
});
}
},
data: function(){
return {
email: "Mike.doe@gmail.com",
password: "test1234"
}
}
}
const Otp = {
template: `
<div class="col-md-4 col-md-offset-4">
<form>
<div class="form-group">
<label for="otp">Enter the secret key:</label>
<input v-model="otp" type="otp" class="form-control" id="otp">
</div>
<button v-on:click="login(otp)" class="btn btn-default">Submit</button>
</form>
</div>
` ,
data: function(){
return {
otp: ""
}
},
methods: {
login: function(otp){
const options = {
headers: {
['x-otp']: otp
}
}
const payload = {
email: localStorage.email,
password: localStorage.password
}
this.$http.post('/login', payload, options).then((response)=>{
if(response.status === 200){
localStorage.clear();
localStorage.loggedin = true;
return router.push('setup');
}
alert('Invalid username or password');
}).catch(err => {
alert("Invalid username or password");
});
}
}
}
const Setup = {
template: `
<div>
<div class="col-md-4 col-md-offset-4" v-if="twofactor.secret">
<h3>Current Settings</h3>
<img :src="twofactor.dataURL" alt="..." class="img-thumbnail">
<p>Secret - {{twofactor.secret || twofactor.tempSecret}}</p>
<p>Type - TOTP</p>
</div>
<div class="col-md-4 col-md-offset-4" v-if="!twofactor.secret">
<h3>Get QR scan code</h3>
<div>
<button v-on:click="setup()" class="btn btn-default">Enable</button>
</div>
<span v-if="!!twofactor.tempSecret">
<p>Scan the QR code or enter the secret key underneath the QR code in Google Authenticator</p>
<img :src="twofactor.dataURL" alt="..." class="img-thumbnail">
<p>Secret - {{twofactor.tempSecret}}</p>
<p>Type - TOTP</p>
<form>
<div class="form-group">
<label for="otp">Enter the secret key:</label>
<input v-model="otp" type="otp" class="form-control" id="otp">
</div>
<button v-on:click="confirm(otp)" class="btn btn-default">Confirm</button>
</form>
</span>
</div>
<div class="col-md-1">
<h3>Log out</h3>
<form>
<button v-on:click="disable()" class="btn btn-danger">Log out</button>
</form>
</div>
</div>
`,
methods: {
/** setup two factor authentication*/
setup: function(){
this.$http.post('/twofactor/setup', {}).then(response => {
const result = response.body;
if(response.status === 200){
console.log(result);
alert(result.message);
this.twofactor = result;
}
});
},
/** Verify the otp once to enable 2fa*/
confirm: function(otp){
const body = {
token: otp
}
this.$http.post('/twofactor/verify', body).then(response => {
const result = response.body;
if(response.status === 200){
this.twofactor.secret = this.twofactor.tempSecret;
this.twofactor.tempSecret = "";
window.location = 'jsoneditor.html'
}
}).catch(err=>alert('Invalid code/secret key'));
},
/** disable 2fa */
disable: function(){
this.$http.delete('/twofactor/setup').then(response => {
const result = response.body;
if(response.status === 200){
router.push('login');
window.location = 'vue.app.html'
}
}).catch(err => alert('error occured'));
}
},
data: function(){
return {
twofactor: {
secret: "",
tempSecret: ""
},
otp: ""
}
},
/** when component is created check if 2fa is enabled*/
created: function(){
this.$http.get('/twofactor/setup').then(response => {
const result = response.body;
if(response.status === 200 && !!result.secret){
this.twofactor = result
}
}).catch((err)=>{
if(err.status === 401){
router.push('login');
}
});
}
}
const routes = [
{ path: '/login', component: Login },
{ path: '/otp', component: Otp },
{ path: '/setup', component: Setup }
];
const router = new VueRouter({
routes // short for `routes: routes`
});
const app = new Vue({
router
}).$mount('#app');
</script>
</html>