-
Notifications
You must be signed in to change notification settings - Fork 13
/
user.go
286 lines (237 loc) · 7.44 KB
/
user.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
// Copyright (c) 2021 Target Brands, Inc. All rights reserved.
//
// Use of this source code is governed by the LICENSE file in this repository.
package database
import (
"database/sql"
"encoding/base64"
"errors"
"regexp"
"github.com/go-vela/types/constants"
"github.com/go-vela/types/library"
"github.com/lib/pq"
)
var (
// userRegex defines the regex pattern for validating
// the Name field for the User type.
userRegex = regexp.MustCompile("^[a-zA-Z0-9_-]{0,38}$")
// ErrEmptyUserHash defines the error type when a
// User type has an empty Hash field provided.
ErrEmptyUserHash = errors.New("empty user hash provided")
// ErrEmptyUserName defines the error type when a
// User type has an empty Name field provided.
ErrEmptyUserName = errors.New("empty user name provided")
// ErrEmptyUserRefreshToken defines the error type when a
// User type has an empty RefreshToken field provided.
ErrEmptyUserRefreshToken = errors.New("empty user refresh token provided")
// ErrEmptyUserToken defines the error type when a
// User type has an empty Token field provided.
ErrEmptyUserToken = errors.New("empty user token provided")
// ErrInvalidUserName defines the error type when a
// User type has an invalid Name field provided.
ErrInvalidUserName = errors.New("invalid user name provided")
// ErrExceededFavoritesLimit defines the error type when a
// User type has Favorites field provided that exceeds the database limit.
ErrExceededFavoritesLimit = errors.New("exceeded favorites limit")
)
// User is the database representation of a user.
type User struct {
ID sql.NullInt64 `sql:"id"`
Name sql.NullString `sql:"name"`
RefreshToken sql.NullString `sql:"refresh_token"`
Token sql.NullString `sql:"token"`
Hash sql.NullString `sql:"hash"`
Favorites pq.StringArray `sql:"favorites" gorm:"type:varchar(5000)"`
Active sql.NullBool `sql:"active"`
Admin sql.NullBool `sql:"admin"`
}
// Decrypt will manipulate the existing user tokens by
// base64 decoding them. Then, a AES-256 cipher
// block is created from the encryption key in order to
// decrypt the base64 decoded user tokens.
func (u *User) Decrypt(key string) error {
// base64 decode the encrypted user hash
decoded, err := base64.StdEncoding.DecodeString(u.Hash.String)
if err != nil {
return err
}
// decrypt the base64 decoded user hash
decrypted, err := decrypt(key, decoded)
if err != nil {
return err
}
// set the decrypted user refresh token
u.Hash = sql.NullString{
String: string(decrypted),
Valid: true,
}
// base64 decode the encrypted user token
decoded, err = base64.StdEncoding.DecodeString(u.Token.String)
if err != nil {
return err
}
// decrypt the base64 decoded user token
decrypted, err = decrypt(key, decoded)
if err != nil {
return err
}
// set the decrypted user token
u.Token = sql.NullString{
String: string(decrypted),
Valid: true,
}
// base64 decode the encrypted user refresh token
decoded, err = base64.StdEncoding.DecodeString(u.RefreshToken.String)
if err != nil {
return err
}
// decrypt the base64 decoded user refresh token
decrypted, err = decrypt(key, decoded)
if err != nil {
return err
}
// set the decrypted user refresh token
u.RefreshToken = sql.NullString{
String: string(decrypted),
Valid: true,
}
return nil
}
// Encrypt will manipulate the existing user tokens by
// creating a AES-256 cipher block from the encryption
// key in order to encrypt the user tokens. Then, the
// user tokens are base64 encoded for transport across
// network boundaries.
func (u *User) Encrypt(key string) error {
// encrypt the user hash
encrypted, err := encrypt(key, []byte(u.Hash.String))
if err != nil {
return err
}
// base64 encode the encrypted user hash to make it network safe
u.Hash = sql.NullString{
String: base64.StdEncoding.EncodeToString(encrypted),
Valid: true,
}
// encrypt the user token
encrypted, err = encrypt(key, []byte(u.Token.String))
if err != nil {
return err
}
// base64 encode the encrypted user token to make it network safe
u.Token = sql.NullString{
String: base64.StdEncoding.EncodeToString(encrypted),
Valid: true,
}
// encrypt the user refresh token
encrypted, err = encrypt(key, []byte(u.RefreshToken.String))
if err != nil {
return err
}
// base64 encode the encrypted user refresh token to make it network safe
u.RefreshToken = sql.NullString{
String: base64.StdEncoding.EncodeToString(encrypted),
Valid: true,
}
return nil
}
// Nullify ensures the valid flag for
// the sql.Null types are properly set.
//
// When a field within the User type is the zero
// value for the field, the valid flag is set to
// false causing it to be NULL in the database.
func (u *User) Nullify() *User {
if u == nil {
return nil
}
// check if the ID field should be false
if u.ID.Int64 == 0 {
u.ID.Valid = false
}
// check if the Name field should be false
if len(u.Name.String) == 0 {
u.Name.Valid = false
}
// check if the RefreshToken field should be false
if len(u.RefreshToken.String) == 0 {
u.RefreshToken.Valid = false
}
// check if the Token field should be false
if len(u.Token.String) == 0 {
u.Token.Valid = false
}
// check if the Hash field should be false
if len(u.Hash.String) == 0 {
u.Hash.Valid = false
}
return u
}
// ToLibrary converts the User type
// to a library User type.
func (u *User) ToLibrary() *library.User {
user := new(library.User)
user.SetID(u.ID.Int64)
user.SetName(u.Name.String)
user.SetRefreshToken(u.RefreshToken.String)
user.SetToken(u.Token.String)
user.SetHash(u.Hash.String)
user.SetActive(u.Active.Bool)
user.SetAdmin(u.Admin.Bool)
user.SetFavorites(u.Favorites)
return user
}
// Validate verifies the necessary fields for
// the User type are populated correctly.
func (u *User) Validate() error {
// verify the Name field is populated
if len(u.Name.String) == 0 {
return ErrEmptyUserName
}
// verify the Token field is populated
if len(u.Token.String) == 0 {
return ErrEmptyUserToken
}
// verify the Hash field is populated
if len(u.Hash.String) == 0 {
return ErrEmptyUserHash
}
// verify the Name field is valid
if !userRegex.MatchString(u.Name.String) {
return ErrInvalidUserName
}
// calculate total size of favorites
total := 0
for _, f := range u.Favorites {
total += len(f)
}
// verify the Favorites field is within the database constraints
if total > constants.FavoritesMaxSize {
return ErrExceededFavoritesLimit
}
// ensure that all User string fields
// that can be returned as JSON are sanitized
// to avoid unsafe HTML content
u.Name = sql.NullString{String: sanitize(u.Name.String), Valid: u.Name.Valid}
// ensure that all Favorites are sanitized
// to avoid unsafe HTML content
for i, v := range u.Favorites {
u.Favorites[i] = sanitize(v)
}
return nil
}
// UserFromLibrary converts the library User type
// to a database User type.
func UserFromLibrary(u *library.User) *User {
user := &User{
ID: sql.NullInt64{Int64: u.GetID(), Valid: true},
Name: sql.NullString{String: u.GetName(), Valid: true},
RefreshToken: sql.NullString{String: u.GetRefreshToken(), Valid: true},
Token: sql.NullString{String: u.GetToken(), Valid: true},
Hash: sql.NullString{String: u.GetHash(), Valid: true},
Active: sql.NullBool{Bool: u.GetActive(), Valid: true},
Admin: sql.NullBool{Bool: u.GetAdmin(), Valid: true},
Favorites: pq.StringArray(u.GetFavorites()),
}
return user.Nullify()
}