-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
72 lines (61 loc) · 1.45 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
package auth
import (
"sync"
"time"
validation "github.com/go-ozzo/ozzo-validation/v4"
"github.com/oklog/ulid"
"github.com/spf13/afero"
"gopkg.in/yaml.v3"
)
var userPool *sync.Pool
func init() {
userPool = &sync.Pool{
New: func() interface{} {
return &User{}
},
}
}
func newUser() *User {
return userPool.New().(*User)
}
func returnUser(u *User) {
*u = User{}
userPool.Put(u)
}
//User contains user properties
type User struct {
ID string `json:"id" yaml:"id,omitempty" storm:"unique"`
Username string `json:"username" yaml:"username" storm:"unique"`
Name string `json:"name" yaml:"name"`
Password string `json:"password,omitempty" yaml:"password,omitempty"`
Scope Scope `json:"scope" yaml:"scope"`
}
func (u User) Validate() error {
return validation.ValidateStruct(&u,
validation.Field(&u.Password, validation.Required, validation.Length(6, 0)))
}
func (u User) toJwt(secret []byte, scope Scope, ttl time.Duration) (string, error) {
return buildJwt(u.ID, u.Username, u.Name, secret, ttl, scope)
}
func LoadUsersFromFile(file string, fs afero.Fs) ([]*User, error) {
data, err := afero.ReadFile(fs, file)
if err != nil {
return nil, err
}
var users []*User
err = yaml.Unmarshal(data, &users)
if err != nil {
return nil, err
}
for _, u := range users {
if u.ID != "" {
continue
}
ID, err := ulid.New(ulid.Timestamp(time.Now()), entropy)
if err != nil {
continue
}
u.ID = ID.String()
}
return users, nil
}