forked from Qihoo360/wayne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ldap.go
95 lines (77 loc) · 2.71 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
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
package ldap
import (
"fmt"
"strconv"
"strings"
"github.com/astaxie/beego"
"github.com/Qihoo360/wayne/src/backend/controllers/auth"
"github.com/Qihoo360/wayne/src/backend/models"
ldapUtils "github.com/Qihoo360/wayne/src/backend/util/ldap"
"github.com/Qihoo360/wayne/src/backend/util/logs"
)
type LDAPAuth struct{}
const metaChars = "&|!=~*<>()"
func init() {
auth.Register(models.AuthTypeLDAP, &LDAPAuth{})
}
func (*LDAPAuth) Authenticate(m models.AuthModel) (*models.User, error) {
u := m.Username
if len(strings.TrimSpace(u)) == 0 {
logs.Info("LDAP authentication failed for empty user id.")
return nil, nil
}
for _, c := range metaChars {
if strings.ContainsRune(u, c) {
return nil, fmt.Errorf("the principal contains meta char: %q", c)
}
}
ldapConfs := models.LdapConf{}
section, err := beego.AppConfig.GetSection("auth.ldap")
if err != nil {
return nil, fmt.Errorf("Can't find ldap config. ")
}
ldapConfs.LdapURL = section["ldap_url"]
ldapConfs.LdapSearchDn = section["ldap_search_dn"]
ldapConfs.LdapSearchPassword = section["ldap_search_password"]
ldapConfs.LdapBaseDn = section["ldap_base_dn"]
ldapConfs.LdapFilter = section["ldap_filter"]
ldapConfs.LdapUID = section["ldap_uid"]
ldapScope, err := strconv.ParseInt(section["ldap_scope"], 10, 64)
if err != nil {
return nil, fmt.Errorf("ldap_scope parse error, must be int. ")
}
ldapConnectionTimeout, err := strconv.ParseInt(section["ldap_connection_timeout"], 10, 64)
if err != nil {
return nil, fmt.Errorf("ldap_connection_timeout parse error, must be int. ")
}
ldapConfs.LdapScope = int(ldapScope)
ldapConfs.LdapConnectionTimeout = int(ldapConnectionTimeout)
ldapConfs, err = ldapUtils.ValidateLdapConf(ldapConfs)
if err != nil {
return nil, fmt.Errorf("invalid ldap request: %v", err)
}
ldapConfs.LdapFilter = ldapUtils.MakeFilter(u, ldapConfs.LdapFilter, ldapConfs.LdapUID)
ldapUsers, err := ldapUtils.SearchUser(ldapConfs)
if err != nil {
logs.Warning("ldap search fail: %v", err)
return nil, err
}
if len(ldapUsers) == 0 {
logs.Warning("Not found an entry.")
return nil, fmt.Errorf("Not found an entry. ")
} else if len(ldapUsers) != 1 {
logs.Warning("Found more than one entry.")
return nil, fmt.Errorf("Found more than one entry. ")
}
user := &models.User{}
user.Name = ldapUsers[0].Username
user.Email = ldapUsers[0].Email
user.Display = ldapUsers[0].Realname
dn := ldapUsers[0].DN
logs.Info("username: %s, dn: %s", user.Name, dn)
if err := ldapUtils.Bind(ldapConfs, dn, m.Password); err != nil {
logs.Warning("Failed to bind user, username: %s, dn: %s, error: %v", user.Name, dn, err)
return nil, fmt.Errorf("Failed to bind user, username: %s, dn: %s, error: %v ", user.Name, dn, err)
}
return user, nil
}