2021-04-26 09:53:06 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/cookiejar"
|
2021-05-08 18:59:31 +00:00
|
|
|
"net/url"
|
2021-05-24 12:41:40 +00:00
|
|
|
"strconv"
|
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-06-16 10:35:17 +00:00
|
|
|
"goauthentik.io/internal/constants"
|
2021-06-16 10:02:02 +00:00
|
|
|
"goauthentik.io/internal/outpost/ak"
|
2021-05-16 19:07:01 +00:00
|
|
|
"goauthentik.io/outpost/api"
|
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()
|
|
|
|
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-06-16 10:35:17 +00:00
|
|
|
Transport: newTransport(ak.SetUserAgent(ak.GetTLSTransport(), constants.OutpostUserAgent()), 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-06-13 20:00:05 +00:00
|
|
|
passed, rerr := pi.solveFlowChallenge(username, bindPW, apiClient, params.Encode(), 1)
|
|
|
|
if rerr != ldap.LDAPResultSuccess {
|
2021-05-12 16:49:15 +00:00
|
|
|
pi.log.WithField("bindDN", bindDN).WithError(err).Warning("failed to solve challenge")
|
2021-06-13 20:00:05 +00:00
|
|
|
return rerr, nil
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
|
|
|
if !passed {
|
|
|
|
return ldap.LDAPResultInvalidCredentials, nil
|
|
|
|
}
|
2021-06-13 20:00:05 +00:00
|
|
|
p, _, err := apiClient.CoreApi.CoreApplicationsCheckAccessRetrieve(context.Background(), pi.appSlug).Execute()
|
2021-05-26 09:47:23 +00:00
|
|
|
if !p.Passing {
|
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-05-24 14:09:05 +00:00
|
|
|
userInfo, _, err := 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-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-24 12:41:40 +00:00
|
|
|
type ChallengeInt interface {
|
|
|
|
GetComponent() string
|
|
|
|
GetType() api.ChallengeChoices
|
|
|
|
GetResponseErrors() map[string][]api.ErrorDetail
|
|
|
|
}
|
|
|
|
|
2021-06-13 20:00:05 +00:00
|
|
|
func (pi *ProviderInstance) solveFlowChallenge(bindDN string, password string, client *api.APIClient, urlParams string, depth int) (bool, ldap.LDAPResultCode) {
|
2021-05-24 12:41:40 +00:00
|
|
|
req := client.FlowsApi.FlowsExecutorGet(context.Background(), pi.flowSlug).Query(urlParams)
|
2021-05-16 19:07:01 +00:00
|
|
|
challenge, _, err := req.Execute()
|
2021-04-26 09:53:06 +00:00
|
|
|
if err != nil {
|
|
|
|
pi.log.WithError(err).Warning("Failed to get challenge")
|
2021-06-13 20:00:05 +00:00
|
|
|
return false, ldap.LDAPResultOperationsError
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
2021-05-24 12:41:40 +00:00
|
|
|
ch := challenge.GetActualInstance().(ChallengeInt)
|
|
|
|
pi.log.WithField("component", ch.GetComponent()).WithField("type", ch.GetType()).Debug("Got challenge")
|
|
|
|
responseReq := client.FlowsApi.FlowsExecutorSolve(context.Background(), pi.flowSlug).Query(urlParams)
|
|
|
|
switch ch.GetComponent() {
|
2021-04-26 09:53:06 +00:00
|
|
|
case "ak-stage-identification":
|
2021-05-24 19:09:18 +00:00
|
|
|
responseReq = responseReq.FlowChallengeResponseRequest(api.IdentificationChallengeResponseRequestAsFlowChallengeResponseRequest(api.NewIdentificationChallengeResponseRequest(bindDN)))
|
2021-04-26 09:53:06 +00:00
|
|
|
case "ak-stage-password":
|
2021-05-24 19:09:18 +00:00
|
|
|
responseReq = responseReq.FlowChallengeResponseRequest(api.PasswordChallengeResponseRequestAsFlowChallengeResponseRequest(api.NewPasswordChallengeResponseRequest(password)))
|
2021-05-24 12:41:40 +00:00
|
|
|
case "ak-stage-authenticator-validate":
|
|
|
|
// We only support duo as authenticator, check if that's allowed
|
|
|
|
var deviceChallenge *api.DeviceChallenge
|
|
|
|
for _, devCh := range challenge.AuthenticatorValidationChallenge.DeviceChallenges {
|
|
|
|
if devCh.DeviceClass == string(api.DEVICECLASSESENUM_DUO) {
|
|
|
|
deviceChallenge = &devCh
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if deviceChallenge == nil {
|
2021-06-13 20:00:05 +00:00
|
|
|
pi.log.Warning("got ak-stage-authenticator-validate without duo")
|
|
|
|
return false, ldap.LDAPResultOperationsError
|
2021-05-24 12:41:40 +00:00
|
|
|
}
|
|
|
|
devId, err := strconv.Atoi(deviceChallenge.DeviceUid)
|
|
|
|
if err != nil {
|
2021-06-13 20:00:05 +00:00
|
|
|
pi.log.Warning("failed to convert duo device id to int")
|
|
|
|
return false, ldap.LDAPResultOperationsError
|
2021-05-24 12:41:40 +00:00
|
|
|
}
|
|
|
|
devId32 := int32(devId)
|
2021-05-24 13:58:59 +00:00
|
|
|
inner := api.NewAuthenticatorValidationChallengeResponseRequest()
|
|
|
|
inner.Duo = &devId32
|
2021-05-24 19:09:18 +00:00
|
|
|
responseReq = responseReq.FlowChallengeResponseRequest(api.AuthenticatorValidationChallengeResponseRequestAsFlowChallengeResponseRequest(inner))
|
2021-05-07 09:46:26 +00:00
|
|
|
case "ak-stage-access-denied":
|
2021-06-13 20:00:05 +00:00
|
|
|
pi.log.Info("got ak-stage-access-denied")
|
|
|
|
return false, ldap.LDAPResultInsufficientAccessRights
|
2021-04-26 09:53:06 +00:00
|
|
|
default:
|
2021-06-13 21:08:15 +00:00
|
|
|
pi.log.WithField("component", ch.GetComponent()).Warning("unsupported challenge type")
|
2021-06-13 20:00:05 +00:00
|
|
|
return false, ldap.LDAPResultOperationsError
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
response, _, err := responseReq.Execute()
|
2021-05-24 12:41:40 +00:00
|
|
|
ch = response.GetActualInstance().(ChallengeInt)
|
|
|
|
pi.log.WithField("component", ch.GetComponent()).WithField("type", ch.GetType()).Debug("Got response")
|
|
|
|
switch ch.GetComponent() {
|
2021-05-12 16:31:44 +00:00
|
|
|
case "ak-stage-access-denied":
|
2021-06-13 20:00:05 +00:00
|
|
|
pi.log.Info("got ak-stage-access-denied")
|
|
|
|
return false, ldap.LDAPResultInsufficientAccessRights
|
2021-05-12 16:31:44 +00:00
|
|
|
}
|
2021-05-24 12:41:40 +00:00
|
|
|
if ch.GetType() == "redirect" {
|
2021-06-13 20:00:05 +00:00
|
|
|
return true, ldap.LDAPResultSuccess
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
pi.log.WithError(err).Warning("Failed to submit challenge")
|
2021-06-13 20:00:05 +00:00
|
|
|
return false, ldap.LDAPResultOperationsError
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
2021-05-24 12:41:40 +00:00
|
|
|
if len(ch.GetResponseErrors()) > 0 {
|
|
|
|
for key, errs := range ch.GetResponseErrors() {
|
2021-04-26 09:53:06 +00:00
|
|
|
for _, err := range errs {
|
2021-05-24 13:58:59 +00:00
|
|
|
pi.log.WithField("key", key).WithField("code", err.Code).WithField("msg", err.String).Warning("Flow error")
|
2021-06-13 20:00:05 +00:00
|
|
|
return false, ldap.LDAPResultInsufficientAccessRights
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-12 16:31:44 +00:00
|
|
|
if depth >= 10 {
|
2021-06-13 20:00:05 +00:00
|
|
|
pi.log.Warning("exceeded stage recursion depth")
|
|
|
|
return false, ldap.LDAPResultOperationsError
|
2021-05-12 16:31:44 +00:00
|
|
|
}
|
|
|
|
return pi.solveFlowChallenge(bindDN, password, client, urlParams, depth+1)
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|