2021-04-26 10:53:06 +01:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
2021-05-04 20:49:15 +01:00
|
|
|
"time"
|
2021-04-26 10:53:06 +01:00
|
|
|
|
2021-07-22 18:17:34 +01:00
|
|
|
"github.com/getsentry/sentry-go"
|
2021-04-26 10:53:06 +01:00
|
|
|
goldap "github.com/go-ldap/ldap/v3"
|
2021-05-04 20:49:15 +01:00
|
|
|
"github.com/nmcclain/ldap"
|
2021-07-19 12:41:29 +01:00
|
|
|
log "github.com/sirupsen/logrus"
|
2021-06-29 15:21:00 +01:00
|
|
|
"goauthentik.io/api"
|
2021-07-17 20:24:11 +01:00
|
|
|
"goauthentik.io/internal/outpost"
|
2021-04-26 10:53:06 +01:00
|
|
|
)
|
|
|
|
|
2021-04-29 19:26:14 +01:00
|
|
|
const ContextUserKey = "ak_user"
|
|
|
|
|
2021-04-26 10:53:06 +01:00
|
|
|
func (pi *ProviderInstance) getUsername(dn string) (string, error) {
|
2021-05-07 10:46:26 +01:00
|
|
|
if !strings.HasSuffix(strings.ToLower(dn), strings.ToLower(pi.BaseDN)) {
|
2021-04-26 10:53:06 +01:00
|
|
|
return "", errors.New("invalid base DN")
|
|
|
|
}
|
|
|
|
dns, err := goldap.ParseDN(dn)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
for _, part := range dns.RDNs {
|
|
|
|
for _, attribute := range part.Attributes {
|
2021-05-07 10:46:26 +01:00
|
|
|
if strings.ToLower(attribute.Type) == "cn" {
|
2021-04-26 10:53:06 +01:00
|
|
|
return attribute.Value, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 10:46:26 +01:00
|
|
|
return "", errors.New("failed to find cn")
|
2021-04-26 10:53:06 +01:00
|
|
|
}
|
|
|
|
|
2021-07-19 12:41:29 +01:00
|
|
|
func (pi *ProviderInstance) Bind(username string, req BindRequest) (ldap.LDAPResultCode, error) {
|
2021-07-22 18:17:34 +01:00
|
|
|
fe := outpost.NewFlowExecutor(req.ctx, pi.flowSlug, pi.s.ac.Client.GetConfig(), log.Fields{
|
2021-07-19 12:41:29 +01:00
|
|
|
"bindDN": req.BindDN,
|
|
|
|
"client": req.conn.RemoteAddr().String(),
|
|
|
|
"requestId": req.id,
|
|
|
|
})
|
|
|
|
fe.DelegateClientIP(req.conn.RemoteAddr())
|
2021-07-17 20:24:11 +01:00
|
|
|
fe.Params.Add("goauthentik.io/outpost/ldap", "true")
|
2021-05-16 20:07:01 +01:00
|
|
|
|
2021-07-17 20:24:11 +01:00
|
|
|
fe.Answers[outpost.StageIdentification] = username
|
2021-07-19 12:41:29 +01:00
|
|
|
fe.Answers[outpost.StagePassword] = req.BindPW
|
2021-07-17 20:24:11 +01:00
|
|
|
|
|
|
|
passed, err := fe.Execute()
|
2021-07-18 21:22:35 +01:00
|
|
|
if !passed {
|
|
|
|
return ldap.LDAPResultInvalidCredentials, nil
|
|
|
|
}
|
2021-07-17 20:24:11 +01:00
|
|
|
if err != nil {
|
2021-07-19 12:41:29 +01:00
|
|
|
req.log.WithError(err).Warning("failed to execute flow")
|
2021-07-17 20:24:11 +01:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
2021-04-26 10:53:06 +01:00
|
|
|
}
|
2021-07-22 18:17:34 +01:00
|
|
|
|
2021-07-17 20:24:11 +01:00
|
|
|
access, err := fe.CheckApplicationAccess(pi.appSlug)
|
|
|
|
if !access {
|
2021-07-19 12:41:29 +01:00
|
|
|
req.log.Info("Access denied for user")
|
2021-05-16 20:07:01 +01:00
|
|
|
return ldap.LDAPResultInsufficientAccessRights, nil
|
|
|
|
}
|
2021-04-26 10:53:06 +01:00
|
|
|
if err != nil {
|
2021-07-19 12:41:29 +01:00
|
|
|
req.log.WithError(err).Warning("failed to check access")
|
2021-04-26 10:53:06 +01:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
|
|
|
}
|
2021-07-19 12:41:29 +01:00
|
|
|
req.log.Info("User has access")
|
2021-07-22 18:17:34 +01:00
|
|
|
uisp := sentry.StartSpan(req.ctx, "authentik.providers.ldap.bind.user_info")
|
2021-04-29 19:26:14 +01:00
|
|
|
// Get user info to store in context
|
2021-07-17 20:24:11 +01:00
|
|
|
userInfo, _, err := fe.ApiClient().CoreApi.CoreUsersMeRetrieve(context.Background()).Execute()
|
2021-04-29 19:26:14 +01:00
|
|
|
if err != nil {
|
2021-07-19 12:41:29 +01:00
|
|
|
req.log.WithError(err).Warning("failed to get user info")
|
2021-04-29 19:26:14 +01:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
|
|
|
}
|
2021-05-04 20:49:15 +01:00
|
|
|
pi.boundUsersMutex.Lock()
|
2021-07-19 12:41:29 +01:00
|
|
|
cs := pi.SearchAccessCheck(userInfo.User)
|
|
|
|
pi.boundUsers[req.BindDN] = UserFlags{
|
2021-08-05 17:16:06 +01:00
|
|
|
UserPk: userInfo.User.Pk,
|
2021-07-19 12:41:29 +01:00
|
|
|
CanSearch: cs != nil,
|
2021-05-04 20:49:15 +01:00
|
|
|
}
|
2021-07-19 12:41:29 +01:00
|
|
|
if pi.boundUsers[req.BindDN].CanSearch {
|
|
|
|
req.log.WithField("group", cs).Info("Allowed access to search")
|
|
|
|
}
|
2021-07-22 18:17:34 +01:00
|
|
|
uisp.Finish()
|
2021-05-08 19:59:31 +01:00
|
|
|
defer pi.boundUsersMutex.Unlock()
|
2021-05-04 20:49:15 +01:00
|
|
|
pi.delayDeleteUserInfo(username)
|
2021-04-26 10:53:06 +01:00
|
|
|
return ldap.LDAPResultSuccess, nil
|
|
|
|
}
|
|
|
|
|
2021-05-04 23:03:19 +01:00
|
|
|
// SearchAccessCheck Check if the current user is allowed to search
|
2021-08-05 17:16:06 +01:00
|
|
|
func (pi *ProviderInstance) SearchAccessCheck(user api.UserSelf) *string {
|
2021-05-04 23:03:19 +01:00
|
|
|
for _, group := range user.Groups {
|
|
|
|
for _, allowedGroup := range pi.searchAllowedGroups {
|
2021-05-12 17:49:15 +01:00
|
|
|
pi.log.WithField("userGroup", group.Pk).WithField("allowedGroup", allowedGroup).Trace("Checking search access")
|
2021-05-16 20:07:01 +01:00
|
|
|
if group.Pk == allowedGroup.String() {
|
2021-07-19 12:41:29 +01:00
|
|
|
return &group.Name
|
2021-05-04 23:03:19 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 12:41:29 +01:00
|
|
|
return nil
|
2021-05-04 23:03:19 +01:00
|
|
|
}
|
2021-07-17 20:24:11 +01:00
|
|
|
|
2021-05-04 20:49:15 +01:00
|
|
|
func (pi *ProviderInstance) delayDeleteUserInfo(dn string) {
|
|
|
|
ticker := time.NewTicker(30 * time.Second)
|
|
|
|
quit := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
pi.boundUsersMutex.Lock()
|
|
|
|
delete(pi.boundUsers, dn)
|
|
|
|
pi.boundUsersMutex.Unlock()
|
|
|
|
close(quit)
|
|
|
|
case <-quit:
|
|
|
|
ticker.Stop()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|