2021-04-26 09:53:06 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
2021-05-08 18:59:31 +00:00
|
|
|
"net/url"
|
2021-04-26 09:53:06 +00:00
|
|
|
"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"
|
|
|
|
httptransport "github.com/go-openapi/runtime/client"
|
2021-05-04 19:49:15 +00:00
|
|
|
"github.com/nmcclain/ldap"
|
2021-05-15 22:01:16 +00:00
|
|
|
"goauthentik.io/outpost/pkg"
|
|
|
|
"goauthentik.io/outpost/pkg/ak"
|
2021-04-26 09:53:06 +00:00
|
|
|
"goauthentik.io/outpost/pkg/client/core"
|
|
|
|
"goauthentik.io/outpost/pkg/client/flows"
|
2021-05-04 22:03:19 +00:00
|
|
|
"goauthentik.io/outpost/pkg/models"
|
2021-04-26 09:53:06 +00:00
|
|
|
)
|
|
|
|
|
2021-04-29 18:26:14 +00:00
|
|
|
const ContextUserKey = "ak_user"
|
|
|
|
|
2021-04-26 13:35:56 +00:00
|
|
|
type UIDResponse struct {
|
|
|
|
UIDFIeld string `json:"uid_field"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type PasswordResponse struct {
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
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-04-26 09:53:06 +00:00
|
|
|
jar, err := cookiejar.New(nil)
|
|
|
|
if err != nil {
|
|
|
|
pi.log.WithError(err).Warning("Failed to create cookiejar")
|
|
|
|
return ldap.LDAPResultOperationsError, nil
|
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
// Create new http client that also sets the correct ip
|
2021-04-26 09:53:06 +00:00
|
|
|
client := &http.Client{
|
|
|
|
Jar: jar,
|
2021-05-15 22:01:16 +00:00
|
|
|
Transport: newTransport(ak.SetUserAgent(ak.GetTLSTransport(), pkg.UserAgent()), map[string]string{
|
2021-05-08 18:59:31 +00:00
|
|
|
"X-authentik-remote-ip": host,
|
|
|
|
}),
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
2021-05-08 18:59:31 +00:00
|
|
|
params := url.Values{}
|
|
|
|
params.Add("goauthentik.io/outpost/ldap", "true")
|
2021-05-12 16:31:44 +00:00
|
|
|
passed, err := pi.solveFlowChallenge(username, bindPW, client, params.Encode(), 1)
|
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 solve challenge")
|
2021-04-26 09:53:06 +00:00
|
|
|
return ldap.LDAPResultOperationsError, nil
|
|
|
|
}
|
|
|
|
if !passed {
|
|
|
|
return ldap.LDAPResultInvalidCredentials, nil
|
|
|
|
}
|
|
|
|
_, err = pi.s.ac.Client.Core.CoreApplicationsCheckAccess(&core.CoreApplicationsCheckAccessParams{
|
|
|
|
Slug: pi.appSlug,
|
|
|
|
Context: context.Background(),
|
|
|
|
HTTPClient: client,
|
|
|
|
}, httptransport.PassThroughAuth)
|
|
|
|
if err != nil {
|
|
|
|
if _, denied := err.(*core.CoreApplicationsCheckAccessForbidden); denied {
|
2021-05-12 16:49:15 +00:00
|
|
|
pi.log.WithField("bindDN", bindDN).Info("Access denied for user")
|
2021-04-26 13:35:56 +00:00
|
|
|
return ldap.LDAPResultInsufficientAccessRights, nil
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
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
|
|
|
|
userInfo, err := pi.s.ac.Client.Core.CoreUsersMe(&core.CoreUsersMeParams{
|
2021-05-04 19:49:15 +00:00
|
|
|
Context: context.Background(),
|
2021-04-29 18:26:14 +00:00
|
|
|
HTTPClient: client,
|
|
|
|
}, httptransport.PassThroughAuth)
|
|
|
|
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{
|
|
|
|
UserInfo: *userInfo.Payload.User,
|
2021-05-04 22:03:19 +00:00
|
|
|
CanSearch: pi.SearchAccessCheck(userInfo.Payload.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
|
|
|
|
func (pi *ProviderInstance) SearchAccessCheck(user *models.User) bool {
|
|
|
|
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")
|
|
|
|
if group.Pk.String() == 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-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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
2021-05-12 16:31:44 +00:00
|
|
|
func (pi *ProviderInstance) solveFlowChallenge(bindDN string, password string, client *http.Client, urlParams string, depth int) (bool, error) {
|
2021-04-26 09:53:06 +00:00
|
|
|
challenge, err := pi.s.ac.Client.Flows.FlowsExecutorGet(&flows.FlowsExecutorGetParams{
|
|
|
|
FlowSlug: pi.flowSlug,
|
2021-05-08 18:59:31 +00:00
|
|
|
Query: urlParams,
|
2021-04-26 09:53:06 +00:00
|
|
|
Context: context.Background(),
|
|
|
|
HTTPClient: client,
|
2021-05-08 18:59:31 +00:00
|
|
|
}, pi.s.ac.Auth)
|
2021-04-26 09:53:06 +00:00
|
|
|
if err != nil {
|
|
|
|
pi.log.WithError(err).Warning("Failed to get challenge")
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
pi.log.WithField("component", challenge.Payload.Component).WithField("type", *challenge.Payload.Type).Debug("Got challenge")
|
|
|
|
responseParams := &flows.FlowsExecutorSolveParams{
|
|
|
|
FlowSlug: pi.flowSlug,
|
2021-05-08 18:59:31 +00:00
|
|
|
Query: urlParams,
|
2021-04-26 09:53:06 +00:00
|
|
|
Context: context.Background(),
|
|
|
|
HTTPClient: client,
|
|
|
|
}
|
|
|
|
switch challenge.Payload.Component {
|
|
|
|
case "ak-stage-identification":
|
|
|
|
responseParams.Data = &UIDResponse{UIDFIeld: bindDN}
|
|
|
|
case "ak-stage-password":
|
|
|
|
responseParams.Data = &PasswordResponse{Password: password}
|
2021-05-07 09:46:26 +00:00
|
|
|
case "ak-stage-access-denied":
|
|
|
|
return false, errors.New("got ak-stage-access-denied")
|
2021-04-26 09:53:06 +00:00
|
|
|
default:
|
|
|
|
return false, fmt.Errorf("unsupported challenge type: %s", challenge.Payload.Component)
|
|
|
|
}
|
2021-05-08 18:59:31 +00:00
|
|
|
response, err := pi.s.ac.Client.Flows.FlowsExecutorSolve(responseParams, pi.s.ac.Auth)
|
2021-04-26 09:53:06 +00:00
|
|
|
pi.log.WithField("component", response.Payload.Component).WithField("type", *response.Payload.Type).Debug("Got response")
|
2021-05-12 16:31:44 +00:00
|
|
|
switch response.Payload.Component {
|
|
|
|
case "ak-stage-access-denied":
|
|
|
|
return false, errors.New("got ak-stage-access-denied")
|
|
|
|
}
|
2021-04-26 09:53:06 +00:00
|
|
|
if *response.Payload.Type == "redirect" {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
pi.log.WithError(err).Warning("Failed to submit challenge")
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if len(response.Payload.ResponseErrors) > 0 {
|
|
|
|
for key, errs := range response.Payload.ResponseErrors {
|
|
|
|
for _, err := range errs {
|
|
|
|
pi.log.WithField("key", key).WithField("code", *err.Code).Debug(*err.String)
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 16:31:44 +00:00
|
|
|
if depth >= 10 {
|
|
|
|
return false, errors.New("exceeded stage recursion depth")
|
|
|
|
}
|
|
|
|
return pi.solveFlowChallenge(bindDN, password, client, urlParams, depth+1)
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|