bagage/auth_ldap.go

138 lines
3.1 KiB
Go
Raw Permalink Normal View History

2021-08-23 18:40:03 +00:00
package main
import (
"errors"
"fmt"
"net/http"
"github.com/go-ldap/ldap/v3"
)
/* Check credentials against LDAP */
type LdapPreAuth struct {
WithConfig *Config
OnWrongPassword ErrorHandler
OnFailure ErrorHandler
OnCreds CredsHandler
}
func (l LdapPreAuth) WithCreds(username, password string) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
2021-11-19 18:54:49 +00:00
var e *LdapWrongPasswordError
2021-08-23 18:40:03 +00:00
2021-11-19 18:54:49 +00:00
access_key, secret_key, err := LdapGetS3(l.WithConfig, username, password)
2021-11-20 08:53:05 +00:00
2021-11-19 18:54:49 +00:00
if err == nil {
l.OnCreds.WithCreds(access_key, secret_key).ServeHTTP(w, r)
} else if errors.As(err, &e) {
2021-08-23 18:40:03 +00:00
l.OnWrongPassword.WithError(err).ServeHTTP(w, r)
2021-11-19 18:54:49 +00:00
} else {
l.OnFailure.WithError(e).ServeHTTP(w, r)
2021-08-23 18:40:03 +00:00
}
})
}
/**
* Private logic
*/
type ldapConnector struct {
conn *ldap.Conn
config *Config
userDn string
}
2021-11-19 18:54:49 +00:00
type LdapError struct {
Username string
2021-11-20 08:53:05 +00:00
Err error
2021-11-19 18:54:49 +00:00
}
2021-11-20 08:53:05 +00:00
func (e *LdapError) Error() string { return "ldap error for " + e.Username + ": " + e.Err.Error() }
type LdapWrongPasswordError struct{ LdapError }
2021-11-19 18:54:49 +00:00
func LdapGetS3(c *Config, username, password string) (access_key, secret_key string, werr error) {
// 1. Connect to the server
conn, err := ldapConnect(c)
if err != nil {
2021-11-20 08:53:05 +00:00
werr = &LdapError{username, err}
2021-11-19 18:54:49 +00:00
return
}
defer conn.Close()
// 2. Authenticate with provided credentials
// @FIXME we should better check the error, it could also be due to an LDAP error
err = conn.auth(username, password)
if err != nil {
2021-11-20 08:53:05 +00:00
werr = &LdapWrongPasswordError{LdapError{username, err}}
2021-11-19 18:54:49 +00:00
return
}
// 3. Fetch user's profile
profile, err := conn.profile()
if err != nil {
2021-11-20 08:53:05 +00:00
werr = &LdapError{username, err}
2021-11-19 18:54:49 +00:00
return
}
// 4. Basic checks upon users' attributes
access_key = profile.GetAttributeValue("garage_s3_access_key")
secret_key = profile.GetAttributeValue("garage_s3_secret_key")
if access_key == "" || secret_key == "" {
err = errors.New(fmt.Sprintf("Either access key or secret key is missing in LDAP for %s", conn.userDn))
2021-11-20 08:53:05 +00:00
werr = &LdapError{username, err}
2021-11-19 18:54:49 +00:00
return
}
// 5. Send fetched credentials to the next middleware
return
}
2021-08-23 18:40:03 +00:00
func ldapConnect(c *Config) (ldapConnector, error) {
ldapSock, err := ldap.Dial("tcp", c.LdapServer)
if err != nil {
return ldapConnector{}, err
}
return ldapConnector{
conn: ldapSock,
config: c,
}, nil
}
func (l *ldapConnector) auth(username, password string) error {
l.userDn = fmt.Sprintf("%s=%s,%s", l.config.UserNameAttr, username, l.config.UserBaseDN)
return l.conn.Bind(l.userDn, password)
}
func (l *ldapConnector) profile() (*ldap.Entry, error) {
searchRequest := ldap.NewSearchRequest(
l.userDn,
ldap.ScopeBaseObject,
ldap.NeverDerefAliases,
0,
0,
false,
"(objectClass=*)",
[]string{"garage_s3_access_key", "garage_s3_secret_key"},
nil)
sr, err := l.conn.Search(searchRequest)
if err != nil {
return nil, err
}
if len(sr.Entries) != 1 {
2021-08-23 19:55:52 +00:00
return nil, errors.New(fmt.Sprintf("Wrong number of LDAP entries, expected 1, got %d", len(sr.Entries)))
2021-08-23 18:40:03 +00:00
}
return sr.Entries[0], nil
}
func (l *ldapConnector) Close() {
if l.conn != nil {
l.conn.Close()
l.conn = nil
}
}