mirror of https://github.com/grafana/grafana
Initial work on ldap support, #1450
parent
2c7d33cdfa
commit
eb793f7feb
@ -0,0 +1,56 @@ |
||||
package ldapauth |
||||
|
||||
import ( |
||||
"errors" |
||||
"fmt" |
||||
"net/url" |
||||
|
||||
"github.com/gogits/gogs/modules/ldap" |
||||
"github.com/grafana/grafana/pkg/log" |
||||
"github.com/grafana/grafana/pkg/setting" |
||||
) |
||||
|
||||
var ( |
||||
ErrInvalidCredentials = errors.New("Invalid Username or Password") |
||||
) |
||||
|
||||
func Login(username, password string) error { |
||||
url, err := url.Parse(setting.LdapUrls[0]) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
log.Info("Host: %v", url.Host) |
||||
conn, err := ldap.Dial("tcp", url.Host) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
defer conn.Close() |
||||
|
||||
bindFormat := "cn=%s,dc=grafana,dc=org" |
||||
|
||||
nx := fmt.Sprintf(bindFormat, username) |
||||
err = conn.Bind(nx, password) |
||||
|
||||
if err != nil { |
||||
if ldapErr, ok := err.(*ldap.Error); ok { |
||||
if ldapErr.ResultCode == 49 { |
||||
return ErrInvalidCredentials |
||||
} |
||||
} |
||||
return err |
||||
} |
||||
return nil |
||||
|
||||
// search := ldap.NewSearchRequest(url.Path,
|
||||
// ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
|
||||
// fmt.Sprintf(ls.Filter, name),
|
||||
// []string{ls.AttributeUsername, ls.AttributeName, ls.AttributeSurname, ls.AttributeMail},
|
||||
// nil)
|
||||
// sr, err := l.Search(search)
|
||||
// if err != nil {
|
||||
// log.Debug("LDAP Authen OK but not in filter %s", name)
|
||||
// return "", "", "", "", false
|
||||
// }
|
||||
} |
||||
@ -0,0 +1,73 @@ |
||||
package auth |
||||
|
||||
import ( |
||||
"errors" |
||||
|
||||
"github.com/grafana/grafana/pkg/bus" |
||||
m "github.com/grafana/grafana/pkg/models" |
||||
"github.com/grafana/grafana/pkg/util" |
||||
) |
||||
|
||||
var ( |
||||
ErrInvalidCredentials = errors.New("Invalid Username or Password") |
||||
) |
||||
|
||||
type LoginSettings struct { |
||||
LdapEnabled bool |
||||
} |
||||
|
||||
type LdapFilterToOrg struct { |
||||
Filter string |
||||
OrgId int |
||||
OrgRole string |
||||
} |
||||
|
||||
type LdapSettings struct { |
||||
Enabled bool |
||||
Hosts []string |
||||
UseSSL bool |
||||
BindDN string |
||||
AttrUsername string |
||||
AttrName string |
||||
AttrSurname string |
||||
AttrMail string |
||||
Filters []LdapFilterToOrg |
||||
} |
||||
|
||||
type AuthSource interface { |
||||
AuthenticateUser(username, password string) (*m.User, error) |
||||
} |
||||
|
||||
type GetAuthSourcesQuery struct { |
||||
Sources []AuthSource |
||||
} |
||||
|
||||
func init() { |
||||
bus.AddHandler("auth", GetAuthSources) |
||||
} |
||||
|
||||
func GetAuthSources(query *GetAuthSourcesQuery) error { |
||||
query.Sources = []AuthSource{&GrafanaDBAuthSource{}} |
||||
return nil |
||||
} |
||||
|
||||
type GrafanaDBAuthSource struct { |
||||
} |
||||
|
||||
func (s *GrafanaDBAuthSource) AuthenticateUser(username, password string) (*m.User, error) { |
||||
userQuery := m.GetUserByLoginQuery{LoginOrEmail: username} |
||||
err := bus.Dispatch(&userQuery) |
||||
|
||||
if err != nil { |
||||
return nil, ErrInvalidCredentials |
||||
} |
||||
|
||||
user := userQuery.Result |
||||
|
||||
passwordHashed := util.EncodePassword(password, user.Salt) |
||||
if passwordHashed != user.Password { |
||||
return nil, ErrInvalidCredentials |
||||
} |
||||
|
||||
return user, nil |
||||
} |
||||
@ -0,0 +1,19 @@ |
||||
package setting |
||||
|
||||
type LdapFilterToOrg struct { |
||||
Filter string |
||||
OrgId int |
||||
OrgRole string |
||||
} |
||||
|
||||
type LdapSettings struct { |
||||
Enabled bool |
||||
Hosts []string |
||||
UseSSL bool |
||||
BindDN string |
||||
AttrUsername string |
||||
AttrName string |
||||
AttrSurname string |
||||
AttrMail string |
||||
Filters []LdapFilterToOrg |
||||
} |
||||
Loading…
Reference in new issue