generated from datumforge/go-template
-
Notifications
You must be signed in to change notification settings - Fork 5
/
urltokens.go
292 lines (229 loc) · 7.29 KB
/
urltokens.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package tokens
import (
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"time"
"github.com/oklog/ulid/v2"
"github.com/vmihailenco/msgpack/v5"
ulids "github.com/datumforge/datum/pkg/utils/ulids"
)
const (
nonceLength = 64
keyLength = 64
expirationDays = 7
resetTokenExpirationMinutes = 15
inviteExpirationDays = 14
)
// NewVerificationToken creates a token struct from an email address that expires
// in 7 days
func NewVerificationToken(email string) (token *VerificationToken, err error) {
if email == "" {
return nil, ErrMissingEmail
}
token = &VerificationToken{
Email: email,
}
if token.SigningInfo, err = NewSigningInfo(time.Hour * 24 * expirationDays); err != nil {
return nil, err
}
return token, nil
}
// VerificationToken packages an email address with random data and an expiration
// time so that it can be serialized and hashed into a token which can be sent to users
type VerificationToken struct {
Email string `msgpack:"email"`
SigningInfo
}
// Sign creates a base64 encoded string from the token data so that it can be sent to
// users as part of a URL. The returned secret should be stored in the database so that
// the string can be recomputed when verifying a user provided token.
func (t *VerificationToken) Sign() (string, []byte, error) {
data, err := msgpack.Marshal(t)
if err != nil {
return "", nil, err
}
return t.signData(data)
}
// Verify checks that a token was signed with the secret and is not expired
func (t *VerificationToken) Verify(signature string, secret []byte) (err error) {
if t.Email == "" {
return ErrTokenMissingEmail
}
if t.IsExpired() {
return ErrTokenExpired
}
if len(secret) != nonceLength+keyLength {
return ErrInvalidSecret
}
// Serialize the struct with the nonce from the secret
t.Nonce = secret[0:nonceLength]
var data []byte
if data, err = msgpack.Marshal(t); err != nil {
return err
}
return t.verifyData(data, signature, secret)
}
// NewResetToken creates a token struct from a user ID that expires in 15 minutes
func NewResetToken(id ulid.ULID) (token *ResetToken, err error) {
if ulids.IsZero(id) {
return nil, ErrMissingUserID
}
token = &ResetToken{
UserID: id,
}
if token.SigningInfo, err = NewSigningInfo(time.Minute * resetTokenExpirationMinutes); err != nil {
return nil, err
}
return token, nil
}
// ResetToken packages a user ID with random data and an expiration time so that it can
// be serialized and hashed into a token which can be sent to users
type ResetToken struct {
UserID ulid.ULID `msgpack:"user_id"`
SigningInfo
}
// Sign creates a base64 encoded string from the token data so that it can be sent to
// users as part of a URL. The returned secret should be stored in the database so that
// the string can be recomputed when verifying a user provided token
func (t *ResetToken) Sign() (string, []byte, error) {
data, err := msgpack.Marshal(t)
if err != nil {
return "", nil, err
}
return t.signData(data)
}
// Verify checks that a token was signed with the secret and is not expired
func (t *ResetToken) Verify(signature string, secret []byte) (err error) {
if ulids.IsZero(t.UserID) {
return ErrTokenMissingUserID
}
if t.IsExpired() {
return ErrTokenExpired
}
if len(secret) != nonceLength+keyLength {
return ErrInvalidSecret
}
// Serialize the struct with the nonce from the secret
t.Nonce = secret[0:nonceLength]
var data []byte
if data, err = msgpack.Marshal(t); err != nil {
return err
}
return t.verifyData(data, signature, secret)
}
// NewSigningInfo creates new signing info with a time expiration
func NewSigningInfo(expires time.Duration) (info SigningInfo, err error) {
if expires == 0 {
return info, ErrExpirationIsRequired
}
info = SigningInfo{
ExpiresAt: time.Now().UTC().Add(expires).Truncate(time.Microsecond),
Nonce: make([]byte, nonceLength),
}
if _, err = rand.Read(info.Nonce); err != nil {
return info, ErrFailedSigning
}
return info, nil
}
// SigningInfo contains an expiration time and a nonce that is used to sign the token
type SigningInfo struct {
ExpiresAt time.Time `msgpack:"expires_at"`
Nonce []byte `msgpack:"nonce"`
}
func (d SigningInfo) IsExpired() bool {
return d.ExpiresAt.Before(time.Now())
}
// Create a signature from raw data and a nonce. The resulting signature is safe to be used in a URL
func (d SigningInfo) signData(data []byte) (_ string, secret []byte, err error) {
// Compute hash with a random 64 byte key
key := make([]byte, keyLength)
if _, err = rand.Read(key); err != nil {
return "", nil, err
}
mac := hmac.New(sha256.New, key)
if _, err = mac.Write(data); err != nil {
return "", nil, err
}
// Include the nonce with the key so that the token can be reconstructed later
secret = make([]byte, nonceLength+keyLength)
copy(secret[0:nonceLength], d.Nonce)
copy(secret[nonceLength:], key)
return base64.RawURLEncoding.EncodeToString(mac.Sum(nil)), secret, nil
}
// Verify data using the signature and secret
func (d SigningInfo) verifyData(data []byte, signature string, secret []byte) (err error) {
// Compute hash to verify the user token
mac := hmac.New(sha256.New, secret[nonceLength:])
if _, err = mac.Write(data); err != nil {
return err
}
// Decode the user token
var token []byte
if token, err = base64.RawURLEncoding.DecodeString(signature); err != nil {
return err
}
// Check if the recomputed token matches the user token
if !hmac.Equal(mac.Sum(nil), token) {
return ErrTokenInvalid
}
return nil
}
// NewOrgInvitationToken creates a token struct from an email address that expires
// in 14 days
func NewOrgInvitationToken(email string, orgID ulid.ULID) (token *OrgInviteToken, err error) {
if email == "" {
return nil, ErrInvitetokenMissingEmail
}
if ulids.IsZero(orgID) {
return nil, ErrInviteTokenMissingOrgID
}
token = &OrgInviteToken{
Email: email,
OrgID: orgID,
}
if token.SigningInfo, err = NewSigningInfo(time.Hour * 24 * inviteExpirationDays); err != nil {
return nil, err
}
return token, nil
}
// OrgInviteToken packages an email address with random data and an expiration
// time so that it can be serialized and hashed into a token which can be sent to users
type OrgInviteToken struct {
Email string `msgpack:"email"`
OrgID ulid.ULID `msgpack:"organization_id"`
SigningInfo
}
// Sign creates a base64 encoded string from the token data so that it can be sent to
// users as part of a URL. The returned secret should be stored in the database so that
// the string can be recomputed when verifying a user provided token.
func (t *OrgInviteToken) Sign() (string, []byte, error) {
data, err := msgpack.Marshal(t)
if err != nil {
return "", nil, err
}
return t.signData(data)
}
// Verify checks that a token was signed with the secret and is not expired
func (t *OrgInviteToken) Verify(signature string, secret []byte) (err error) {
if t.Email == "" {
return ErrInvitetokenMissingEmail
}
if ulids.IsZero(t.OrgID) {
return ErrInviteTokenMissingOrgID
}
if t.IsExpired() {
return ErrTokenExpired
}
if len(secret) != nonceLength+keyLength {
return ErrInvalidSecret
}
// Serialize the struct with the nonce from the secret
t.Nonce = secret[0:nonceLength]
var data []byte
if data, err = msgpack.Marshal(t); err != nil {
return err
}
return t.verifyData(data, signature, secret)
}