github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/auth/ldap/ldap.go (about) 1 package ldap 2 3 import ( 4 "fmt" 5 "strings" 6 7 log "github.com/Sirupsen/logrus" 8 "github.com/shipyard/shipyard/auth" 9 goldap "gopkg.in/ldap.v1" 10 ) 11 12 type ( 13 LdapAuthenticator struct { 14 Server string 15 Port int 16 BaseDN string 17 DefaultAccessLevel string 18 AutocreateUsers bool 19 } 20 ) 21 22 func NewAuthenticator(server string, port int, baseDN string, autocreateUsers bool, defaultAccessLevel string) auth.Authenticator { 23 log.Infof("Using LDAP authentication: server=%s port=%d basedn=%s", 24 server, port, baseDN) 25 return &LdapAuthenticator{ 26 Server: server, 27 Port: port, 28 BaseDN: baseDN, 29 AutocreateUsers: autocreateUsers, 30 DefaultAccessLevel: defaultAccessLevel, 31 } 32 } 33 34 func (a LdapAuthenticator) Name() string { 35 return "ldap" 36 } 37 38 func (a LdapAuthenticator) Authenticate(username, password, hash string) (bool, error) { 39 log.Debugf("ldap authentication: username=%s", username) 40 l, err := goldap.Dial("tcp", fmt.Sprintf("%s:%d", a.Server, a.Port)) 41 if err != nil { 42 log.Error(err) 43 return false, err 44 } 45 defer l.Close() 46 47 dn := fmt.Sprintf("cn=%s,%s", username, a.BaseDN) 48 49 if strings.Contains(a.BaseDN, "{username}") { 50 dn = strings.Replace(a.BaseDN, "{username}", username, -1) 51 } 52 53 log.Debugf("ldap authentication: dn=%s", dn) 54 55 if err := l.Bind(dn, password); err != nil { 56 return false, err 57 } 58 59 log.Debugf("ldap authentication successful: username=%s", username) 60 61 return true, nil 62 } 63 64 func (a LdapAuthenticator) IsUpdateSupported() bool { 65 return false 66 } 67 68 func (a LdapAuthenticator) GenerateToken() (string, error) { 69 return auth.GenerateToken() 70 }