Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions oauth/provider/ldap/ldap.go
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",
Copy link
Member

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.

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)",
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the whole thing should be configurable. Maybe don't define New at all and simply make ldapconf public and rename it to e.g. LDAPProvider.

}

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})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&tls.Config{InsecureSkipVerify: true} is a security risk and should default to false.

}

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
}
36 changes: 36 additions & 0 deletions oauth/provider/ldap/ldap_test.go
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",
Copy link
Member

Choose a reason for hiding this comment

The 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())
}