2016-07-26 01:30:28 +05:30
|
|
|
// Package ldap implements strategies for authenticating using the LDAP protocol.
|
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"gopkg.in/ldap.v2"
|
|
|
|
|
2016-08-11 11:01:42 +05:30
|
|
|
"github.com/coreos/dex/connector"
|
2016-07-26 01:30:28 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Config holds the configuration parameters for the LDAP connector.
|
|
|
|
type Config struct {
|
|
|
|
Host string `yaml:"host"`
|
|
|
|
BindDN string `yaml:"bindDN"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open returns an authentication strategy using LDAP.
|
|
|
|
func (c *Config) Open() (connector.Connector, error) {
|
|
|
|
if c.Host == "" {
|
|
|
|
return nil, errors.New("missing host parameter")
|
|
|
|
}
|
|
|
|
if c.BindDN == "" {
|
|
|
|
return nil, errors.New("missing bindDN paramater")
|
|
|
|
}
|
|
|
|
return &ldapConnector{*c}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ldapConnector struct {
|
|
|
|
Config
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:44:24 +05:30
|
|
|
var _ connector.PasswordConnector = (*ldapConnector)(nil)
|
|
|
|
|
2016-07-26 01:30:28 +05:30
|
|
|
func (c *ldapConnector) do(f func(c *ldap.Conn) error) error {
|
|
|
|
// TODO(ericchiang): Connection pooling.
|
|
|
|
conn, err := ldap.Dial("tcp", c.Host)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to connect: %v", err)
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
return f(conn)
|
|
|
|
}
|
|
|
|
|
2016-08-03 09:44:24 +05:30
|
|
|
func (c *ldapConnector) Login(username, password string) (connector.Identity, bool, error) {
|
2016-07-26 01:30:28 +05:30
|
|
|
err := c.do(func(conn *ldap.Conn) error {
|
|
|
|
return conn.Bind(fmt.Sprintf("uid=%s,%s", username, c.BindDN), password)
|
|
|
|
})
|
|
|
|
if err != nil {
|
2016-08-03 09:44:24 +05:30
|
|
|
// TODO(ericchiang): Determine when the user has entered invalid credentials.
|
|
|
|
return connector.Identity{}, false, err
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2016-08-03 09:44:24 +05:30
|
|
|
return connector.Identity{Username: username}, true, nil
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ldapConnector) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|