-
Notifications
You must be signed in to change notification settings - Fork 4
/
user.go
224 lines (204 loc) · 6.14 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
package user
import (
"context"
"errors"
"fmt"
"html"
"log"
"regexp"
"strings"
"time"
"github.com/badoux/checkmail"
"github.com/jackc/pgx/v4/pgxpool"
"golang.org/x/crypto/bcrypt"
)
var AdminEmail string
type User struct {
Name string `json:"name"`
Email string `json:"user_email"`
Password string `json:"password"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Token string `json:"token"`
DevMode string `json:"dev_mode"`
roles map[string]bool
capabilities map[string]bool
IsActive int `json:"is_active"`
UserGitProfile GitProfile `json:"gitProfile"`
}
// JetStore Capabilities:
// - jetstore_read: Read access from ui
// - client_config: Add, modify client configuration
// - workspace_ide: Access workspace IDE screens and functions, including query tool and git functions
// - run_pipelines: Load files and run pipelines
// - user_profile: Update user profile
// NOTE: role_capability table is initialized in base_workspace_init_db.sql
func NewUser(email string) *User {
u := User{Email: email}
u.roles = make(map[string]bool)
u.capabilities = make(map[string]bool)
return &u
}
func Hash(password string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(password), bcrypt.DefaultCost)
}
func VerifyPassword(hashedPassword, password string) error {
return bcrypt.CompareHashAndPassword([]byte(hashedPassword), []byte(password))
}
func (u *User) IsAdmin() bool {
return u.Email == AdminEmail
}
func (u *User) GetCapabilities() []string {
keys := make([]string, 0, len(u.capabilities))
for k := range u.capabilities {
keys = append(keys, k)
}
return keys
}
func (u *User) HasCapability(capability string) bool {
if capability == "" {
return false
}
if u.IsAdmin() {
return true
}
return u.capabilities[capability]
}
func (u *User) BeforeSave() error {
hashedPassword, err := Hash(u.Password)
if err != nil {
return err
}
u.Password = string(hashedPassword)
return nil
}
func (u *User) Prepare() {
u.Name = html.EscapeString(strings.TrimSpace(u.Name))
u.Email = html.EscapeString(strings.TrimSpace(u.Email))
u.CreatedAt = time.Now()
u.UpdatedAt = time.Now()
}
func (u *User) Validate(action string) error {
switch strings.ToLower(action) {
case "login":
if u.Password == "" {
return errors.New("Required Password")
}
if u.Email == "" {
return errors.New("Required Email")
}
if !u.IsAdmin() {
if err := checkmail.ValidateFormat(u.Email); err != nil {
return errors.New("Invalid Email")
}
}
return nil
default:
if u.IsAdmin() {
return errors.New("Login Reserved for Administrator")
}
if u.Name == "" {
return errors.New("Required Name")
}
if u.Password == "" {
return errors.New("Required Password")
}
//* check that password pass test
hasDigit, _ := regexp.MatchString("[0-9]", u.Password)
hasUpper, _ := regexp.MatchString("[A-Z]", u.Password)
hasLower, _ := regexp.MatchString("[a-z]", u.Password)
hasSpecial, _ := regexp.MatchString(`[!@#$%^&*()_+-=\[\]{}|']`, u.Password)
if !hasDigit || !hasUpper || !hasLower || !hasSpecial || len(u.Password) < 14 {
return errors.New("Invalid Password")
}
if u.Email == "" {
return errors.New("Required Email")
}
if err := checkmail.ValidateFormat(u.Email); err != nil {
return errors.New("Invalid Email")
}
return nil
}
}
func (u *User) InsertUser(dbpool *pgxpool.Pool) error {
// hash the password
err := u.BeforeSave()
if err != nil {
log.Println("while hashing user's password before save in db:", err)
return errors.New("Unknown error while saving user")
}
// insert in db
stmt := `INSERT INTO jetsapi.users (name, user_email, password) VALUES ($1, $2, $3)`
_, err = dbpool.Exec(context.Background(), stmt, u.Name, u.Email, u.Password)
if err != nil {
log.Println("while inserting in db:", err)
return errors.New("Unknown error while saving user")
}
return nil
}
func GetUserByEmail(dbpool *pgxpool.Pool, email string) (*User, error) {
// select from db
u := NewUser(email)
encryptedRoles := make([]string, 0)
stmt := `SELECT name, password, encrypted_roles, is_active, git_name, git_email, git_handle
FROM jetsapi.users
WHERE user_email = $1`
err := dbpool.QueryRow(context.Background(), stmt, u.Email).
Scan(&u.Name, &u.Password, &encryptedRoles, &u.IsActive,
&u.UserGitProfile.Name,
&u.UserGitProfile.Email,
&u.UserGitProfile.GitHandle)
if err != nil {
log.Println("while select user by user_email from db:", err)
return nil, errors.New("invalid user or password")
}
if u.IsAdmin() {
u.capabilities["client_config"] = true
u.capabilities["workspace_ide"] = true
u.capabilities["run_pipelines"] = true
return u, nil
}
// Decrypt user's role and map it to capabilities
// @**@ profile read Decrypt user's role and map it to capabilities
for _, role := range encryptedRoles {
u.roles[role] = true
}
if len(u.roles) > 0 {
for i := range encryptedRoles {
encryptedRoles[i] = fmt.Sprintf("'%s'", encryptedRoles[i])
}
stmt = fmt.Sprintf("SELECT capability FROM jetsapi.role_capability WHERE role IN (%s)",
strings.Join(encryptedRoles, ","))
rows, err := dbpool.Query(context.Background(), stmt)
if err != nil {
log.Println("while select user by user_email from db:", err)
return nil, errors.New("error retreiving roles")
}
defer rows.Close()
var capability string
for rows.Next() {
if err := rows.Scan(&capability); err != nil {
log.Println("while scanning capability from db:", err)
return nil, errors.New("error retreiving capabilities")
}
u.capabilities[capability] = true
}
}
return u, nil
}
func GetUserByToken(dbpool *pgxpool.Pool, token string) (*User, error) {
// Get user info
userEmail, err := ExtractTokenID(token)
if err != nil {
log.Println("while ExtractTokenID", err.Error())
err = errors.New("error: unauthorized, cannot extract user email from token")
return nil, err
}
user, err := GetUserByEmail(dbpool, userEmail)
if err != nil {
log.Println("while GetUserByEmail", err.Error())
err = errors.New("error: unauthorized, cannot get user info")
return nil, err
}
return user, nil
}