-
Notifications
You must be signed in to change notification settings - Fork 165
/
redis.go
136 lines (99 loc) · 2.64 KB
/
redis.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
package backends
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/pkg/errors"
"github.com/iegomez/mosquitto-go-auth/common"
goredis "github.com/go-redis/redis"
)
type Redis struct {
Host string
Port string
Password string
DB int32
Conn *goredis.Client
}
func NewRedis(authOpts map[string]string) (Redis, error) {
var redis = Redis{
Host: "localhost",
Port: "6379",
Password: "",
DB: 1,
}
if redisHost, ok := authOpts["redis_host"]; ok {
redis.Host = redisHost
}
if redisPort, ok := authOpts["redis_port"]; ok {
redis.Port = redisPort
}
if redisPassword, ok := authOpts["redis_password"]; ok {
redis.Password = redisPassword
}
if redisDB, ok := authOpts["redis_db"]; ok {
db, err := strconv.ParseInt(redisDB, 10, 32)
if err == nil {
redis.DB = int32(db)
}
}
addr := fmt.Sprintf("%s:%s", redis.Host, redis.Port)
//Try to start redis.
goredisClient := goredis.NewClient(&goredis.Options{
Addr: addr,
Password: redis.Password, // no password set
DB: int(redis.DB), // use default DB
})
_, err := goredisClient.Ping().Result()
if err != nil {
return redis, errors.Errorf("couldn't start redis backend. error: %s\n", err)
}
redis.Conn = goredisClient
return redis, nil
}
//GetUser checks that the username exists and the given password hashes to the same password.
func (o Redis) GetUser(username, password string) bool {
pwHash, err := o.Conn.Get(username).Result()
if err != nil {
log.Printf("Redis get user error: %s\n", err)
return false
}
if common.HashCompare(password, pwHash) {
return true
}
return false
}
//GetSuperuser checks that the key username:su exists and has value "true".
func (o Redis) GetSuperuser(username string) bool {
isSuper, err := o.Conn.Get(fmt.Sprintf("%s:su", username)).Result()
if err != nil {
log.Printf("Redis get superuser error: %s\n", err)
return false
}
if isSuper == "true" {
return true
}
return false
}
//CheckAcl gets all acls for the username and tries to match against topic, acc, and username/clientid if needed.
func (o Redis) CheckAcl(username, topic, clientid string, acc int32) bool {
var acls []string
var err error
acls, err = o.Conn.SMembers(fmt.Sprintf("%s:acls", username)).Result()
if err != nil {
log.Printf("Redis check acl error: %s\n", err)
return false
}
for _, acl := range acls {
aclTopic := strings.Replace(acl, "%c", clientid, -1)
aclTopic = strings.Replace(aclTopic, "%u", username, -1)
if common.TopicsMatch(aclTopic, topic) {
return true
}
}
return false
}
//GetName returns the backend's name
func (o Redis) GetName() string {
return "Redis"
}