-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.go
More file actions
55 lines (44 loc) · 1.09 KB
/
Copy pathauth.go
File metadata and controls
55 lines (44 loc) · 1.09 KB
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
package main
import "go.uber.org/zap"
type User struct {
Username string
Password string `koanf:"Password"`
AllowedDomains []string `koanf:"AllowedDomains"`
}
// Return valid user password and ip
func GetValidUser(username, password, url string) *User {
u := GetUser(username)
if u == nil {
log.Info("user: not found", zap.String("username", username))
return nil
}
if !CompareHash(u.Password, password) {
log.Error("user: bad password", zap.String("username", username))
return nil
}
if !u.Allowed(url) {
return nil
}
return u
}
// verify user allowed domains
func (u *User) Allowed(url string) (ret bool) {
ret = CompareDomains(u.AllowedDomains, url)
if !ret {
log.Error("user: not allowed", zap.String("username", u.Username), zap.String("url", url))
}
return ret
}
// find user from configuration
func GetUser(username string) *User {
if configuration.Users == nil || len(configuration.Users) == 0 {
log.Info("user: no user configured")
return nil
}
u, ok := configuration.Users[username]
if !ok {
return nil
}
u.Username = username
return u
}