Created
May 20, 2015 07:09
-
-
Save Zenithar/f5637fe82d40d3ad310b to your computer and use it in GitHub Desktop.
Active Directory Bind authentication with User search (GO)
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
package main | |
import ( | |
"fmt" | |
"log" | |
"github.com/nmcclain/ldap" | |
) | |
var ( | |
LdapServer = "192.168.0.254" | |
LdapPort = 389 | |
BindDN = "%s@example.foo.com" | |
BaseDN = "CN=Users,DC=example,DC=foo,DC=com" | |
Filter = "(sAMAccountName=%s)" | |
Attributes = []string{"sAMAccountName", "givenName", "sn", "mail", "memberOf", "jpegPhoto", "photo", "userPrincipalName"} | |
Username = "user1" | |
Password = "toto" | |
) | |
func main() { | |
l, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", LdapServer, LdapPort)) | |
if err != nil { | |
log.Fatalf("ERROR: %s\n", err.Error()) | |
} | |
defer l.Close() | |
ldapUser := fmt.Sprintf(BindDN, Username) | |
err = l.Bind(ldapUser, Password) | |
if err != nil { | |
fmt.Println(err) | |
} else { | |
fmt.Println("Not to get cocky but I think it worked!") | |
} | |
search := ldap.NewSearchRequest( | |
BaseDN, | |
ldap.ScopeWholeSubtree, | |
ldap.NeverDerefAliases, | |
0, | |
0, | |
false, | |
fmt.Sprintf(Filter, Username), | |
Attributes, | |
nil) | |
sr, err := l.Search(search) | |
if err != nil { | |
log.Fatalf("ERROR: %s\n", err.Error()) | |
return | |
} | |
log.Printf("Search: %s -> num of entries = %d\n", search.Filter, len(sr.Entries)) | |
sr.PrettyPrint(0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment