-
Notifications
You must be signed in to change notification settings - Fork 0
/
verified_store.go
executable file
·110 lines (87 loc) · 2.71 KB
/
verified_store.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
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
package campid
import (
"context"
"fmt"
"github.com/influx6/npkg/nerror"
"github.com/influx6/npkg/ntrace"
openTracing "github.com/opentracing/opentracing-go"
"github.com/influx6/npkg/nstorage"
)
const (
verifiedStatus = "verified"
pendingStatus = "pending"
)
type VerifiedShop struct {
SMS TelCode
Email MailCode
Store nstorage.ExpirableStore
}
func NewVerifiedShop(sms TelCode, email MailCode, store nstorage.ExpirableStore) *VerifiedShop {
return &VerifiedShop{
Email: email,
SMS: sms,
Store: store,
}
}
func (ac *VerifiedShop) IsCompleted(ctx context.Context, u *User, d *Device) (bool, error) {
var span openTracing.Span
if ctx, span = ntrace.NewMethodSpanFromContext(ctx); span != nil {
defer span.Finish()
}
var printId = fmt.Sprintf("%s:%s", d.FingerprintId, u.Pid)
var status, getCodeErr = ac.Store.Get(printId)
if getCodeErr != nil {
return false, nerror.Wrap(getCodeErr, "failed to get result from store")
}
return string(status) == verifiedStatus, nil
}
func (ac *VerifiedShop) IsPending(ctx context.Context, u *User, d *Device) (bool, error) {
var span openTracing.Span
if ctx, span = ntrace.NewMethodSpanFromContext(ctx); span != nil {
defer span.Finish()
}
var printId = fmt.Sprintf("%s:%s", d.FingerprintId, u.Pid)
var status, getCodeErr = ac.Store.Get(printId)
if getCodeErr != nil {
return false, nerror.Wrap(getCodeErr, "failed to get result from store")
}
return string(status) == pendingStatus, nil
}
func (ac *VerifiedShop) Initiated(ctx context.Context, u *User, d *Device) error {
var span openTracing.Span
if ctx, span = ntrace.NewMethodSpanFromContext(ctx); span != nil {
defer span.Finish()
}
var printId = fmt.Sprintf("%s:%s", d.FingerprintId, u.Pid)
var status, getCodeErr = ac.Store.Exists(printId)
if getCodeErr != nil {
return nerror.Wrap(getCodeErr, "failed to get result from store")
}
if status {
return nerror.New("verification already initiated")
}
var saveErr = ac.Store.Save(printId, []byte(pendingStatus))
if saveErr != nil {
return nerror.Wrap(saveErr, "failed to save verification status")
}
return nil
}
func (ac *VerifiedShop) Complete(ctx context.Context, u *User, d *Device) error {
var span openTracing.Span
if ctx, span = ntrace.NewMethodSpanFromContext(ctx); span != nil {
defer span.Finish()
}
var printId = fmt.Sprintf("%s:%s", d.FingerprintId, u.Pid)
var status, getCodeErr = ac.Store.Exists(printId)
if getCodeErr != nil {
return nerror.Wrap(getCodeErr, "failed to get result from store")
}
if !status {
return nerror.New("verification not started")
}
var saveErr = ac.Store.Save(printId, []byte(verifiedStatus))
if saveErr != nil {
return nerror.Wrap(saveErr, "failed to save verification status")
}
return nil
}