-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
192 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package ldapuserdriver | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
import ( | ||
"github.com/clawio/clawiod/root" | ||
"github.com/go-kit/kit/log/levels" | ||
"gopkg.in/ldap.v2" | ||
) | ||
|
||
type driver struct { | ||
logger levels.Levels | ||
bindUsername string | ||
bindPassword string | ||
hostname string | ||
port int | ||
baseDN string | ||
filter string | ||
} | ||
|
||
func New(logger levels.Levels, | ||
bindUsername, | ||
bindPassword, | ||
hostname string, | ||
port int, | ||
baseDN string, | ||
filter string) (root.UserDriver, error) { | ||
|
||
logger.Info().Log("msg", "ldap configuration", "hostname", hostname, "port", port, "bindusername", bindUsername, "basedn", baseDN, "filter", filter) | ||
return &driver{ | ||
logger: logger, | ||
bindUsername: bindUsername, | ||
bindPassword: bindPassword, | ||
hostname: hostname, | ||
port: port, | ||
baseDN: baseDN, | ||
filter: filter, | ||
}, nil | ||
} | ||
|
||
func (c *driver) GetByCredentials(username, password string) (root.User, error) { | ||
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", c.hostname, c.port)) | ||
if err != nil { | ||
c.logger.Error().Log("error", err) | ||
return nil, err | ||
} | ||
defer l.Close() | ||
c.logger.Info().Log("msg", "connection stablished") | ||
|
||
// Reconnect with TLS | ||
/* | ||
err = l.StartTLS(&tls.Config{InsecureSkipVerify: true}) | ||
if err != nil { | ||
c.logger.Error().Log("error", err) | ||
return nil, err | ||
} | ||
*/ | ||
|
||
// First bind with a read only user | ||
err = l.Bind(c.bindUsername, c.bindPassword) | ||
if err != nil { | ||
c.logger.Error().Log("error", err) | ||
return nil, err | ||
} | ||
|
||
// Search for the given username | ||
searchRequest := ldap.NewSearchRequest( | ||
c.baseDN, | ||
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, | ||
//fmt.Sprintf("(&(objectClass=user)&(samaccountname=%s))", username), | ||
fmt.Sprintf(c.filter, username), | ||
[]string{"dn"}, | ||
nil, | ||
) | ||
|
||
sr, err := l.Search(searchRequest) | ||
if err != nil { | ||
c.logger.Error().Log("error", err) | ||
return nil, err | ||
} | ||
|
||
if len(sr.Entries) != 1 { | ||
err := userNotFoundError("user " + username + " not found") | ||
c.logger.Error().Log("error", err) | ||
return nil, err | ||
} | ||
|
||
userdn := sr.Entries[0].DN | ||
c.logger.Info().Log("msg", "user exists", "dn", userdn) | ||
|
||
// Bind as the user to verify their password | ||
err = l.Bind(userdn, password) | ||
if err != nil { | ||
c.logger.Error().Log("error", err) | ||
return nil, err | ||
} | ||
|
||
// TODO(labkode) Get more attrs from LDAP query like email and displayName at least | ||
u := &user{ | ||
username: username, | ||
} | ||
return u, nil | ||
} | ||
|
||
type user struct { | ||
username string | ||
email string | ||
displayName string | ||
} | ||
|
||
func (u *user) Username() string { | ||
return u.username | ||
} | ||
|
||
func (u *user) Email() string { | ||
return u.email | ||
} | ||
|
||
func (u *user) DisplayName() string { | ||
return u.displayName | ||
} | ||
|
||
func (u *user) ExtraAttributes() map[string]interface{} { | ||
return nil | ||
} | ||
|
||
type userNotFoundError string | ||
|
||
func (e userNotFoundError) Error() string { | ||
return string(e) | ||
} | ||
func (e userNotFoundError) Code() root.Code { | ||
return root.Code(root.CodeUserNotFound) | ||
} | ||
func (e userNotFoundError) Message() string { | ||
return string(e) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters