-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenservice.go
168 lines (145 loc) · 4.3 KB
/
tokenservice.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package doorman
import (
"bytes"
"crypto/rand"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"image/png"
"time"
"github.com/pquerna/otp"
"github.com/pquerna/otp/totp"
"go.uber.org/zap"
)
const (
toplevelKey = "otp:"
toplevelTmpKey = "tmpotp:"
toplevelTmpToken = "tmptoken:"
randKeyLen = 64
)
type tokenservice struct {
store kvstore
settings StoreSettings
}
type otpRegistration struct {
PrivateKey string `json:"private_key"`
URL string `json:"url"`
}
func newOTP(kv kvstore, sst StoreSettings) *tokenservice {
return &tokenservice{
store: kv,
settings: sst,
}
}
func (otps *tokenservice) validateTempRegistration(log *zap.Logger, issuer, uid, key, token string) error {
val, err := otps.store.GetTTL(log, genKey(toplevelTmpKey, uid, key))
if err != nil {
return err
}
var res otpRegistration
err = json.Unmarshal([]byte(val), &res)
if err != nil {
return err
}
if !totp.Validate(token, res.PrivateKey) {
return fmt.Errorf("illegal token")
}
otps.store.Del(log, genKey(toplevelTmpKey, uid, key))
return otps.store.Put(log, genKey(toplevelKey, uid, issuer), val)
}
func (otps *tokenservice) checkTempRegistration(log *zap.Logger, issuer, uid string) error {
_, err := otps.store.GetTTL(log, genKey(toplevelTmpKey, uid, issuer))
return err
}
func (otps *tokenservice) checkTempToken(log *zap.Logger, issuer, uid string) (string, error) {
return otps.store.GetTTL(log, genKey(toplevelTmpToken, uid, issuer))
}
func (otps *tokenservice) removeTempToken(log *zap.Logger, issuer, uid string) {
otps.store.Del(log, genKey(toplevelTmpToken, uid, issuer))
}
func (otps *tokenservice) newTempToken(log *zap.Logger, issuer, uid, val string, dur time.Duration) error {
return otps.store.PutTTL(log, genKey(toplevelTmpToken, uid, issuer), val, dur)
}
func (otps *tokenservice) hasUser(log *zap.Logger, issuer, uid string) (bool, error) {
_, err := otps.store.Get(log, genKey(toplevelKey, uid, issuer))
if err != nil {
if errors.Is(err, ErrNoKey) {
return false, nil
}
return false, err
}
return true, nil
}
func (otps *tokenservice) validateUser(log *zap.Logger, issuer, uid, token string) (bool, error) {
val, err := otps.store.Get(log, genKey(toplevelKey, uid, issuer))
if err != nil {
if errors.Is(err, ErrNoKey) {
return false, fmt.Errorf("no registration for user: %q", uid)
}
return false, err
}
var res otpRegistration
err = json.Unmarshal([]byte(val), &res)
if err != nil {
return false, err
}
if !totp.Validate(token, res.PrivateKey) {
return false, nil
}
return true, nil
}
func (otps *tokenservice) qrImage(log *zap.Logger, uid, regkey string) (string, error) {
val, err := otps.store.GetTTL(log, genKey(toplevelTmpKey, uid, regkey))
if err != nil {
return "", err
}
var res otpRegistration
err = json.Unmarshal([]byte(val), &res)
if err != nil {
return "", err
}
var buf bytes.Buffer
key, err := otp.NewKeyFromURL(res.URL)
if err != nil {
return "", fmt.Errorf("cannot load key URL: %w", err)
}
img, err := key.Image(200, 200)
if err != nil {
return "", fmt.Errorf("cannot create QR image: %w", err)
}
err = png.Encode(&buf, img)
if err != nil {
return "", fmt.Errorf("cannot encode QR image: %w", err)
}
return base64.StdEncoding.EncodeToString(buf.Bytes()), nil
}
func (otps *tokenservice) newTempRegistration(log *zap.Logger, issuer, uid string) (string, error) {
key := randomKey(randKeyLen)
seckey, err := totp.Generate(totp.GenerateOpts{
Issuer: issuer,
AccountName: uid,
})
if err != nil {
return "", fmt.Errorf("cannot create secret key: %w", err)
}
reg := otpRegistration{
PrivateKey: seckey.Secret(),
URL: seckey.URL(),
}
val, err := json.Marshal(reg)
if err != nil {
return "", fmt.Errorf("cannot marshal to json: %w", err)
}
// store a TTL value only with uid/issuer, so we can detect if this user already has created a tmp-registration
_ = otps.store.PutTTL(log, genKey(toplevelTmpKey, uid, issuer), string(val), time.Duration(otps.settings.OTP.Timeout))
return key, otps.store.PutTTL(log, genKey(toplevelTmpKey, uid, key), string(val), time.Duration(otps.settings.OTP.Timeout))
}
func genKey(prefix, uid, key string) string {
return prefix + uid + ":" + key
}
func randomKey(ln int) string {
buf := make([]byte, ln)
_, _ = rand.Read(buf)
return base64.StdEncoding.EncodeToString(buf)
}