forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
134 lines (107 loc) · 2.57 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
package coordinator
import (
"code.google.com/p/go.crypto/bcrypt"
"github.com/pmylund/go-cache"
"regexp"
)
var userCache *cache.Cache
func init() {
userCache = cache.New(0, 0)
}
type Matcher struct {
IsRegex bool
Name string
}
func (self *Matcher) Matches(name string) bool {
if self.IsRegex {
matches, _ := regexp.MatchString(self.Name, name)
return matches
}
return self.Name == name
}
type CommonUser struct {
Name string `json:"name"`
Hash string `json:"hash"`
IsUserDeleted bool `json:"is_deleted"`
}
func (self *CommonUser) GetName() string {
return self.Name
}
func (self *CommonUser) IsDeleted() bool {
return self.IsUserDeleted
}
func (self *CommonUser) changePassword(password string) error {
hash, err := hashPassword(password)
if err != nil {
return err
}
self.Hash = string(hash)
userCache.Delete(self.Name)
return nil
}
func (self *CommonUser) isValidPwd(password string) bool {
if pwd, ok := userCache.Get(self.Name); ok {
return password == pwd.(string)
}
isValid := bcrypt.CompareHashAndPassword([]byte(self.Hash), []byte(password)) == nil
if isValid {
userCache.Set(self.Name, password, 0)
}
return isValid
}
func (self *CommonUser) IsClusterAdmin() bool {
return false
}
func (self *CommonUser) IsDbAdmin(db string) bool {
return false
}
func (self *CommonUser) GetDb() string {
return ""
}
func (self *CommonUser) HasWriteAccess(name string) bool {
return false
}
func (self *CommonUser) HasReadAccess(name string) bool {
return false
}
type clusterAdmin struct {
CommonUser `json:"common"`
}
func (self *clusterAdmin) IsClusterAdmin() bool {
return true
}
type dbUser struct {
CommonUser `json:"common"`
Db string `json:"db"`
WriteTo []*Matcher `json:"write_matchers"`
ReadFrom []*Matcher `json:"read_matchers"`
IsAdmin bool `json:"is_admin"`
}
func (self *dbUser) IsDbAdmin(db string) bool {
return self.IsAdmin && self.Db == db
}
func (self *dbUser) HasWriteAccess(name string) bool {
for _, matcher := range self.WriteTo {
if matcher.Matches(name) {
return true
}
}
return false
}
func (self *dbUser) HasReadAccess(name string) bool {
for _, matcher := range self.ReadFrom {
if matcher.Matches(name) {
return true
}
}
return false
}
func (self *dbUser) GetDb() string {
return self.Db
}
// private funcs
func hashPassword(password string) ([]byte, error) {
// The second arg is the cost of the hashing, higher is slower but makes it harder
// to brute force, since it will be really slow and impractical
return bcrypt.GenerateFromPassword([]byte(password), 10)
}