-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Adding basic LDAP package #57
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package ldap | ||
|
||
import ( | ||
"fmt" | ||
. "github.com/ory-am/hydra/oauth/provider" | ||
"gopkg.in/ldap.v2" | ||
"crypto/tls" | ||
) | ||
|
||
type ldapconf struct { | ||
id string | ||
host string | ||
port string | ||
tls bool | ||
base_dn string | ||
bind_dn string | ||
uid string | ||
password string | ||
filter string | ||
} | ||
|
||
func New(id, client, secret, redirectURL string) *ldapconf { | ||
return &ldapconf{ | ||
id: id, | ||
host: "ldap.forumsys.com", | ||
tls: false, | ||
port: "389", | ||
bind_dn: "cn=read-only-admin,dc=example,dc=com", | ||
base_dn: "dc=example,dc=com", | ||
uid: "uid", | ||
password: "password", | ||
filter: "(objectClass=organizationalPerson)", | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, the whole thing should be configurable. Maybe don't define |
||
} | ||
|
||
func (l *ldapconf) GetAuthenticationURL(state string) string { | ||
return "" | ||
} | ||
|
||
func (l *ldapconf) FetchSession(code string) (Session, error) { | ||
var connection *ldap.Conn | ||
var err error | ||
|
||
if !l.tls { | ||
connection, err = ldap.Dial("tcp", fmt.Sprintf("%s:%s", l.host, l.port)) | ||
} else { | ||
connection, err = ldap.DialTLS("tcp", fmt.Sprintf("%s:%s", l.host, l.port), &tls.Config{InsecureSkipVerify: true}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
|
||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
defer connection.Close() | ||
|
||
if err = connection.Bind(l.bind_dn, l.password); err != nil { | ||
return nil, err | ||
} | ||
|
||
searchRequest := ldap.NewSearchRequest( | ||
l.base_dn, | ||
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false, | ||
fmt.Sprintf("(&%s(%s=%s))", l.filter, l.uid, code), | ||
[]string{l.uid}, | ||
nil, | ||
) | ||
|
||
sr, err := connection.Search(searchRequest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(sr.Entries) != 1 { | ||
return nil, err | ||
} | ||
|
||
acc := make(map[string]interface{}, 10) | ||
for _, attr := range sr.Entries[0].Attributes { | ||
acc[attr.Name] = attr.Values | ||
} | ||
|
||
return &DefaultSession{ | ||
RemoteSubject: fmt.Sprintf("%s", sr.Entries[0].DN), | ||
Extra: acc, | ||
}, nil | ||
} | ||
|
||
func (l *ldapconf) GetID() string { | ||
return l.id | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package ldap | ||
|
||
import ( | ||
"testing" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
var mock = ldapconf{ | ||
id: "123", | ||
host: "ldap://ldap.forumsys.com", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would appreciate it if you would use dockertest to spin up an LDAP server and test against that one. Using a public LDAP is not reliable because the server could shut down or configuration could change. |
||
port: "389", | ||
bind_dn: "cn=read-only-admin,dc=example,dc=com", | ||
base_dn: "dc=example,dc=com", | ||
uid: "uid", | ||
password: "password", | ||
filter: "(objectClass=organizationalPerson)", | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
m := New("321", "client", "secret", "/callback") | ||
assert.Equal(t, "321", m.id) | ||
|
||
} | ||
|
||
func TestGetID(t *testing.T) { | ||
assert.Equal(t, "123", mock.GetID()) | ||
} | ||
|
||
func TestFetchSession(t *testing.T) { | ||
m := New("321", "client", "secret", "/callback") | ||
ses, err := m.FetchSession("riemann") | ||
|
||
assert.NoError(t, err) | ||
assert.Equal(t, "uid=riemann,dc=example,dc=com", ses.GetRemoteSubject()) | ||
//fmt.Printf(ses.GetExtra()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be hardcoded, so other people can use different LDAP servers.