-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
290 lines (228 loc) · 6.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
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
287
288
289
290
package auth
import (
"fmt"
"net/http"
"sync"
"github.com/alrusov/config"
"github.com/alrusov/log"
"github.com/alrusov/misc"
)
//----------------------------------------------------------------------------------------------------------------------------//
type (
// Handlers --
Handlers struct {
mutex *sync.RWMutex
cfg *config.Listener
list []Handler
}
// Handler --
Handler interface {
Init(lCfg *config.Listener) error
Enabled() bool
Score() int
WWWAuthHeader() (name string, withRealm bool)
Check(id uint64, prefix string, path string, w http.ResponseWriter, r *http.Request) (identity *Identity, tryNext bool, err error)
}
// Identity --
Identity struct {
Method string
User string
UserID uint64
UserGUID string
Groups []string
Type string
IsAdmin bool
Extra any
}
IdentityProvider interface {
Init(aCfg *config.Auth) (err error)
GetIdentity(u string) (identity *Identity, err error)
Check(u string, p string, hashedPassword bool) (identity *Identity, exists bool, err error)
}
)
const (
// HTTP заголовок
Header = "Authorization"
)
var (
// Log --
Log = log.NewFacility("stdhttp.auth")
stdIdentityProviders []IdentityProvider
)
//----------------------------------------------------------------------------------------------------------------------------//
// NewHandlers --
func NewHandlers(cfg *config.Listener) *Handlers {
if len(stdIdentityProviders) == 0 {
provider := &LocalIdentityProvider{}
_ = AddStdIdentityProvider(provider, &cfg.Auth)
}
return &Handlers{
mutex: new(sync.RWMutex),
cfg: cfg,
}
}
//----------------------------------------------------------------------------------------------------------------------------//
// Add --
func (hh *Handlers) Add(ah Handler) (err error) {
hh.mutex.Lock()
defer hh.mutex.Unlock()
err = ah.Init(hh.cfg)
if err != nil {
return
}
if ah.Enabled() {
hh.add(ah)
return
}
return
}
func (hh *Handlers) add(ah Handler) {
ln := len(hh.list)
if ln == 0 {
hh.list = []Handler{ah}
return
}
score := ah.Score()
i := 0
for ; i < ln; i++ {
if hh.list[i].Score() > score {
break
}
}
if i == 0 {
hh.list = append([]Handler{ah}, hh.list...)
return
}
if i == ln {
hh.list = append(hh.list, ah)
return
}
hh.list = append(hh.list, nil)
copy(hh.list[i+1:], hh.list[i:])
hh.list[i] = ah
}
//----------------------------------------------------------------------------------------------------------------------------//
// Enabled --
func (hh *Handlers) Enabled() bool {
hh.mutex.RLock()
defer hh.mutex.RUnlock()
return len(hh.list) > 0
}
//----------------------------------------------------------------------------------------------------------------------------//
// WriteAuthRequestHeaders --
func (hh *Handlers) WriteAuthRequestHeaders(w http.ResponseWriter, prefix string, path string) {
hh.mutex.RLock()
defer hh.mutex.RUnlock()
if len(hh.list) == 0 {
return
}
for _, ah := range hh.list {
name, withRealm := ah.WWWAuthHeader()
if name == "" {
continue
}
s := name
if withRealm {
realm := hh.cfg.Auth.Realm
if realm == "" {
realm = prefix + path
}
s = fmt.Sprintf(`%s realm="%s"`, name, realm)
}
w.Header().Add("WWW-Authenticate", s)
}
}
//----------------------------------------------------------------------------------------------------------------------------//
// Check --
func (hh *Handlers) Check(id uint64, prefix string, path string, permissions misc.BoolMap, w http.ResponseWriter, r *http.Request) (identity *Identity, code int, msg string) {
hh.mutex.RLock()
defer hh.mutex.RUnlock()
code = 0
if len(hh.list) == 0 {
return
}
tryNext := false
var err error
for _, ah := range hh.list {
identity, tryNext, err = ah.Check(id, prefix, path, w, r)
if identity != nil {
if !identity.checkPermissions(permissions) {
code = http.StatusForbidden
msg = "Forbidden"
}
return
}
if !tryNext {
break
}
}
code = http.StatusUnauthorized
if err != nil {
msg = err.Error()
return
}
msg = "Unauthorised"
return
}
//----------------------------------------------------------------------------------------------------------------------------//
func (identity *Identity) checkPermissions(permissions misc.BoolMap) bool {
if len(permissions) == 0 {
return false
}
user := identity.User
p, exists := permissions[user]
if exists {
return p
}
if len(identity.Groups) > 0 {
enabled := false
for _, g := range identity.Groups {
p, exists := permissions["@"+g]
if exists {
if !p {
return false
}
enabled = true
}
}
if enabled {
return true
}
}
p, exists = permissions["*"]
if exists {
return p
}
return false
}
//----------------------------------------------------------------------------------------------------------------------------//
// Hash --
func Hash(p []byte, salt []byte) []byte {
return misc.Sha512Hash(append(p, salt...))
}
//----------------------------------------------------------------------------------------------------------------------------//
func AddStdIdentityProvider(provider IdentityProvider, aCfg *config.Auth) (err error) {
stdIdentityProviders = append(stdIdentityProviders, provider)
return provider.Init(aCfg)
}
//----------------------------------------------------------------------------------------------------------------------------//
func StdGetIdentity(u string) (identity *Identity, err error) {
for _, provider := range stdIdentityProviders {
identity, err = provider.GetIdentity(u)
if identity != nil || err != nil {
return
}
}
return nil, nil
}
//----------------------------------------------------------------------------------------------------------------------------//
func StdCheckUser(u string, p string, hashedPassword bool) (identity *Identity, exists bool, err error) {
for _, provider := range stdIdentityProviders {
identity, exists, err = provider.Check(u, p, hashedPassword)
if exists || err != nil {
return
}
}
return nil, false, nil
}
//----------------------------------------------------------------------------------------------------------------------------//