2021-04-26 09:53:06 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"strings"
|
2021-05-04 19:49:15 +00:00
|
|
|
"time"
|
2021-04-26 09:53:06 +00:00
|
|
|
|
|
|
|
goldap "github.com/go-ldap/ldap/v3"
|
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-06-29 14:21:00 +00:00
|
|
|
"goauthentik.io/api"
|
2021-07-17 19:24:11 +00:00
|
|
|
"goauthentik.io/internal/outpost"
|
2021-04-26 09:53:06 +00:00
|
|
|
)
|
|
|
|
|
2021-04-29 18:26:14 +00:00
|
|
|
const ContextUserKey = "ak_user"
|
|
|
|
|
2021-04-26 09:53:06 +00:00
|
|
|
func (pi *ProviderInstance) getUsername(dn string) (string, error) {
|
2021-05-07 09:46:26 +00:00
|
|
|
if !strings.HasSuffix(strings.ToLower(dn), strings.ToLower(pi.BaseDN)) {
|
2021-04-26 09:53:06 +00: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 09:46:26 +00:00
|
|
|
if strings.ToLower(attribute.Type) == "cn" {
|
2021-04-26 09:53:06 +00:00
|
|
|
return attribute.Value, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-07 09:46:26 +00:00
|
|
|
return "", errors.New("failed to find cn")
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 11:41:29 +00:00
|
|
|
func (pi *ProviderInstance) Bind(username string, req BindRequest) (ldap.LDAPResultCode, error) {
|
|
|
|
fe := outpost.NewFlowExecutor(pi.flowSlug, pi.s.ac.Client.GetConfig(), log.Fields{
|
|
|
|
"bindDN": req.BindDN,
|
|
|
|
"client": req.conn.RemoteAddr().String(),
|
|
|
|
"requestId": req.id,
|
|
|
|
})
|
|
|
|
fe.DelegateClientIP(req.conn.RemoteAddr())
|
2021-07-17 19:24:11 +00:00
|
|
|
fe.Params.Add("goauthentik.io/outpost/ldap", "true")
|
2021-05-16 19:07:01 +00:00
|
|
|
|
2021-07-17 19:24:11 +00:00
|
|
|
fe.Answers[outpost.StageIdentification] = username
|
2021-07-19 11:41:29 +00:00
|
|
|
fe.Answers[outpost.StagePassword] = req.BindPW
|
2021-07-17 19:24:11 +00:00
|
|
|
|
|
|
|
passed, err := fe.Execute()
|
2021-07-18 20:22:35 +00:00
|
|
|
if !passed {
|
|
|
|
return ldap.LDAPResultInvalidCredentials, nil
|
|
|
|
}
|
2021-07-17 19:24:11 +00:00
|
|
|
if err != nil {
|
2021-07-19 11:41:29 +00:00
|
|
|
req.log.WithError(err).Warning("failed to execute flow")
|
2021-07-17 19:24:11 +00:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
2021-07-17 19:24:11 +00:00
|
|
|
access, err := fe.CheckApplicationAccess(pi.appSlug)
|
|
|
|
if !access {
|
2021-07-19 11:41:29 +00:00
|
|
|
req.log.Info("Access denied for user")
|
2021-05-16 19:07:01 +00:00
|
|
|
return ldap.LDAPResultInsufficientAccessRights, nil
|
|
|
|
}
|
2021-04-26 09:53:06 +00:00
|
|
|
if err != nil {
|
2021-07-19 11:41:29 +00:00
|
|
|
req.log.WithError(err).Warning("failed to check access")
|
2021-04-26 09:53:06 +00:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
|
|
|
}
|
2021-07-19 11:41:29 +00:00
|
|
|
req.log.Info("User has access")
|
2021-04-29 18:26:14 +00:00
|
|
|
// Get user info to store in context
|
2021-07-17 19:24:11 +00:00
|
|
|
userInfo, _, err := fe.ApiClient().CoreApi.CoreUsersMeRetrieve(context.Background()).Execute()
|
2021-04-29 18:26:14 +00:00
|
|
|
if err != nil {
|
2021-07-19 11:41:29 +00:00
|
|
|
req.log.WithError(err).Warning("failed to get user info")
|
2021-04-29 18:26:14 +00:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
|
|
|
}
|
2021-05-04 19:49:15 +00:00
|
|
|
pi.boundUsersMutex.Lock()
|
2021-07-19 11:41:29 +00:00
|
|
|
cs := pi.SearchAccessCheck(userInfo.User)
|
|
|
|
pi.boundUsers[req.BindDN] = UserFlags{
|
2021-05-16 19:07:01 +00:00
|
|
|
UserInfo: userInfo.User,
|
2021-07-19 11:41:29 +00:00
|
|
|
CanSearch: cs != nil,
|
2021-05-04 19:49:15 +00:00
|
|
|
}
|
2021-07-19 11:41:29 +00:00
|
|
|
if pi.boundUsers[req.BindDN].CanSearch {
|
|
|
|
req.log.WithField("group", cs).Info("Allowed access to search")
|
|
|
|
}
|
|
|
|
|
2021-05-08 18:59:31 +00:00
|
|
|
defer pi.boundUsersMutex.Unlock()
|
2021-05-04 19:49:15 +00:00
|
|
|
pi.delayDeleteUserInfo(username)
|
2021-04-26 09:53:06 +00:00
|
|
|
return ldap.LDAPResultSuccess, nil
|
|
|
|
}
|
|
|
|
|
2021-05-04 22:03:19 +00:00
|
|
|
// SearchAccessCheck Check if the current user is allowed to search
|
2021-07-19 11:41:29 +00:00
|
|
|
func (pi *ProviderInstance) SearchAccessCheck(user api.User) *string {
|
2021-05-04 22:03:19 +00:00
|
|
|
for _, group := range user.Groups {
|
|
|
|
for _, allowedGroup := range pi.searchAllowedGroups {
|
2021-05-12 16:49:15 +00:00
|
|
|
pi.log.WithField("userGroup", group.Pk).WithField("allowedGroup", allowedGroup).Trace("Checking search access")
|
2021-05-16 19:07:01 +00:00
|
|
|
if group.Pk == allowedGroup.String() {
|
2021-07-19 11:41:29 +00:00
|
|
|
return &group.Name
|
2021-05-04 22:03:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-19 11:41:29 +00:00
|
|
|
return nil
|
2021-05-04 22:03:19 +00:00
|
|
|
}
|
2021-07-17 19:24:11 +00:00
|
|
|
|
2021-05-04 19:49:15 +00: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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|