Match aliases as groups

This commit is contained in:
Yohann D'ANELLO 2021-01-03 05:07:25 +01:00
parent 8d2adad509
commit 7e0ee7aba5
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
3 changed files with 30 additions and 11 deletions

View File

@ -4,11 +4,12 @@ package ldap
import (
"github.com/go-ldap/ldap/v3"
"log"
"strings"
)
// Options holds package configuration
type Options struct {
Aliases map[string]string
Aliases map[string]map[string]string
URI string
UserDn string
}
@ -22,17 +23,33 @@ type LDAP struct {
// Login tries to bind to LDAP
// Returns (true, nil) if success
func (a LDAP) Login(username string, password string) (bool, error) {
// Resolve stream alias if necessary
for aliasFor, ok := a.Cfg.Aliases[username]; ok; aliasFor, ok = a.Cfg.Aliases[username] {
log.Printf("[LDAP] Use stream alias %s for username %s", username, aliasFor)
username = aliasFor
aliasSplit := strings.SplitN(username, "__", 2)
potentialUsernames := []string{username}
for len(aliasSplit) == 2 {
alias := aliasSplit[0]
trueUsername := aliasSplit[1]
// Resolve stream alias if necessary
if aliases, ok := a.Cfg.Aliases[alias]; ok {
if _, ok := aliases[trueUsername]; ok {
log.Printf("[LDAP] Use stream alias %s for username %s", alias, trueUsername)
potentialUsernames = append(potentialUsernames, trueUsername)
}
}
}
// Try to bind as user
bindDn := "cn=" + username + "," + a.Cfg.UserDn
err := a.Conn.Bind(bindDn, password)
var err error = nil
for _, username := range potentialUsernames {
// Try to bind as user
bindDn := "cn=" + username + "," + a.Cfg.UserDn
err = a.Conn.Bind(bindDn, password)
if err == nil {
// Login succeeded if no error
return true, nil
}
}
// Login succeeded if no error
// Unable to log in
return err == nil, err
}

View File

@ -36,8 +36,10 @@ auth:
# userdn: cn=users,dc=example,dc=com
#
# # You can define aliases, to stream on stream.example.com/example with the credentials of the demo account.
# # You will have to use the streamid example__demo:password
# aliases:
# example: demo
# example:
# demo: ignored
#
## Stream forwarding ##

View File

@ -42,7 +42,7 @@ func New() *Config {
Credentials: make(map[string]string),
},
LDAP: ldap.Options{
Aliases: make(map[string]string),
Aliases: make(map[string]map[string]string),
URI: "ldap://127.0.0.1:389",
UserDn: "cn=users,dc=example,dc=com",
},