2021-04-19 22:30:27 +00:00
|
|
|
package ldap
|
|
|
|
|
|
|
|
import (
|
2021-05-16 19:07:01 +00:00
|
|
|
"context"
|
2021-04-26 09:53:06 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2021-05-08 18:59:31 +00:00
|
|
|
"net/http"
|
2021-04-26 09:53:06 +00:00
|
|
|
"strings"
|
2021-05-08 18:59:31 +00:00
|
|
|
"sync"
|
2021-04-26 09:53:06 +00:00
|
|
|
|
2021-05-04 22:03:19 +00:00
|
|
|
"github.com/go-openapi/strfmt"
|
2021-04-19 22:30:27 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ls *LDAPServer) Refresh() error {
|
2021-05-16 19:35:23 +00:00
|
|
|
outposts, _, err := ls.ac.Client.OutpostsApi.OutpostsLdapList(context.Background()).Execute()
|
2021-04-26 09:53:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
if len(outposts.Results) < 1 {
|
2021-04-26 09:53:06 +00:00
|
|
|
return errors.New("no ldap provider defined")
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
providers := make([]*ProviderInstance, len(outposts.Results))
|
|
|
|
for idx, provider := range outposts.Results {
|
|
|
|
userDN := strings.ToLower(fmt.Sprintf("ou=users,%s", *provider.BaseDn))
|
|
|
|
groupDN := strings.ToLower(fmt.Sprintf("ou=groups,%s", *provider.BaseDn))
|
2021-04-26 09:53:06 +00:00
|
|
|
providers[idx] = &ProviderInstance{
|
2021-05-16 19:07:01 +00:00
|
|
|
BaseDN: *provider.BaseDn,
|
2021-05-04 22:03:19 +00:00
|
|
|
GroupDN: groupDN,
|
|
|
|
UserDN: userDN,
|
2021-05-16 19:07:01 +00:00
|
|
|
appSlug: provider.ApplicationSlug,
|
|
|
|
flowSlug: provider.BindFlowSlug,
|
|
|
|
searchAllowedGroups: []*strfmt.UUID{(*strfmt.UUID)(provider.SearchGroup.Get())},
|
2021-05-08 18:59:31 +00:00
|
|
|
boundUsersMutex: sync.RWMutex{},
|
|
|
|
boundUsers: make(map[string]UserFlags),
|
2021-05-04 22:03:19 +00:00
|
|
|
s: ls,
|
|
|
|
log: log.WithField("logger", "authentik.outpost.ldap").WithField("provider", provider.Name),
|
2021-04-26 09:53:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ls.providers = providers
|
|
|
|
ls.log.Info("Update providers")
|
2021-04-19 22:30:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-06 21:07:13 +00:00
|
|
|
func (ls *LDAPServer) StartHTTPServer() error {
|
|
|
|
listen := "0.0.0.0:4180" // same port as proxy
|
|
|
|
m := http.NewServeMux()
|
|
|
|
m.HandleFunc("/akprox/ping", func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.WriteHeader(204)
|
|
|
|
})
|
|
|
|
ls.log.WithField("listen", listen).Info("Starting http server")
|
|
|
|
return http.ListenAndServe(listen, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LDAPServer) StartLDAPServer() error {
|
2021-04-26 12:46:29 +00:00
|
|
|
listen := "0.0.0.0:3389"
|
2021-06-06 21:07:13 +00:00
|
|
|
ls.log.WithField("listen", listen).Info("Starting ldap server")
|
|
|
|
return ls.s.ListenAndServe(listen)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ls *LDAPServer) Start() error {
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(2)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
err := ls.StartHTTPServer()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
err := ls.StartLDAPServer()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
wg.Wait()
|
2021-04-19 22:30:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-05-08 18:59:31 +00:00
|
|
|
|
|
|
|
type transport struct {
|
|
|
|
headers map[string]string
|
2021-05-16 19:07:01 +00:00
|
|
|
inner http.RoundTripper
|
2021-05-08 18:59:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *transport) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
|
|
for key, value := range t.headers {
|
|
|
|
req.Header.Add(key, value)
|
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
return t.inner.RoundTrip(req)
|
2021-05-08 18:59:31 +00:00
|
|
|
}
|
2021-05-16 19:07:01 +00:00
|
|
|
func newTransport(inner http.RoundTripper, headers map[string]string) *transport {
|
|
|
|
return &transport{
|
|
|
|
inner: inner,
|
|
|
|
headers: headers,
|
|
|
|
}
|
2021-05-08 18:59:31 +00:00
|
|
|
}
|