-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Ldap authentication #369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jdoucerain
wants to merge
28
commits into
jpillora:master
Choose a base branch
from
jdoucerain:LDAP-authentication
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Ldap authentication #369
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
6c1f269
ldap authentication
jdoucerain 4bc9fc4
new --ldap-config in README.md
jdoucerain 6c50813
remove comments in ldap.go
jdoucerain 5b77520
add user in debugging log when too many entries
jdoucerain 79c0a67
ca file debug
jdoucerain 6f7bc5d
Merge branch 'jpillora:master' into LDAP-authentication
jdoucerain 00836b1
allow combination of both LDAP and local authentication and give ldap…
jdoucerain f7cdb96
a bit improved version of the password validation keeping the ability…
jdoucerain 7c0e741
Changes as recommended by J.Pillora
jdoucerain 2d7c977
markdown adjustments
jdoucerain 8f96052
markdown adjustments
jdoucerain 111b545
ldap authentication
jdoucerain 505ba02
new --ldap-config in README.md
jdoucerain ba54e8d
remove comments in ldap.go
jdoucerain 92b20ba
add user in debugging log when too many entries
jdoucerain 4708fe1
ca file debug
jdoucerain 40da157
allow combination of both LDAP and local authentication and give ldap…
jdoucerain 69a8ce0
a bit improved version of the password validation keeping the ability…
jdoucerain cfb1d00
Changes as recommended by J.Pillora
jdoucerain 659c06d
markdown adjustments
jdoucerain 3e05ec5
markdown adjustments
jdoucerain 62920af
Merge branch 'LDAP-authentication' of https://github.com/jdoucerain/c…
jdoucerain b66e317
markdown adjustments
jdoucerain 6b87e3f
MD again
jdoucerain 3f92a77
Merge branch 'master' of https://github.com/jpillora/chisel into LDAP…
jdoucerain 3a6f13c
update README.md with features from master upstream branch
jdoucerain 8786463
fix unreachable code
jdoucerain fc5387c
exclusion between local passwords and LDAP authentication
jdoucerain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| //go:build pprof | ||
| // +build pprof | ||
|
|
||
| package cos | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| //+build !windows | ||
| //go:build !windows | ||
| // +build !windows | ||
|
|
||
| package cos | ||
|
|
||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| //+build windows | ||
| //go:build windows | ||
| // +build windows | ||
|
|
||
| package cos | ||
|
|
||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package settings | ||
|
jdoucerain marked this conversation as resolved.
|
||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io/ioutil" | ||
| "log" | ||
|
|
||
| "github.com/go-ldap/ldap/v3" | ||
| ) | ||
|
|
||
| //LDAPConfig enables LDAP auth | ||
| type LDAPConfig struct { | ||
| BindDN string `json:"BindDN"` | ||
| BindPassword string `json:"BindPassword"` | ||
| Url string `json:"Url"` | ||
| BaseDN string `json:"BaseDN"` | ||
| Filter string `json:"Filter"` | ||
| IDMapTo string `json:"IDMapTo"` | ||
| CA string `json:"CA"` | ||
| Insecure bool `json:"Insecure"` | ||
| } | ||
|
|
||
| // parse the LDAP config file | ||
| func ParseConfigFile(Configfile string) (LDAPConfig, error) { | ||
| var ldapConfig LDAPConfig | ||
| file, err := ioutil.ReadFile(Configfile) | ||
| if err != nil { | ||
| return ldapConfig, fmt.Errorf("LDAP config file error") | ||
| } | ||
| err = json.Unmarshal([]byte(file), &ldapConfig) | ||
| if err != nil { | ||
| return ldapConfig, fmt.Errorf("Error occured during unmarshaling ldap config file") | ||
| } | ||
| return ldapConfig, nil | ||
| } | ||
|
|
||
| // authenticate a user using ldap credentials | ||
| func LDAPAuthUser(user *User, password []byte, ldapconfig LDAPConfig) error { | ||
| log.Printf("User %s to be authenticated in LDAP", user.Name) | ||
| l, err := connectTLS(ldapconfig) | ||
| if err != nil { | ||
| log.Printf("Error occured during TLS connection to %s", ldapconfig.Url) | ||
| return fmt.Errorf("Error occured during TLS connection to %s", ldapconfig.Url) | ||
| } | ||
| defer l.Close() | ||
| // Normal Bind and Search | ||
| result, err := bindAndSearch(l, ldapconfig, user) | ||
| if err != nil { | ||
| log.Printf("User %s not found in LDAP", user.Name) | ||
| return fmt.Errorf("User %s not found in LDAP", user.Name) | ||
| } | ||
| userdn := result.Entries[0].DN | ||
| log.Printf("DN:%s", userdn) | ||
|
|
||
| if len(result.Entries) != 1 { | ||
| log.Printf("too many entries returned for user %s", user.Name) | ||
| return fmt.Errorf("too many entries returned") | ||
| } | ||
| // Bind as the user to verify their password | ||
| err = l.Bind(userdn, string(password[:])) | ||
| if err != nil { | ||
| return fmt.Errorf("Bad password for user %s", user.Name) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // LDAP Connection with TLS | ||
| func connectTLS(ldapconfig LDAPConfig) (*ldap.Conn, error) { | ||
| var tlsConf *tls.Config | ||
|
|
||
| if ldapconfig.Insecure { | ||
| tlsConf = &tls.Config{InsecureSkipVerify: true} | ||
| } | ||
|
|
||
| if ldapconfig.CA != "" { | ||
| log.Printf("CA file %s", ldapconfig.CA) | ||
| certpool := x509.NewCertPool() | ||
| CAfile, err := ioutil.ReadFile(ldapconfig.CA) | ||
| if err != nil { | ||
| log.Printf("LDAP CA file error") | ||
| return nil, fmt.Errorf("LDAP CA file error") | ||
| } | ||
| certpool.AppendCertsFromPEM([]byte(CAfile)) | ||
| tlsConf = &tls.Config{RootCAs: certpool} | ||
| log.Printf("CA file %s loaded", ldapconfig.CA) | ||
| } | ||
|
|
||
| l, err := ldap.DialTLS("tcp", ldapconfig.Url, tlsConf) | ||
| if err != nil { | ||
| log.Printf("TLS error: %s", err) | ||
| return nil, err | ||
| } | ||
|
|
||
| return l, nil | ||
| } | ||
|
|
||
| // Normal Bind and Search | ||
| func bindAndSearch(l *ldap.Conn, ldapconfig LDAPConfig, user *User) (*ldap.SearchResult, error) { | ||
| var filter string | ||
| l.Bind(ldapconfig.BindDN, ldapconfig.BindPassword) | ||
| if ldapconfig.Filter != "" { | ||
| filter = fmt.Sprintf("(&(%s)(%s=%s))", ldapconfig.Filter, ldapconfig.IDMapTo, user.Name) | ||
| } else { | ||
| filter = fmt.Sprintf("(%s=%s)", ldapconfig.IDMapTo, user.Name) | ||
| } | ||
| log.Printf("filter %s", filter) | ||
| searchReq := ldap.NewSearchRequest( | ||
| ldapconfig.BaseDN, | ||
| ldap.ScopeWholeSubtree, | ||
| ldap.NeverDerefAliases, | ||
| 0, | ||
| 0, | ||
| false, | ||
| filter, | ||
| []string{"dn"}, | ||
| nil, | ||
| ) | ||
| result, err := l.Search(searchReq) | ||
| if err != nil { | ||
| log.Printf("Search Error: %s", err) | ||
| return nil, fmt.Errorf("Search Error: %s", err) | ||
| } | ||
|
|
||
| if len(result.Entries) > 0 { | ||
| return result, nil | ||
| } else { | ||
| return nil, fmt.Errorf("Couldn't fetch search entries") | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.