2021-04-19 22:30:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
|
2021-05-04 19:49:15 +00:00
|
|
|
"github.com/nmcclain/ldap"
|
2021-09-09 13:52:24 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-11-05 09:37:30 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/bind"
|
2021-09-09 13:52:24 +00:00
|
|
|
"goauthentik.io/internal/outpost/ldap/metrics"
|
2021-09-05 17:47:30 +00:00
|
|
|
"goauthentik.io/internal/utils"
|
2021-04-19 22:30:27 +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-11-05 09:37:30 +00:00
|
|
|
req, span := bind.NewRequest(bindDN, bindPW, conn)
|
2021-07-22 17:17:34 +00:00
|
|
|
|
2021-09-09 13:52:24 +00:00
|
|
|
defer func() {
|
|
|
|
span.Finish()
|
|
|
|
metrics.Requests.With(prometheus.Labels{
|
2021-09-16 08:03:31 +00:00
|
|
|
"outpost_name": ls.ac.Outpost.Name,
|
|
|
|
"type": "bind",
|
|
|
|
"filter": "",
|
|
|
|
"dn": req.BindDN,
|
2021-11-05 09:37:30 +00:00
|
|
|
"client": req.RemoteAddr(),
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Observe(float64(span.EndTime.Sub(span.StartTime)))
|
2021-11-05 09:37:30 +00:00
|
|
|
req.Log().WithField("took-ms", span.EndTime.Sub(span.StartTime).Milliseconds()).Info("Bind request")
|
2021-09-09 13:52:24 +00:00
|
|
|
}()
|
2021-04-26 09:53:06 +00:00
|
|
|
for _, instance := range ls.providers {
|
2021-11-05 09:37:30 +00:00
|
|
|
username, err := instance.binder.GetUsername(bindDN)
|
2021-04-26 09:53:06 +00:00
|
|
|
if err == nil {
|
2021-11-05 09:37:30 +00:00
|
|
|
return instance.binder.Bind(username, req)
|
2021-05-07 09:46:26 +00:00
|
|
|
} else {
|
2021-11-05 09:37:30 +00:00
|
|
|
req.Log().WithError(err).Debug("Username not for instance")
|
2021-04-25 20:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-05 09:37:30 +00:00
|
|
|
req.Log().WithField("request", "bind").Warning("No provider found for request")
|
2021-09-09 13:52:24 +00:00
|
|
|
metrics.RequestsRejected.With(prometheus.Labels{
|
2021-09-16 08:03:31 +00:00
|
|
|
"outpost_name": ls.ac.Outpost.Name,
|
|
|
|
"type": "bind",
|
|
|
|
"reason": "no_provider",
|
|
|
|
"dn": bindDN,
|
|
|
|
"client": utils.GetIP(conn.RemoteAddr()),
|
2021-09-09 13:52:24 +00:00
|
|
|
}).Inc()
|
2021-04-26 09:53:06 +00:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
2021-04-19 22:30:27 +00:00
|
|
|
}
|