-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_device_codes.go
executable file
·152 lines (121 loc) · 3.69 KB
/
auth_device_codes.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
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
package campid
import (
"context"
"fmt"
"strings"
"time"
"github.com/influx6/npkg/nxid"
"github.com/influx6/npkg/nerror"
"github.com/influx6/npkg/ntrace"
openTracing "github.com/opentracing/opentracing-go"
"github.com/influx6/npkg/nstorage"
)
type DeviceAuthCodes struct {
SMS TelCode
Email MailCode
TTL time.Duration
Store nstorage.ExpirableStore
}
func NewDeviceAuthCodes(sms TelCode, email MailCode, ttl time.Duration, store nstorage.ExpirableStore) *DeviceAuthCodes {
return &DeviceAuthCodes{
SMS: sms,
Email: email,
TTL: ttl,
Store: store,
}
}
func (ac *DeviceAuthCodes) VerifyCode(ctx context.Context, u *User, d *Device, returnedCode string) error {
var span openTracing.Span
if ctx, span = ntrace.NewMethodSpanFromContext(ctx); span != nil {
defer span.Finish()
}
var storedCode, getCodeErr = ac.Store.Get(u.Pid)
if getCodeErr != nil {
return nerror.Wrap(getCodeErr, "code as expired")
}
var parts = strings.Split(string(storedCode), ":")
if len(parts) < 2 {
return nerror.Wrap(getCodeErr, "invalid code provided")
}
var fingerPrint, deviceCode = parts[0], parts[1]
var providedParts = strings.Split(string(storedCode), ":")
if len(providedParts) < 2 {
return nerror.Wrap(getCodeErr, "invalid code provided")
}
var providedFingerPrint, providedDeviceCode = providedParts[0], providedParts[1]
if deviceCode != providedDeviceCode {
return nerror.New("invalid device code")
}
if fingerPrint != providedFingerPrint {
return nerror.New("invalid device fingerprint")
}
return nil
}
func (ac *DeviceAuthCodes) ExpireCode(ctx context.Context, u *User) error {
var span openTracing.Span
if ctx, span = ntrace.NewMethodSpanFromContext(ctx); span != nil {
defer span.Finish()
}
var _, delErr = ac.Store.Remove(u.Pid)
if delErr != nil {
return nerror.WrapOnly(delErr)
}
return nil
}
func (ac *DeviceAuthCodes) SendEmailCode(ctx context.Context, u *User, d *Device) error {
var span openTracing.Span
if ctx, span = ntrace.NewMethodSpanFromContext(ctx); span != nil {
defer span.Finish()
}
if len(u.Email) == 0 {
return nerror.New("user has no attached email address")
}
var code, getCodeErr = ac.Store.Get(u.Pid)
if getCodeErr == nil {
if err := ac.Email.SendToEmail(ctx, u.Email, string(code)); err != nil {
return nerror.WrapOnly(err)
}
}
code = nxid.New().Bytes()
var deviceCode = fmt.Sprintf("%s:%s", d.FingerprintId, code)
if saveErr := ac.Store.SaveTTL(u.Pid, []byte(deviceCode), ac.TTL); saveErr != nil {
return nerror.WrapOnly(saveErr)
}
if err := ac.Email.SendToEmail(ctx, u.Email, string(code)); err != nil {
return nerror.WrapOnly(err)
}
return nil
}
func (ac *DeviceAuthCodes) SendPhoneCode(ctx context.Context, u *User, d *Device) error {
var span openTracing.Span
if ctx, span = ntrace.NewMethodSpanFromContext(ctx); span != nil {
defer span.Finish()
}
if len(u.Phone) == 0 {
return nerror.New("user has no attached phone number")
}
var code, getCodeErr = ac.Store.Get(u.Pid)
if getCodeErr == nil {
if err := ac.SMS.SendToPhone(ctx, u.Phone, string(code)); err != nil {
return nerror.WrapOnly(err)
}
}
var deviceCode = fmt.Sprintf("%s:%s", d.FingerprintId, code)
if saveErr := ac.Store.SaveTTL(u.Pid, []byte(deviceCode), ac.TTL); saveErr != nil {
return nerror.WrapOnly(saveErr)
}
if err := ac.SMS.SendToPhone(ctx, u.Phone, string(code)); err != nil {
return nerror.WrapOnly(err)
}
return nil
}
func (ac *DeviceAuthCodes) SendCode(ctx context.Context, u *User, d *Device) error {
var span openTracing.Span
if ctx, span = ntrace.NewMethodSpanFromContext(ctx); span != nil {
defer span.Finish()
}
if len(u.Email) != 0 {
return ac.SendEmailCode(ctx, u, d)
}
return ac.SendPhoneCode(ctx, u, d)
}