2021-04-26 09:53:06 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"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-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-05-12 16:49:15 +00:00
|
|
|
func (pi *ProviderInstance) Bind(username string, bindDN, bindPW string, conn net.Conn) (ldap.LDAPResultCode, error) {
|
2021-05-08 18:59:31 +00:00
|
|
|
host, _, err := net.SplitHostPort(conn.RemoteAddr().String())
|
|
|
|
if err != nil {
|
|
|
|
pi.log.WithError(err).Warning("Failed to get remote IP")
|
|
|
|
return ldap.LDAPResultOperationsError, nil
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
|
2021-07-17 19:24:11 +00:00
|
|
|
fe := outpost.NewFlowExecutor(pi.flowSlug, pi.s.ac.Client.GetConfig())
|
|
|
|
fe.ApiClient().GetConfig().AddDefaultHeader("X-authentik-remote-ip", host)
|
|
|
|
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
|
|
|
|
fe.Answers[outpost.StagePassword] = bindPW
|
|
|
|
|
|
|
|
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 {
|
|
|
|
pi.log.WithField("bindDN", bindDN).WithError(err).Warning("failed to execute flow")
|
|
|
|
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-05-16 19:07:01 +00:00
|
|
|
pi.log.WithField("bindDN", bindDN).Info("Access denied for user")
|
|
|
|
return ldap.LDAPResultInsufficientAccessRights, nil
|
|
|
|
}
|
2021-04-26 09:53:06 +00:00
|
|
|
if err != nil {
|
2021-05-12 16:49:15 +00:00
|
|
|
pi.log.WithField("bindDN", bindDN).WithError(err).Warning("failed to check access")
|
2021-04-26 09:53:06 +00:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
|
|
|
}
|
2021-05-12 16:49:15 +00:00
|
|
|
pi.log.WithField("bindDN", bindDN).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-05-12 16:49:15 +00:00
|
|
|
pi.log.WithField("bindDN", bindDN).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-05-12 16:49:15 +00:00
|
|
|
pi.boundUsers[bindDN] = UserFlags{
|
2021-05-16 19:07:01 +00:00
|
|
|
UserInfo: userInfo.User,
|
|
|
|
CanSearch: pi.SearchAccessCheck(userInfo.User),
|
2021-05-04 19:49:15 +00:00
|
|
|
}
|
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-05-16 19:07:01 +00:00
|
|
|
func (pi *ProviderInstance) SearchAccessCheck(user api.User) bool {
|
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-05-04 22:03:19 +00:00
|
|
|
pi.log.WithField("group", group.Name).Info("Allowed access to search")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|