foodoles/auth/auth.go

51 lines
1.1 KiB
Go

package auth
import (
"forge.cadoles.com/foodoles/config"
"log"
"github.com/jtblin/go-ldap-client"
"github.com/pkg/errors"
)
const configFile string = "server.conf"
// LogIn auth the client
func LogIn(username string, password string) (ok bool, user map[string]string) {
var conf *config.Config
var conferr error
conf, conferr = config.NewFromFile(configFile)
if conferr != nil {
panic(errors.Wrapf(conferr, "error while loading config file '%s'", configFile))
}
ldapclient := &ldap.LDAPClient{
Base: conf.LDAP.Base,
Host: conf.LDAP.Host,
Port: conf.LDAP.Port,
UseSSL: false,
BindDN: conf.LDAP.BindDN,
BindPassword: conf.LDAP.BindPassword,
UserFilter: conf.LDAP.UserFilter,
GroupFilter: "(memberUid=%s)",
Attributes: conf.LDAP.Attributes,
}
defer ldapclient.Close()
ok, user, err := ldapclient.Authenticate(username, password)
if err != nil {
log.Printf("Error authenticating user %s: %+v", "username", err)
return
}
if !ok {
log.Printf("Authenticating failed for user %s", "username")
return
}
log.Printf("User %s authentificated", username)
return ok, user
}