This repository has been archived by the owner on Jul 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
auth.go
138 lines (123 loc) · 3.3 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
package ldbauth
import (
"fmt"
"strings"
"github.com/goftp/ftpd/web"
"github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/util"
)
type LDBAuth struct {
DB *leveldb.DB
}
func (db *LDBAuth) CheckPasswd(user, pass string) (bool, error) {
p, err := db.GetUser(user)
if err != nil {
if err == leveldb.ErrNotFound {
return false, nil
}
return false, err
}
return p == pass, nil
}
func (db *LDBAuth) GetUser(user string) (string, error) {
v, err := db.DB.Get([]byte(fmt.Sprintf("pass:%s", user)), nil)
if err != nil {
return "", err
}
return string(v), err
}
func (db *LDBAuth) AddUser(user, pass string) error {
return db.DB.Put([]byte(fmt.Sprintf("pass:%s", user)), []byte(pass), nil)
}
func (db *LDBAuth) DelUser(user string) error {
return db.DB.Delete([]byte(fmt.Sprintf("pass:%s", user)), nil)
}
func (db *LDBAuth) ChgPass(user, pass string) error {
return db.DB.Put([]byte(fmt.Sprintf("pass:%s", user)), []byte(pass), nil)
}
func (db *LDBAuth) UserList(users *[]web.User) error {
it := db.DB.NewIterator(&util.Range{[]byte("pass:"), nil}, nil)
defer it.Release()
for it.Next() {
key := string(it.Key())
if !strings.HasPrefix(key, "pass:") {
break
}
*users = append(*users, web.User{
key[5:],
string(it.Value()),
})
}
return nil
}
func (db *LDBAuth) GroupList(groups *[]string) error {
it := db.DB.NewIterator(&util.Range{[]byte("group:"), nil}, nil)
defer it.Release()
for it.Next() {
key := string(it.Key())
if !strings.HasPrefix(key, "group:") {
break
}
*groups = append(*groups, key[6:])
}
return nil
}
func (db *LDBAuth) AddGroup(group string) error {
return db.DB.Put([]byte(fmt.Sprintf("group:%s", group)), []byte(""), nil)
}
func (db *LDBAuth) DelGroup(group string) error {
start := fmt.Sprintf("groupuser:%s:", group)
it := db.DB.NewIterator(&util.Range{[]byte(start), nil}, nil)
defer it.Release()
keys := make([]string, 0)
for it.Next() {
key := string(it.Key())
if !strings.HasPrefix(key, start) {
break
}
keys = append(keys, key)
user := strings.TrimLeft(key, start)
err := db.DB.Delete([]byte(fmt.Sprintf("usergroup:%s:%s", user, group)), nil)
if err != nil {
return err
}
}
for _, k := range keys {
err := db.DB.Delete([]byte(k), nil)
if err != nil {
return err
}
}
return db.DB.Delete([]byte(fmt.Sprintf("group:%s", group)), nil)
}
func (db *LDBAuth) AddUserGroup(user, group string) error {
err := db.DB.Put([]byte(fmt.Sprintf("usergroup:%s:%s", user, group)), []byte(""), nil)
if err != nil {
return err
}
return db.DB.Put([]byte(fmt.Sprintf("groupuser:%s:%s", group, user)), []byte(""), nil)
}
func (db *LDBAuth) DelUserGroup(user, group string) error {
err := db.DB.Delete([]byte(fmt.Sprintf("usergroup:%s:%s", user, group)), nil)
if err != nil && err != leveldb.ErrNotFound {
return err
}
err = db.DB.Delete([]byte(fmt.Sprintf("groupuser:%s:%s", group, user)), nil)
if err != nil && err != leveldb.ErrNotFound {
return err
}
return nil
}
func (db *LDBAuth) GroupUser(group string, users *[]string) error {
prefix := fmt.Sprintf("groupuser:%s:", group)
it := db.DB.NewIterator(&util.Range{[]byte(prefix), nil}, nil)
defer it.Release()
for it.Next() {
key := string(it.Key())
if !strings.HasPrefix(key, prefix) {
break
}
*users = append(*users, key[len(prefix):])
}
return nil
}