2021-04-19 22:30:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2021-07-22 17:17:34 +00:00
|
|
|
"context"
|
2021-04-19 22:30:27 +00:00
|
|
|
"net"
|
2021-05-12 16:49:15 +00:00
|
|
|
"strings"
|
2021-04-19 22:30:27 +00:00
|
|
|
|
2021-07-22 17:17:34 +00:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-07-19 11:41:29 +00:00
|
|
|
"github.com/google/uuid"
|
2021-05-04 19:49:15 +00:00
|
|
|
"github.com/nmcclain/ldap"
|
2021-07-19 11:41:29 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-04-19 22:30:27 +00:00
|
|
|
)
|
|
|
|
|
2021-07-19 11:41:29 +00:00
|
|
|
type BindRequest struct {
|
|
|
|
BindDN string
|
|
|
|
BindPW string
|
|
|
|
id string
|
|
|
|
conn net.Conn
|
|
|
|
log *log.Entry
|
2021-07-22 17:17:34 +00:00
|
|
|
ctx context.Context
|
2021-07-19 11:41:29 +00:00
|
|
|
}
|
|
|
|
|
2021-05-04 19:49:15 +00:00
|
|
|
func (ls *LDAPServer) Bind(bindDN string, bindPW string, conn net.Conn) (ldap.LDAPResultCode, error) {
|
2021-07-22 17:17:34 +00:00
|
|
|
span := sentry.StartSpan(context.TODO(), "authentik.providers.ldap.bind",
|
|
|
|
sentry.TransactionName("authentik.providers.ldap.bind"))
|
|
|
|
span.SetTag("user", bindDN)
|
|
|
|
defer span.Finish()
|
|
|
|
|
2021-05-12 16:49:15 +00:00
|
|
|
bindDN = strings.ToLower(bindDN)
|
2021-07-19 11:41:29 +00:00
|
|
|
rid := uuid.New().String()
|
|
|
|
req := BindRequest{
|
|
|
|
BindDN: bindDN,
|
|
|
|
BindPW: bindPW,
|
|
|
|
conn: conn,
|
|
|
|
log: ls.log.WithField("bindDN", bindDN).WithField("requestId", rid).WithField("client", conn.RemoteAddr().String()),
|
|
|
|
id: rid,
|
2021-07-22 17:17:34 +00:00
|
|
|
ctx: span.Context(),
|
2021-07-19 11:41:29 +00:00
|
|
|
}
|
|
|
|
req.log.Info("Bind request")
|
2021-04-26 09:53:06 +00:00
|
|
|
for _, instance := range ls.providers {
|
|
|
|
username, err := instance.getUsername(bindDN)
|
|
|
|
if err == nil {
|
2021-07-19 11:41:29 +00:00
|
|
|
return instance.Bind(username, req)
|
2021-05-07 09:46:26 +00:00
|
|
|
} else {
|
|
|
|
ls.log.WithError(err).Debug("Username not for instance")
|
2021-04-25 20:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-19 11:41:29 +00:00
|
|
|
req.log.WithField("request", "bind").Warning("No provider found for request")
|
2021-04-26 09:53:06 +00:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
2021-04-19 22:30:27 +00:00
|
|
|
}
|