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"
|
2021-05-04 19:49:15 +00:00
|
|
|
"github.com/nmcclain/ldap"
|
2021-05-16 19:07:01 +00:00
|
|
|
"goauthentik.io/outpost/api"
|
|
|
|
"goauthentik.io/outpost/pkg"
|
|
|
|
"goauthentik.io/outpost/pkg/ak"
|
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-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
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
|
2021-05-08 18:59:31 +00:00
|
|
|
// Create new http client that also sets the correct ip
|
2021-05-16 19:07:01 +00:00
|
|
|
config := api.NewConfiguration()
|
|
|
|
// Carry over the bearer authentication, so that failed login attempts are attributed to the outpost
|
|
|
|
config.DefaultHeader = pi.s.ac.Client.GetConfig().DefaultHeader
|
|
|
|
config.Host = pi.s.ac.Client.GetConfig().Host
|
|
|
|
config.Scheme = pi.s.ac.Client.GetConfig().Scheme
|
|
|
|
config.HTTPClient = &http.Client{
|
2021-04-26 09:53:06 +00:00
|
|
|
Jar: jar,
|
2021-05-16 19:07:01 +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-16 19:07:01 +00:00
|
|
|
// create the API client, with the transport
|
|
|
|
apiClient := api.NewAPIClient(config)
|
|
|
|
|
2021-05-08 18:59:31 +00:00
|
|
|
params := url.Values{}
|
|
|
|
params.Add("goauthentik.io/outpost/ldap", "true")
|
2021-05-16 19:07:01 +00:00
|
|
|
passed, err := pi.solveFlowChallenge(username, bindPW, apiClient, 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
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
r, err := pi.s.ac.Client.CoreApi.CoreApplicationsCheckAccessRetrieve(context.Background(), pi.appSlug).Execute()
|
|
|
|
if r.StatusCode == 403 {
|
|
|
|
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-05-16 19:07:01 +00:00
|
|
|
userInfo, _, err := pi.s.ac.Client.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-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-16 19:07:01 +00:00
|
|
|
func (pi *ProviderInstance) solveFlowChallenge(bindDN string, password string, client *api.APIClient, urlParams string, depth int) (bool, error) {
|
|
|
|
req := client.FlowsApi.FlowsExecutorGet(context.Background(), pi.flowSlug)
|
|
|
|
req.Query(urlParams)
|
|
|
|
challenge, _, err := req.Execute()
|
2021-04-26 09:53:06 +00:00
|
|
|
if err != nil {
|
|
|
|
pi.log.WithError(err).Warning("Failed to get challenge")
|
|
|
|
return false, err
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
pi.log.WithField("component", challenge.Component).WithField("type", challenge.Type).Debug("Got challenge")
|
|
|
|
responseReq := client.FlowsApi.FlowsExecutorSolve(context.Background(), pi.flowSlug)
|
|
|
|
responseReq.Query(urlParams)
|
|
|
|
switch *challenge.Component {
|
2021-04-26 09:53:06 +00:00
|
|
|
case "ak-stage-identification":
|
2021-05-16 19:07:01 +00:00
|
|
|
responseReq.RequestBody(map[string]interface{}{
|
|
|
|
"uid_field": bindDN,
|
|
|
|
})
|
2021-04-26 09:53:06 +00:00
|
|
|
case "ak-stage-password":
|
2021-05-16 19:07:01 +00:00
|
|
|
responseReq.RequestBody(map[string]interface{}{
|
|
|
|
"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:
|
2021-05-16 19:07:01 +00:00
|
|
|
return false, fmt.Errorf("unsupported challenge type: %s", *challenge.Component)
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
response, _, err := responseReq.Execute()
|
|
|
|
pi.log.WithField("component", response.Component).WithField("type", response.Type).Debug("Got response")
|
|
|
|
switch *response.Component {
|
2021-05-12 16:31:44 +00:00
|
|
|
case "ak-stage-access-denied":
|
|
|
|
return false, errors.New("got ak-stage-access-denied")
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
if response.Type == "redirect" {
|
2021-04-26 09:53:06 +00:00
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
pi.log.WithError(err).Warning("Failed to submit challenge")
|
|
|
|
return false, err
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
if len(*response.ResponseErrors) > 0 {
|
|
|
|
for key, errs := range *response.ResponseErrors {
|
2021-04-26 09:53:06 +00:00
|
|
|
for _, err := range errs {
|
2021-05-16 19:07:01 +00:00
|
|
|
pi.log.WithField("key", key).WithField("code", err.Code).Debug(err.String)
|
2021-04-26 09:53:06 +00:00
|
|
|
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
|
|
|
}
|