Created
June 30, 2018 13:38
-
-
Save BillyPurvis/069ed5bd774326fa6f0b686ec579aceb to your computer and use it in GitHub Desktop.
This file contains hidden or 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 handler | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "net/http" | |
| "github.com/julienschmidt/httprouter" | |
| ldap "gopkg.in/ldap.v2" | |
| ) | |
| // ConnectionDetails For LDAP | |
| // Uppercase struct fields denot public properties to be accessed | |
| type ConnectionDetails struct { | |
| CustomerID int `json:"customer_id"` | |
| Host string | |
| Port int | |
| BaseDN string | |
| Identifier string | |
| Password string | |
| } | |
| // DataFields Field list from LDAP | |
| type DataFields struct { | |
| Fields []string `json:"entry_attributes"` | |
| } | |
| // LDAPIndex POST Endpoint to retrieve LDAP connection details from Boom API | |
| func LDAPIndex(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { | |
| //TODO: Move to middlewear | |
| w.Header().Set("Content-Type", "application/json") | |
| // Decode request body into struct | |
| var credentials ConnectionDetails | |
| decoder := json.NewDecoder(r.Body) | |
| err := decoder.Decode(&credentials) | |
| // Check for errors in decoding | |
| if err != nil { | |
| panic(err) | |
| } | |
| // Make LDAP Connection | |
| data := GetEntryAttributeNames(&credentials) | |
| // Create new struct for JSON response body of attributes | |
| result := DataFields{data} | |
| json.NewEncoder(w).Encode(result) | |
| } | |
| // LDAPConnectionBind Returns LDAP Connection Binding | |
| func LDAPConnectionBind(credentials *ConnectionDetails) *ldap.Conn { | |
| // Create Connection to LDAP Server | |
| conn, err := ldap.Dial("tcp", fmt.Sprintf("%s:%d", credentials.Host, credentials.Port)) | |
| if err != nil { | |
| panic(err) | |
| } | |
| // Create LDAP Binding | |
| err = conn.Bind(credentials.Identifier, credentials.Password) | |
| if err != nil { | |
| panic(err) | |
| } | |
| // Return connection binding | |
| return conn | |
| } | |
| // LDAPSearch Return results from LDAP | |
| func LDAPSearch(credentials *ConnectionDetails) { | |
| //TODO: Make request to return just field names from DN search | |
| conn := LDAPConnectionBind(credentials) | |
| defer conn.Close() // Defer until end of function | |
| // Make Search Request | |
| searchRequest := ldap.NewSearchRequest( | |
| fmt.Sprintf("dc=%v,dc=com,dc=local", credentials.BaseDN), | |
| ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, | |
| "(&(objectClass=user))", | |
| []string{}, //TODO: create map of field names required to pass to string slice of required data from LDAP | |
| nil, | |
| ) | |
| // Make Search request | |
| sr, err := conn.Search(searchRequest) | |
| if err != nil { | |
| panic(err) | |
| } | |
| // Iterate through search results slice and print | |
| attributesSlice := sr.Entries[0].Attributes | |
| // Loop through Attributes | |
| for _, attribute := range attributesSlice { | |
| fmt.Printf("%v\n", attribute.Name) | |
| } | |
| } | |
| // GetEntryAttributeNames Returns attribute field lists for an entry | |
| func GetEntryAttributeNames(credentials *ConnectionDetails) []string { | |
| conn := LDAPConnectionBind(credentials) | |
| defer conn.Close() // Defer until end of function | |
| // Make Search request | |
| searchRequest := ldap.NewSearchRequest( | |
| fmt.Sprintf("dc=%v,dc=com,dc=local", credentials.BaseDN), | |
| ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, | |
| "(&(objectClass=user))", | |
| []string{}, //TODO: create map of field names required to pass to string slice of required data from LDAP | |
| nil, | |
| ) | |
| // Make Search Request | |
| sr, err := conn.Search(searchRequest) | |
| if err != nil { | |
| panic(err) | |
| } | |
| // Assign Attributes slice to var | |
| attributesSlice := sr.Entries[0].Attributes | |
| // Create New Slice of attribute names and return | |
| var attributeNames []string | |
| for _, attribute := range attributesSlice { | |
| attributeNames = append(attributeNames, attribute.Name) | |
| } | |
| return attributeNames | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment