Skip to content

Commit

Permalink
impl: helper to get a new client with different credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
TheDevMinerTV committed Oct 4, 2023
1 parent 8622caf commit 73f51cd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
22 changes: 21 additions & 1 deletion auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var (
ErrActiveDirectoryMustBeLDAPS = errors.New("ActiveDirectory servers must be connected to via LDAPS to change passwords")
)

func (l LDAP) CheckPasswordForSAMAccountName(sAMAccountName, password string) (*User, error) {
func (l *LDAP) CheckPasswordForSAMAccountName(sAMAccountName, password string) (*User, error) {
c, err := l.getConnection()
if err != nil {
return nil, err
Expand All @@ -33,6 +33,26 @@ func (l LDAP) CheckPasswordForSAMAccountName(sAMAccountName, password string) (*
return user, nil
}

func (l *LDAP) CheckPasswordForDN(dn, password string) (*User, error) {
c, err := l.getConnection()
if err != nil {
return nil, err
}
defer c.Close()

user, err := l.FindUserByDN(dn)
if err != nil {
return nil, err
}

err = c.Bind(user.DN(), password)
if err != nil {
return nil, err
}

return user, nil
}

func encodePassword(password string) (string, error) {
encoded, err := utf16le.NewEncoder().String("\"" + password + "\"")
if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ func New(config Config, user, password string) (*LDAP, error) {
return l, nil
}

func (l *LDAP) WithCredentials(dn, password string) (*LDAP, error) {
return New(l.config, dn, password)
}

func (l LDAP) getConnection() (*ldap.Conn, error) {
c, err := ldap.DialURL(l.config.Server)
if err != nil {
Expand Down

0 comments on commit 73f51cd

Please sign in to comment.