-
Notifications
You must be signed in to change notification settings - Fork 29
/
fingerprintsAuth.ts
109 lines (96 loc) · 3.58 KB
/
fingerprintsAuth.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
import { IComponentOptions, IScope } from 'angular';
import { Credential, BioSample } from "@digitalpersona/core";
import { FingerprintsAuth } from '@digitalpersona/authentication';
import { IAuthService, ServiceError } from '@digitalpersona/services';
import { FingerprintReader, QualityCode, ErrorOccurred } from '@digitalpersona/devices';
import { TokenAuth, Success } from '../tokenAuth';
import template from './fingerprintsAuth.html';
export default class FingerprintsAuthControl extends TokenAuth
{
public static readonly Component: IComponentOptions = {
...TokenAuth.Component,
template,
controller: FingerprintsAuthControl,
bindings: {
...TokenAuth.Component.bindings,
reader: "<",
},
};
public reader: FingerprintReader;
private isReaderConnected: boolean = false;
public static $inject = ["$scope", "AuthService"];
constructor(
private $scope: IScope,
private authService: IAuthService,
){
super(Credential.Fingerprints);
}
public $onInit() {
this.reader.onDeviceConnected = async (device) => {
await this.updateReaderStatus();
this.$scope.$applyAsync();
};
this.reader.onDeviceDisconnected = async (device) => {
await this.updateReaderStatus();
this.$scope.$applyAsync();
};
this.reader.onQualityReported = (quality) => {
this.updateSampleQuality(quality.quality);
this.$scope.$applyAsync();
};
this.reader.onSamplesAcquired = async (data) => {
await this.submit(data.samples);
this.$scope.$applyAsync();
};
this.reader.onErrorOccurred = (reason) => {
super.notify(new Error(this.mapDeviceError(reason)));
this.$scope.$applyAsync();
};
}
public $onDestroy() {
this.reader.off();
}
private async updateReaderStatus() {
try {
const devices = await this.reader.enumerateDevices();
this.isReaderConnected = devices.length > 0;
} catch (err) {
this.isReaderConnected = false;
super.notify(new Error(this.mapDeviceError(err)));
}
}
private updateSampleQuality(quality: QualityCode) {
super.notify(quality !== QualityCode.Good
? new Error(`Fingerprints.QualityCode.${QualityCode[quality]}`)
: undefined);
}
public async submit(samples: BioSample[]) {
super.emitOnBusy();
try {
const auth = new FingerprintsAuth(this.authService);
const token = await (this.user.name
? auth.authenticate(this.identity, samples)
: auth.identify(samples));
super.emitOnToken(token);
super.notify(new Success('Fingerprints.Auth.Success'));
}
catch (error) {
super.notify(new Error(this.mapServiceError(error)));
}
}
protected mapServiceError(error: ServiceError): string {
switch (error.code) {
case -2146893044:
case -2147023652:
return "Fingerprints.Auth.Error.NoMatch";
case -2146893042:
return "Fingerprints.Auth.Error.NotEnrolled";
default: return super.mapServiceError(error);
}
}
private mapDeviceError(reason: ErrorOccurred) {
switch (reason.error) {
default: return (reason.error >>> 0).toString(16);
}
}
}