-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtouchid.go
42 lines (36 loc) · 814 Bytes
/
touchid.go
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
package touchid
import (
"errors"
"sync"
"time"
)
type DeviceType string
const (
DeviceTypeAny DeviceType = "any"
DeviceTypeBiometrics DeviceType = "biometrics"
)
var serialLock sync.Mutex
var lastAuthTime time.Time
func SerialAuth(dType DeviceType, reason string, timeout time.Duration) (bool, error) {
serialLock.Lock()
defer serialLock.Unlock()
if lastAuthTime.Add(timeout).After(time.Now()) {
return true, nil
} else {
ok, err := Auth(dType, reason)
if ok && err == nil {
lastAuthTime = time.Now()
}
return ok, err
}
}
func Auth(dType DeviceType, reason string) (bool, error) {
switch dType {
case DeviceTypeAny:
return Authenticate(1, reason)
case DeviceTypeBiometrics:
return Authenticate(2, reason)
default:
return false, errors.New("invalid device type")
}
}