Skip to content

Commit

Permalink
superusers must not reset password by email
Browse files Browse the repository at this point in the history
  • Loading branch information
lillian committed Dec 26, 2023
1 parent c97c89a commit 7c0d313
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion internal/auth/ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"crypto/tls"
"fmt"
"os"
"strings"

"github.com/go-ldap/ldap"
)

var invalidGroups = []string{"cn=operations", "cn=hacklab-sudoer", "cn=board", "ou=permissions"}

func GetEmailFromUsername(bindDN, bindPassword, targetUsername string) (string, error) {
ldapURL := os.Getenv("LDAP_URL")
if ldapURL == "" {
Expand All @@ -30,7 +33,7 @@ func GetEmailFromUsername(bindDN, bindPassword, targetUsername string) (string,
"ou=people,dc=hacklab,dc=to",
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
fmt.Sprintf("(&(uid=%s))", targetUsername),
[]string{"mail"},
[]string{"mail", "memberOf"},
nil,
)

Expand All @@ -40,6 +43,14 @@ func GetEmailFromUsername(bindDN, bindPassword, targetUsername string) (string,
}

for _, entry := range res.Entries {
for _, val := range entry.GetAttributeValues("memberOf") {
for _, g := range invalidGroups {
if strings.Contains(val, g) {
return "", fmt.Errorf("user not allowed to reset password by email")
}
}
}
//lint:ignore SA4004 "only one entry should ever be found"
return entry.GetAttributeValue("mail"), nil
}

Expand Down

0 comments on commit 7c0d313

Please sign in to comment.