-
Notifications
You must be signed in to change notification settings - Fork 35
/
auth.go
246 lines (197 loc) · 5.1 KB
/
auth.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
package hal
import (
"fmt"
"github.com/danryan/env"
"strings"
)
// UserHasRole determines whether the Response's user has a given role
func UserHasRole(res *Response, role string) bool {
user := res.Envelope.User
for _, r := range user.Roles {
if r == role {
return true
}
}
return false
}
// Auth type to group authentication methods
type Auth struct {
robot *Robot
admins []string
}
type authConfig struct {
Enabled bool `env:"key=HAL_AUTH_ENABLED default=true"`
Admins string `env:"key=HAL_AUTH_ADMIN"`
}
// NewAuth returns a pointer to an initialized Auth
func NewAuth(r *Robot) *Auth {
a := &Auth{robot: r}
c := &authConfig{}
env.MustProcess(c)
if c.Enabled {
if c.Admins != "" {
a.admins = strings.Split(c.Admins, ",")
}
r.Handle(
addUserRoleHandler,
removeUserRoleHandler,
listUserRolesHandler,
listAdminsHandler,
)
}
return a
}
// Admins returns a slice of admin Users
func (a *Auth) Admins() (admins []User) {
for _, name := range a.admins {
user, err := a.robot.Users.GetByName(name)
if err != nil {
continue
}
admins = append(admins, user)
}
return
}
// HasRole checks whether a user located by id has a given role(s)
func (a *Auth) HasRole(id string, roles ...string) bool {
user, err := a.robot.Users.Get(id)
if err != nil {
return false
}
if len(user.Roles) == 0 {
return false
}
for _, r := range roles {
for _, b := range user.Roles {
if b == r {
return true
}
}
}
return false
}
// UsersWithRole returns a slice of Users that have a given role
func (a *Auth) UsersWithRole(role string) (users []User) {
for _, user := range a.robot.Users.All() {
if a.HasRole(user.ID, role) {
users = append(users, user)
}
}
return
}
// AddRole adds a role to a User
func (a *Auth) AddRole(user User, r string) error {
if r == "admin" {
return fmt.Errorf(`the "admin" role can only be defined by the HAL_AUTH_ADMIN environment variable`)
}
if a.HasRole(user.ID, r) {
return fmt.Errorf("%s already has the %s role", user.Name, r)
}
user.Roles = append(user.Roles, r)
a.robot.Users.Set(user.ID, user)
return nil
}
// RemoveRole adds a role to a User
func (a *Auth) RemoveRole(user User, role string) error {
if role == "admin" {
return fmt.Errorf(`the "admin" role can only be defined by the HAL_AUTH_ADMIN environment variable`)
}
if !a.HasRole(user.ID, role) {
return fmt.Errorf("%s already does not have the %s role", user.Name, role)
}
roles := make([]string, len(user.Roles)-1)
for _, r := range user.Roles {
if r != role {
roles = append(roles, role)
}
}
user.Roles = roles
a.robot.Users.Set(user.ID, user)
return nil
}
// IsAdmin checks whether a user is an admin
func (a *Auth) IsAdmin(user User) bool {
for _, a := range a.admins {
if a == user.Name {
return true
}
}
return false
}
var addUserRoleHandler = &Handler{
Pattern: `(?i)@?(.+) (?:has)(?: the)? (["'\w: -_]+) (?:role)`,
Method: RESPOND,
Run: func(res *Response) error {
name := strings.TrimSpace(res.Match[1])
role := strings.ToLower(res.Match[2])
for _, s := range []string{"", "who", "what", "where", "when", "why"} {
if s == name {
return nil // don't match
}
}
user, err := res.Robot.Users.GetByName(name)
if err != nil {
return res.Reply(err.Error())
}
if err := res.Robot.Auth.AddRole(user, role); err != nil {
return res.Reply(err.Error())
}
return res.Reply(fmt.Sprintf("%s now has the %s role", name, role))
},
}
var removeUserRoleHandler = &Handler{
Pattern: `(?i)@?(.+) (?:does(?:n't| not) have)(?: the)? (["'\w: -_]+) (role)`,
Method: RESPOND,
Run: func(res *Response) error {
name := strings.TrimSpace(res.Match[1])
role := strings.ToLower(res.Match[2])
for _, s := range []string{"", "who", "what", "where", "when", "why"} {
if s == name {
return nil // don't match
}
}
user, err := res.Robot.Users.GetByName(name)
if err != nil {
return res.Reply(err.Error())
}
if err := res.Robot.Auth.RemoveRole(user, role); err != nil {
return res.Reply(err.Error())
}
return res.Reply(fmt.Sprintf("%s no longer has the %s role", name, role))
},
}
var listUserRolesHandler = &Handler{
Pattern: `(?i)(?:what roles? does) @?(.+) (?:have)\??`,
Method: RESPOND,
Run: func(res *Response) error {
name := res.Match[1]
user, err := res.Robot.Users.GetByName(name)
// return if we didn't find a user
if err != nil {
res.Reply(err.Error())
}
roles := user.Roles
if res.Robot.Auth.IsAdmin(user) {
roles = append(roles, "admin")
}
if len(roles) == 0 {
return res.Reply(name + " has no roles")
}
return res.Reply(fmt.Sprintf("%s has the following roles: %s", name, strings.Join(roles, ", ")))
},
}
var listAdminsHandler = &Handler{
Pattern: `who (?:has)(?: the)? admin role\??`,
Method: RESPOND,
Run: func(res *Response) error {
admins := res.Robot.Auth.Admins()
names := make([]string, len(admins))
if len(names) == 0 {
return res.Reply(`no users have the "admin" role`)
}
for i, u := range admins {
names[i] = u.Name
}
return res.Reply(fmt.Sprintf(`the following users have the "admin" role: %s`, strings.Join(names, ", ")))
},
}