foodoles/auth/auth.go

51 lines
1.1 KiB
Go
Raw Permalink Normal View History

2019-11-27 10:06:24 +01:00
package auth
import (
2019-12-13 16:05:19 +01:00
"forge.cadoles.com/foodoles/config"
2019-11-27 10:06:24 +01:00
"log"
"github.com/jtblin/go-ldap-client"
2019-11-27 12:09:15 +01:00
"github.com/pkg/errors"
2019-11-27 10:06:24 +01:00
)
2019-12-13 16:05:19 +01:00
const configFile string = "server.conf"
2019-11-27 10:06:24 +01:00
// LogIn auth the client
func LogIn(username string, password string) (ok bool, user map[string]string) {
2019-11-27 12:09:15 +01:00
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))
}
2019-11-27 10:06:24 +01:00
ldapclient := &ldap.LDAPClient{
2019-11-27 12:09:15 +01:00
Base: conf.LDAP.Base,
Host: conf.LDAP.Host,
Port: conf.LDAP.Port,
2019-11-27 10:06:24 +01:00
UseSSL: false,
2019-11-27 12:09:15 +01:00
BindDN: conf.LDAP.BindDN,
BindPassword: conf.LDAP.BindPassword,
UserFilter: conf.LDAP.UserFilter,
2019-11-27 10:06:24 +01:00
GroupFilter: "(memberUid=%s)",
2019-11-27 12:09:15 +01:00
Attributes: conf.LDAP.Attributes,
2019-11-27 10:06:24 +01:00
}
2019-11-28 14:36:49 +01:00
2019-11-27 10:06:24 +01:00
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
}