-
Notifications
You must be signed in to change notification settings - Fork 0
/
otp.go
51 lines (45 loc) · 890 Bytes
/
otp.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
43
44
45
46
47
48
49
50
51
package main
import (
"context"
"github.com/google/uuid"
"time"
)
type OTP struct {
Key string
Created time.Time
}
type RetentionMap map[string]OTP
func NewRetentionMap(ctx context.Context, retentionPeriod time.Duration) RetentionMap {
rm := make(RetentionMap)
return rm
}
func (rm RetentionMap) NewOTP() OTP {
o := OTP{
Key: uuid.NewString(),
Created: time.Now(),
}
rm[o.Key] = o
return o
}
func (rm RetentionMap) VerifyOTP(otp string) bool {
if _, ok := rm[otp]; !ok {
return false
}
delete(rm, otp)
return true
}
func (rm RetentionMap) Retention(ctx context.Context, retentionPeriod time.Duration) {
ticker := time.NewTicker(400 * time.Millisecond)
for {
select {
case <-ticker.C:
for _, otp := range rm {
if otp.Created.Add(retentionPeriod).Before(time.Now()) {
delete(rm, otp.Key)
}
}
case <-ctx.Done():
return
}
}
}