Skip to content

Commit cf63d78

Browse files
committed
refactor(user): use slice for user list
Use slice instead of map to store users information.
1 parent 71df3cf commit cf63d78

File tree

8 files changed

+213
-192
lines changed

8 files changed

+213
-192
lines changed

src/serverHandler/handler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ type handler struct {
4848
globalAuth bool
4949
authUrls []string
5050
authDirs []string
51-
users user.Users
51+
users user.List
5252

5353
shows *regexp.Regexp
5454
showDirs *regexp.Regexp
@@ -138,7 +138,7 @@ func newHandler(
138138
root string,
139139
urlPrefix string,
140140
allAliases aliases,
141-
users user.Users,
141+
users user.List,
142142
theme tpl.Theme,
143143
logger *serverLog.Logger,
144144
errHandler *serverErrHandler.ErrHandler,

src/serverHandler/multiplexer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func (mux multiplexer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
3535

3636
func NewMultiplexer(
3737
p *param.Param,
38-
users user.Users,
38+
users user.List,
3939
theme tpl.Theme,
4040
logger *serverLog.Logger,
4141
errHandler *serverErrHandler.ErrHandler,

src/user/list.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package user
2+
3+
import (
4+
"errors"
5+
)
6+
7+
type List struct {
8+
users []user
9+
}
10+
11+
var errUserExists = errors.New("username already exist")
12+
13+
func (list *List) findIndex(username string) int {
14+
for i := range list.users {
15+
if list.users[i].getName() == username {
16+
return i
17+
}
18+
}
19+
return -1
20+
}
21+
22+
func (list *List) AddPlain(username, password string) error {
23+
if list.findIndex(username) >= 0 {
24+
return errUserExists
25+
}
26+
27+
user := newPlainUser(username, password)
28+
list.users = append(list.users, user)
29+
return nil
30+
}
31+
32+
func (list *List) AddBase64(username, password string) error {
33+
if list.findIndex(username) >= 0 {
34+
return errUserExists
35+
}
36+
37+
user := newBase64User(username, password)
38+
list.users = append(list.users, user)
39+
return nil
40+
}
41+
42+
func (list *List) AddMd5(username, password string) error {
43+
if list.findIndex(username) >= 0 {
44+
return errUserExists
45+
}
46+
47+
user, err := newMd5User(username, password)
48+
if err != nil {
49+
return err
50+
}
51+
52+
list.users = append(list.users, user)
53+
return nil
54+
}
55+
56+
func (list *List) AddSha1(username, password string) error {
57+
if list.findIndex(username) >= 0 {
58+
return errUserExists
59+
}
60+
61+
user, err := newSha1User(username, password)
62+
if err != nil {
63+
return err
64+
}
65+
66+
list.users = append(list.users, user)
67+
return nil
68+
}
69+
70+
func (list *List) AddSha256(username, password string) error {
71+
if list.findIndex(username) >= 0 {
72+
return errUserExists
73+
}
74+
75+
user, err := newSha256User(username, password)
76+
if err != nil {
77+
return err
78+
}
79+
80+
list.users = append(list.users, user)
81+
return nil
82+
}
83+
84+
func (list *List) AddSha512(username, password string) error {
85+
if list.findIndex(username) >= 0 {
86+
return errUserExists
87+
}
88+
89+
user, err := newSha512User(username, password)
90+
if err != nil {
91+
return err
92+
}
93+
94+
list.users = append(list.users, user)
95+
return nil
96+
}
97+
98+
func (list *List) Auth(username, password string) bool {
99+
index := list.findIndex(username)
100+
if index < 0 {
101+
return false
102+
}
103+
104+
return list.users[index].auth(password)
105+
}
106+
107+
func NewList() *List {
108+
return &List{}
109+
}

src/user/list_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package user
2+
3+
import (
4+
"testing"
5+
)
6+
7+
var list *List
8+
9+
func init() {
10+
list = NewList()
11+
}
12+
13+
func TestUserPlain(t *testing.T) {
14+
list.AddPlain("plain_user", "123")
15+
if !list.Auth("plain_user", "123") {
16+
t.Error()
17+
}
18+
if list.Auth("plain_user", "12") {
19+
t.Error()
20+
}
21+
}
22+
23+
func TestUserBase64(t *testing.T) {
24+
list.AddBase64("base64_user", "MjM0")
25+
if !list.Auth("base64_user", "234") {
26+
t.Error()
27+
}
28+
if list.Auth("base64_user", "23") {
29+
t.Error()
30+
}
31+
}
32+
33+
func TestUserMd5(t *testing.T) {
34+
list.AddMd5("md5_user", "d81f9c1be2e08964bf9f24b15f0e4900")
35+
if !list.Auth("md5_user", "345") {
36+
t.Error()
37+
}
38+
if list.Auth("md5_user", "34") {
39+
t.Error()
40+
}
41+
}
42+
43+
func TestUserSha1(t *testing.T) {
44+
list.AddSha1("sha1_user", "51eac6b471a284d3341d8c0c63d0f1a286262a18")
45+
if !list.Auth("sha1_user", "456") {
46+
t.Error()
47+
}
48+
if list.Auth("sha1_user", "45") {
49+
t.Error()
50+
}
51+
}
52+
53+
func TestUserSha256(t *testing.T) {
54+
list.AddSha256("sha256_user", "97a6d21df7c51e8289ac1a8c026aaac143e15aa1957f54f42e30d8f8a85c3a55")
55+
if !list.Auth("sha256_user", "567") {
56+
t.Error()
57+
}
58+
if list.Auth("sha256_user", "56") {
59+
t.Error()
60+
}
61+
}
62+
63+
func TestUserSha512(t *testing.T) {
64+
list.AddSha512("sha512_user", "c7d57e5c0b0792b154d573089792d80f5b64d2bc0cf4d7d1f551a9e4a4966e925d06b253cc9662c01df76623fdfecb812a2a0604119cb1ac37c47e8027e94cb5")
65+
if !list.Auth("sha512_user", "678") {
66+
t.Error()
67+
}
68+
if list.Auth("sha512_user", "67") {
69+
t.Error()
70+
}
71+
}

src/user/main.go

Lines changed: 0 additions & 103 deletions
This file was deleted.

src/user/main_test.go

Lines changed: 0 additions & 71 deletions
This file was deleted.

0 commit comments

Comments
 (0)