-
Notifications
You must be signed in to change notification settings - Fork 4
/
ldap-bind.go
66 lines (56 loc) · 1.23 KB
/
ldap-bind.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
package ldapbind
import (
"crypto/tls"
"fmt"
"log"
"net/url"
"time"
"github.com/dgrijalva/jwt-go"
"github.com/mcluseau/autorizo/api"
"gopkg.in/ldap.v2"
)
// New Authenticator with ldap backend
func New(server, userTemplate string) api.Authenticator {
u, err := url.Parse(server)
if err != nil {
log.Fatal("Bad LDAP server URL: ", err)
}
return &auth{
url: u,
userTemplate: userTemplate,
}
}
type auth struct {
url *url.URL
userTemplate string
}
var _ api.Authenticator = auth{}
func (a auth) Authenticate(user, password string, expiresAt time.Time) (jwt.Claims, error) {
var (
l *ldap.Conn
err error
)
switch a.url.Scheme {
case "ldaps":
l, err = ldap.DialTLS("tcp", a.url.Host, &tls.Config{
InsecureSkipVerify: true,
})
case "ldap":
l, err = ldap.Dial("tcp", a.url.Host)
default:
log.Fatal("ldap: bad protocol: ", a.url.Scheme)
}
if err != nil {
log.Print("LDAP dial error: ", err)
return nil, err
}
if err := l.Bind(fmt.Sprintf(a.userTemplate, user), password); err != nil {
log.Print("LDAP bind error: ", err)
return nil, api.ErrInvalidAuthentication
}
return jwt.StandardClaims{
IssuedAt: time.Now().Unix(),
ExpiresAt: expiresAt.Unix(),
Subject: user,
}, nil
}