-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserloader.go
57 lines (50 loc) · 1.34 KB
/
userloader.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
_ "net/http/pprof"
"os"
)
func loadUserAuthFile(userFile string) *Auth {
// if len(userFile) == 0 {
// return new(auth.Allow)
// }
file, err := os.OpenFile(userFile, os.O_RDONLY, 0666)
if err != nil {
logPrint("X", fmt.Sprintf("%s %s %s: %s", lang("READFAIL"), lang("USERDATABASE"), userFile, err))
return nil
}
userData, err := ioutil.ReadAll(file)
if err != nil {
logPrint("X", fmt.Sprintf("%s %s %s: %s", lang("READFAIL"), lang("USERDATABASE"), userFile, err))
return nil
}
var userPermissions Auth
json.Unmarshal(userData, &userPermissions)
var auth *Auth = &userPermissions
logPrint("I", fmt.Sprintf("%s %s: %d", lang("LOADED"), lang("USERDB"), len(userPermissions.Users)))
logPrint("I", fmt.Sprintf("%s %s: %d", lang("LOADED"), lang("PERMDB"), len(userPermissions.AllowedTopics)))
return auth
}
type Auth struct {
Users map[string]string
AllowedTopics map[string][]string
}
func (a *Auth) Authenticate(user, password []byte) bool {
if pass, ok := a.Users[string(user)]; ok && pass == string(password) {
return true
}
return false
}
func (a *Auth) ACL(user []byte, topic string, write bool) bool {
if topics, ok := a.AllowedTopics[string(user)]; ok {
for _, t := range topics {
if t == topic {
return true
}
}
return false
}
return true
}