Newer
Older
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
// Package ldap implements strategies for authenticating using the LDAP protocol.
package ldap
import (
"errors"
"fmt"
"gopkg.in/ldap.v2"
"github.com/coreos/poke/connector"
"github.com/coreos/poke/storage"
)
// 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
}
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)
}
func (c *ldapConnector) Login(username, password string) (storage.Identity, error) {
err := c.do(func(conn *ldap.Conn) error {
return conn.Bind(fmt.Sprintf("uid=%s,%s", username, c.BindDN), password)
})
if err != nil {
return storage.Identity{}, err
}
return storage.Identity{Username: username}, nil
}
func (c *ldapConnector) Close() error {
return nil
}