-
Notifications
You must be signed in to change notification settings - Fork 3
/
ldap.go
70 lines (60 loc) · 1.41 KB
/
ldap.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
package testutil
import (
"fmt"
"github.com/macrat/lauth/ldap"
)
var (
LDAP = DummyLDAP{
"macrat": DummyUserInfo{
Password: "foobar",
Attributes: map[string][]string{
"displayName": {"SHIDA Yuuma"},
"givenName": {"yuuma"},
"sn": {"shida"},
"mail": {"m@crat.jp"},
"telephoneNumber": {"000-1234-5678"},
},
},
"j.smith": DummyUserInfo{
Password: "hello",
Attributes: map[string][]string{
"displayName": {"Jhon smith"},
"givenName": {"jhon"},
"sn": {"smith"},
"mail": {"jhon@example.com"},
},
},
}
)
type DummyUserInfo struct {
Password string
Attributes map[string][]string
}
type DummyLDAP map[string]DummyUserInfo
func (c DummyLDAP) Connect() (ldap.Session, error) {
return c, nil
}
func (c DummyLDAP) Close() error {
return nil
}
func (c DummyLDAP) LoginTest(username, password string) error {
if user, ok := c[username]; !ok {
return ldap.UserNotFoundError
} else if user.Password != password {
return fmt.Errorf("incorrect password")
}
return nil
}
func (c DummyLDAP) GetUserAttributes(username string, attributes []string) (map[string][]string, error) {
user, ok := c[username]
if !ok {
return nil, ldap.UserNotFoundError
}
result := make(map[string][]string)
for _, attr := range attributes {
if value, ok := user.Attributes[attr]; ok {
result[attr] = value
}
}
return result, nil
}