2024-04-11 23:04:30 +02:00
|
|
|
// Copyright (C) NHR@FAU, University Erlangen-Nuremberg.
|
2022-07-29 06:29:21 +02:00
|
|
|
// All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
2021-12-08 10:03:00 +01:00
|
|
|
package auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2023-06-15 12:00:45 +02:00
|
|
|
"fmt"
|
2022-07-07 14:08:37 +02:00
|
|
|
"net/http"
|
2021-12-08 10:03:00 +01:00
|
|
|
"os"
|
|
|
|
"strings"
|
|
|
|
|
2023-08-18 10:43:06 +02:00
|
|
|
"github.com/ClusterCockpit/cc-backend/internal/config"
|
2023-08-17 12:34:30 +02:00
|
|
|
"github.com/ClusterCockpit/cc-backend/internal/repository"
|
2022-06-21 17:52:36 +02:00
|
|
|
"github.com/ClusterCockpit/cc-backend/pkg/log"
|
2022-09-07 12:24:45 +02:00
|
|
|
"github.com/ClusterCockpit/cc-backend/pkg/schema"
|
2021-12-08 10:03:00 +01:00
|
|
|
"github.com/go-ldap/ldap/v3"
|
|
|
|
)
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
type LdapAuthenticator struct {
|
2022-07-07 14:08:37 +02:00
|
|
|
syncPassword string
|
2024-03-13 17:09:36 +01:00
|
|
|
UserAttr string
|
2022-07-07 14:08:37 +02:00
|
|
|
}
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
var _ Authenticator = (*LdapAuthenticator)(nil)
|
|
|
|
|
2023-08-18 10:43:06 +02:00
|
|
|
func (la *LdapAuthenticator) Init() error {
|
2022-07-07 14:08:37 +02:00
|
|
|
la.syncPassword = os.Getenv("LDAP_ADMIN_PASSWORD")
|
|
|
|
if la.syncPassword == "" {
|
2023-01-23 18:48:06 +01:00
|
|
|
log.Warn("environment variable 'LDAP_ADMIN_PASSWORD' not set (ldap sync will not work)")
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
2022-02-14 14:37:33 +01:00
|
|
|
|
2023-08-29 09:30:57 +02:00
|
|
|
lc := config.Keys.LdapConfig
|
|
|
|
|
|
|
|
if lc.UserAttr != "" {
|
|
|
|
la.UserAttr = lc.UserAttr
|
|
|
|
} else {
|
|
|
|
la.UserAttr = "gecos"
|
|
|
|
}
|
|
|
|
|
2021-12-08 10:03:00 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
func (la *LdapAuthenticator) CanLogin(
|
2023-08-17 10:29:00 +02:00
|
|
|
user *schema.User,
|
2023-08-14 12:40:21 +02:00
|
|
|
username string,
|
2022-09-07 12:24:45 +02:00
|
|
|
rw http.ResponseWriter,
|
2024-03-13 17:09:36 +01:00
|
|
|
r *http.Request,
|
|
|
|
) (*schema.User, bool) {
|
2023-08-18 10:43:06 +02:00
|
|
|
lc := config.Keys.LdapConfig
|
|
|
|
|
2023-08-18 08:49:25 +02:00
|
|
|
if user != nil {
|
|
|
|
if user.AuthSource == schema.AuthViaLDAP {
|
|
|
|
return user, true
|
|
|
|
}
|
2023-08-14 12:40:21 +02:00
|
|
|
} else {
|
2023-08-18 10:43:06 +02:00
|
|
|
if lc.SyncUserOnLogin {
|
2023-08-14 12:40:21 +02:00
|
|
|
l, err := la.getLdapConnection(true)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("LDAP connection error")
|
|
|
|
}
|
2023-08-17 14:02:04 +02:00
|
|
|
defer l.Close()
|
2023-08-14 12:40:21 +02:00
|
|
|
|
|
|
|
// Search for the given username
|
|
|
|
searchRequest := ldap.NewSearchRequest(
|
2023-08-18 10:43:06 +02:00
|
|
|
lc.UserBase,
|
2023-08-14 12:40:21 +02:00
|
|
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
2023-08-18 10:43:06 +02:00
|
|
|
fmt.Sprintf("(&%s(uid=%s))", lc.UserFilter, username),
|
2023-08-29 09:30:57 +02:00
|
|
|
[]string{"dn", "uid", la.UserAttr}, nil)
|
2023-08-14 12:40:21 +02:00
|
|
|
|
|
|
|
sr, err := l.Search(searchRequest)
|
|
|
|
if err != nil {
|
|
|
|
log.Warn(err)
|
2023-08-18 08:49:25 +02:00
|
|
|
return nil, false
|
2023-08-14 12:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(sr.Entries) != 1 {
|
2023-08-18 08:49:25 +02:00
|
|
|
log.Warn("LDAP: User does not exist or too many entries returned")
|
|
|
|
return nil, false
|
2023-08-14 12:40:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
entry := sr.Entries[0]
|
2023-08-29 09:30:57 +02:00
|
|
|
name := entry.GetAttributeValue(la.UserAttr)
|
2023-08-17 12:34:30 +02:00
|
|
|
var roles []string
|
|
|
|
roles = append(roles, schema.GetRoleString(schema.RoleUser))
|
|
|
|
projects := make([]string, 0)
|
|
|
|
|
|
|
|
user = &schema.User{
|
|
|
|
Username: username,
|
|
|
|
Name: name,
|
|
|
|
Roles: roles,
|
|
|
|
Projects: projects,
|
|
|
|
AuthType: schema.AuthSession,
|
|
|
|
AuthSource: schema.AuthViaLDAP,
|
|
|
|
}
|
2023-08-14 12:40:21 +02:00
|
|
|
|
2023-08-17 12:34:30 +02:00
|
|
|
if err := repository.GetUserRepository().AddUser(user); err != nil {
|
|
|
|
log.Errorf("User '%s' LDAP: Insert into DB failed", username)
|
2023-08-17 14:02:04 +02:00
|
|
|
return nil, false
|
2023-08-14 12:40:21 +02:00
|
|
|
}
|
|
|
|
|
2023-08-17 14:02:04 +02:00
|
|
|
return user, true
|
2023-08-14 12:40:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-17 14:02:04 +02:00
|
|
|
return nil, false
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
func (la *LdapAuthenticator) Login(
|
2023-08-17 10:29:00 +02:00
|
|
|
user *schema.User,
|
2022-09-07 12:24:45 +02:00
|
|
|
rw http.ResponseWriter,
|
2024-03-13 17:09:36 +01:00
|
|
|
r *http.Request,
|
|
|
|
) (*schema.User, error) {
|
2022-07-07 14:08:37 +02:00
|
|
|
l, err := la.getLdapConnection(false)
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warn("Error while getting ldap connection")
|
2022-07-07 14:08:37 +02:00
|
|
|
return nil, err
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
2022-02-14 14:22:44 +01:00
|
|
|
defer l.Close()
|
2021-12-08 10:03:00 +01:00
|
|
|
|
2023-08-18 10:43:06 +02:00
|
|
|
userDn := strings.Replace(config.Keys.LdapConfig.UserBind, "{username}", user.Username, -1)
|
2022-07-07 14:08:37 +02:00
|
|
|
if err := l.Bind(userDn, r.FormValue("password")); err != nil {
|
2023-08-18 11:17:31 +02:00
|
|
|
log.Errorf("AUTH/LDAP > Authentication for user %s failed: %v",
|
2023-08-18 08:49:25 +02:00
|
|
|
user.Username, err)
|
2023-08-18 11:17:31 +02:00
|
|
|
return nil, fmt.Errorf("Authentication failed")
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
|
|
|
|
2022-07-07 14:08:37 +02:00
|
|
|
return user, nil
|
2021-12-08 10:03:00 +01:00
|
|
|
}
|
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
func (la *LdapAuthenticator) Sync() error {
|
2021-12-08 10:03:00 +01:00
|
|
|
const IN_DB int = 1
|
|
|
|
const IN_LDAP int = 2
|
|
|
|
const IN_BOTH int = 3
|
2023-08-17 12:34:30 +02:00
|
|
|
ur := repository.GetUserRepository()
|
2023-08-18 10:43:06 +02:00
|
|
|
lc := config.Keys.LdapConfig
|
2021-12-08 10:03:00 +01:00
|
|
|
|
|
|
|
users := map[string]int{}
|
2023-08-17 12:34:30 +02:00
|
|
|
usernames, err := ur.GetLdapUsernames()
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-17 12:34:30 +02:00
|
|
|
for _, username := range usernames {
|
2021-12-08 10:03:00 +01:00
|
|
|
users[username] = IN_DB
|
|
|
|
}
|
|
|
|
|
2022-07-07 14:08:37 +02:00
|
|
|
l, err := la.getLdapConnection(true)
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
2023-01-31 18:28:44 +01:00
|
|
|
log.Error("LDAP connection error")
|
2021-12-08 10:03:00 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-14 14:22:44 +01:00
|
|
|
defer l.Close()
|
2021-12-08 10:03:00 +01:00
|
|
|
|
|
|
|
ldapResults, err := l.Search(ldap.NewSearchRequest(
|
2023-08-18 10:43:06 +02:00
|
|
|
lc.UserBase,
|
2023-08-14 12:40:21 +02:00
|
|
|
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
2023-08-18 10:43:06 +02:00
|
|
|
lc.UserFilter,
|
2023-08-29 09:30:57 +02:00
|
|
|
[]string{"dn", "uid", la.UserAttr}, nil))
|
2021-12-08 10:03:00 +01:00
|
|
|
if err != nil {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warn("LDAP search error")
|
2021-12-08 10:03:00 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
newnames := map[string]string{}
|
|
|
|
for _, entry := range ldapResults.Entries {
|
|
|
|
username := entry.GetAttributeValue("uid")
|
|
|
|
if username == "" {
|
|
|
|
return errors.New("no attribute 'uid'")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, ok := users[username]
|
|
|
|
if !ok {
|
|
|
|
users[username] = IN_LDAP
|
2023-08-29 09:30:57 +02:00
|
|
|
newnames[username] = entry.GetAttributeValue(la.UserAttr)
|
2021-12-08 10:03:00 +01:00
|
|
|
} else {
|
|
|
|
users[username] = IN_BOTH
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for username, where := range users {
|
2023-08-18 10:43:06 +02:00
|
|
|
if where == IN_DB && lc.SyncDelOldUsers {
|
2023-08-17 12:34:30 +02:00
|
|
|
ur.DelUser(username)
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Debugf("sync: remove %v (does not show up in LDAP anymore)", username)
|
2021-12-08 10:03:00 +01:00
|
|
|
} else if where == IN_LDAP {
|
|
|
|
name := newnames[username]
|
2023-08-17 12:34:30 +02:00
|
|
|
|
|
|
|
var roles []string
|
|
|
|
roles = append(roles, schema.GetRoleString(schema.RoleUser))
|
|
|
|
projects := make([]string, 0)
|
|
|
|
|
|
|
|
user := &schema.User{
|
|
|
|
Username: username,
|
|
|
|
Name: name,
|
|
|
|
Roles: roles,
|
|
|
|
Projects: projects,
|
|
|
|
AuthSource: schema.AuthViaLDAP,
|
|
|
|
}
|
|
|
|
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Debugf("sync: add %v (name: %v, roles: [user], ldap: true)", username, name)
|
2023-08-17 12:34:30 +02:00
|
|
|
if err := ur.AddUser(user); err != nil {
|
|
|
|
log.Errorf("User '%s' LDAP: Insert into DB failed", username)
|
2021-12-08 10:03:00 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-07-07 14:08:37 +02:00
|
|
|
|
2022-09-07 12:24:45 +02:00
|
|
|
func (la *LdapAuthenticator) getLdapConnection(admin bool) (*ldap.Conn, error) {
|
2023-08-18 10:43:06 +02:00
|
|
|
lc := config.Keys.LdapConfig
|
|
|
|
conn, err := ldap.DialURL(lc.Url)
|
2022-07-07 14:08:37 +02:00
|
|
|
if err != nil {
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warn("LDAP URL dial failed")
|
2022-07-07 14:08:37 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if admin {
|
2023-08-18 10:43:06 +02:00
|
|
|
if err := conn.Bind(lc.SearchDN, la.syncPassword); err != nil {
|
2022-07-07 14:08:37 +02:00
|
|
|
conn.Close()
|
2023-02-01 11:58:27 +01:00
|
|
|
log.Warn("LDAP connection bind failed")
|
2022-07-07 14:08:37 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return conn, nil
|
|
|
|
}
|