This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
/
pin-code.ts
220 lines (183 loc) · 6.09 KB
/
pin-code.ts
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
220
import { Component, NgZone, OnDestroy, OnInit } from '@angular/core';
import { Platform, NavController, NavParams, ModalController, AlertController } from '@ionic/angular';
import { Vibration } from '@ionic-native/vibration/ngx';
import { AuthProvider } from '@/services/auth/auth';
import { Subscription, timer } from 'rxjs';
import { TranslateService } from '@ngx-translate/core';
import moment from 'moment';
import lodash from 'lodash';
import * as constants from '@/app/app.constants';
import { map, takeWhile, finalize } from 'rxjs/operators';
@Component({
selector: 'modal-pin-code',
templateUrl: 'pin-code.html',
styleUrls: ['pin-code.pcss'],
providers: [Vibration],
})
export class PinCodeModal implements OnInit, OnDestroy {
public message: string;
public password: string;
public isWrong = false;
public unlockDiff = 0;
public unlockCountdown$: Subscription;
// Send a password created before, useful for create pin and confirm
private expectedPassword: string;
// Will send back the entered password
private outputPassword = false;
// Check if the entered password is correct
private validatePassword = false;
private length = 6;
private attempts = 0;
constructor(
public platform: Platform,
public navCtrl: NavController,
public navParams: NavParams,
public modalCtrl: ModalController,
private authProvider: AuthProvider,
private zone: NgZone,
private vibration: Vibration,
private alertCtrl: AlertController,
private translateService: TranslateService,
) {
this.password = '';
this.message = this.navParams.get('message');
this.expectedPassword = this.navParams.get('expectedPassword');
this.outputPassword = this.navParams.get('outputPassword') || false;
this.validatePassword = this.navParams.get('validatePassword') || false;
}
add(value: number) {
if (this.unlockDiff > 0) {
return;
}
if (this.password.length < this.length) {
this.zone.run(() => {
this.password = this.password + value;
});
// When the user reach the password length
if (this.password.length === this.length) {
// New password
if (!this.expectedPassword && !this.validatePassword) {
if (this.authProvider.isWeakPassword(this.password) ) {
// Show message about weak PIN
this.translateService.get([
'PIN_CODE.WEAK_PIN',
'PIN_CODE.WEAK_PIN_DETAIL',
'NO',
'YES'
]).subscribe(async (translation) => {
const alert = await this.alertCtrl.create({
header: translation['PIN_CODE.WEAK_PIN'],
message: translation['PIN_CODE.WEAK_PIN_DETAIL'],
buttons: [{
text: translation.NO,
handler: () => {
this.password = '';
}
}, {
text: translation.YES,
handler: () => {
this.dismiss(true);
}
}]
});
alert.present();
});
} else {
return this.dismiss(true);
}
}
// Confirm with the previous entered password (validate new password)
if (this.expectedPassword) {
if (this.expectedPassword !== this.password) {
this.setWrong();
} else {
this.dismiss(true);
}
}
// Compare the password entered with the saved in the storage
if (this.validatePassword) {
this.authProvider.validateMasterPassword(this.password).subscribe((result) => {
if (!result) {
this.setWrong();
} else {
this.dismiss(true);
}
});
}
}
}
}
verifyAttempts() {
if (this.attempts >= constants.PIN_ATTEMPTS_LIMIT) {
this.authProvider.increaseUnlockTimestamp().then(() => this.loadUnlockTime());
}
}
setWrong() {
this.vibration.vibrate(constants.VIBRATION_TIME_LONG_MS);
this.authProvider.increaseAttempts().subscribe((newAttempts) => {
this.attempts = newAttempts;
this.verifyAttempts();
this.zone.run(() => {
this.isWrong = true;
this.password = '';
this.message = 'PIN_CODE.WRONG';
setTimeout(() => this.isWrong = false, 500);
});
});
}
delete() {
if (this.password.length > 0) {
this.zone.run(() => {
this.password = this.password.substring(0, this.password.length - 1);
});
}
}
dismiss(status: boolean = true) {
if (!status) {
return this.modalCtrl.dismiss();
}
// When logged in, the attempts are restarted
if (status) {
this.authProvider.clearAttempts();
}
if (this.outputPassword) {
this.modalCtrl.dismiss(this.password);
return;
}
this.modalCtrl.dismiss(status);
}
private loadUnlockTime() {
this.authProvider.getUnlockTimestamp().subscribe((timestamp) => {
if (!timestamp || lodash.isEmpty(timestamp)) {
return;
}
// If an unlock time is set in storage
// Check if this time has already been spent
const now = moment.now();
const diff = moment(timestamp).diff(now, 'seconds');
if (diff <= 0) {
this.authProvider.clearAttempts();
this.attempts = 0;
return this.loadUnlockTime();
}
this.unlockDiff = diff;
this.password = '';
this.message = 'PIN_CODE.WRONG_PIN_MANY_TIMES';
// Start the countdown
this.unlockCountdown$ = timer(0, 1000).pipe(
map(x => this.zone.run(() => this.unlockDiff--)),
takeWhile(() => this.unlockDiff > 0),
finalize(() => this.zone.run(() => this.message = 'PIN_CODE.DEFAULT_MESSAGE'))
).subscribe()
});
}
ngOnInit() {
this.authProvider.getAttempts().subscribe((attempts) => this.attempts = attempts);
this.loadUnlockTime();
}
ngOnDestroy() {
if (this.unlockCountdown$) {
this.unlockCountdown$.unsubscribe();
}
}
}