-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_auth_user.go
More file actions
140 lines (114 loc) · 3.11 KB
/
memory_auth_user.go
File metadata and controls
140 lines (114 loc) · 3.11 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
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
139
140
package xmppcore
import (
"errors"
"hash"
"github.com/google/uuid"
scramauth "github.com/yang-zzhong/scram-auth"
)
type MemoryAuthUser struct {
id string
username string
salt string
iterationCount int
passwords map[string]string
}
type MemoryPlainAuthUser struct {
username string
password string
}
func NewMemoryPlainAuthUser(username, password string) *MemoryPlainAuthUser {
return &MemoryPlainAuthUser{username, password}
}
func (mpu *MemoryPlainAuthUser) Username() string {
return mpu.username
}
func (mpu *MemoryPlainAuthUser) Password() string {
return mpu.password
}
type MemoryPlainAuthUserFetcher struct {
users []*MemoryPlainAuthUser
}
func NewMemoryPlainAuthUserFetcher() *MemoryPlainAuthUserFetcher {
return &MemoryPlainAuthUserFetcher{[]*MemoryPlainAuthUser{}}
}
func (mpuf *MemoryPlainAuthUserFetcher) Add(u *MemoryPlainAuthUser) {
mpuf.users = append(mpuf.users, u)
}
func (mpuf *MemoryPlainAuthUserFetcher) UserByUsername(username string) (PlainAuthUser, error) {
for _, u := range mpuf.users {
if u.Username() == username {
return u, nil
}
}
return nil, errors.New("user not found")
}
func NewMemomryAuthUser(username, password string, hash map[string]func() hash.Hash, ic int) *MemoryAuthUser {
u := new(MemoryAuthUser)
u.id = uuid.New().String()
u.username = username
u.iterationCount = ic
u.passwords = make(map[string]string)
u.salt = uuid.New().String()
for name, hashBuilder := range hash {
s := scramauth.NewServerScramAuth(hashBuilder, scramauth.None, nil)
u.passwords[name] = string(s.SaltedPassword([]byte(password), []byte(u.salt), u.iterationCount))
}
return u
}
func (au *MemoryAuthUser) ID() string {
return au.id
}
func (au *MemoryAuthUser) Username() string {
return au.username
}
func (au *MemoryAuthUser) Salt() string {
return au.salt
}
func (au *MemoryAuthUser) IterationCount() int {
return au.iterationCount
}
func (au *MemoryAuthUser) Password(hashName string) (string, error) {
p, ok := au.passwords[hashName]
if !ok {
return "", errors.New(ErrHashNotSupported)
}
return p, nil
}
type MemoryAuthUserFetcher struct {
users []*MemoryAuthUser
}
func NewMemoryAuthUserFetcher() *MemoryAuthUserFetcher {
return &MemoryAuthUserFetcher{users: []*MemoryAuthUser{}}
}
func (uf *MemoryAuthUserFetcher) Add(user *MemoryAuthUser) {
uf.users = append(uf.users, user)
}
func (uf *MemoryAuthUserFetcher) UserByUsername(username string) (ScramAuthUser, error) {
for _, u := range uf.users {
if u.Username() == username {
return u, nil
}
}
return nil, errors.New("user not found")
}
type MemoryAuthorized struct {
parts []Part
}
func NewMemoryAuthorized() *MemoryAuthorized {
return &MemoryAuthorized{parts: []Part{}}
}
func (ma *MemoryAuthorized) Authorized(_ string, part Part) {
ma.parts = append(ma.parts, part)
}
func (ma *MemoryAuthorized) BindResource(part Part, resource string) (string, error) {
part.Attr().JID.Resource = resource
return part.Attr().JID.String(), nil
}
func (ma *MemoryAuthorized) FindPart(jid *JID) Part {
for _, part := range ma.parts {
if jid.Equal(part.Attr().JID) {
return part
}
}
return nil
}