Skip to content

Commit

Permalink
Separate config for DB and LDAP to enable 3-backend setup. Fix Ldap u…
Browse files Browse the repository at this point in the history
…ser login filter format
  • Loading branch information
Rob Archibald committed Jan 15, 2017
1 parent 6f945a7 commit 27a190f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
12 changes: 7 additions & 5 deletions backendLDAPLogin.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package main

import (
"fmt"
"github.com/robarchibald/onedb"
"gopkg.in/ldap.v2"
"strconv"
)

type backendLDAPLogin struct {
db onedb.DBer
baseDn string
db onedb.DBer
baseDn string
userLoginFilter string
}

func NewBackendLDAPLogin(server string, port int, bindDn, password, baseDn string) (LoginBackender, error) {
func NewBackendLDAPLogin(server string, port int, bindDn, password, baseDn, userLoginFilter string) (LoginBackender, error) {
db, err := onedb.NewLdap(server, port, bindDn, password)
if err != nil {
return nil, err
}
return &backendLDAPLogin{db, baseDn}, nil
return &backendLDAPLogin{db, baseDn, userLoginFilter}, nil
}

type ldapData struct {
Expand All @@ -28,7 +30,7 @@ type ldapData struct {
}

func (l *backendLDAPLogin) GetLogin(email, loginProvider string) (*UserLogin, error) {
req := ldap.NewSearchRequest(l.baseDn, ldap.ScopeSingleLevel, ldap.NeverDerefAliases, 0, 0, false, "uid="+email, []string{"uid", "userPassword", "uidNumber", "gidNumber", "homeDirectory"}, nil)
req := ldap.NewSearchRequest(l.baseDn, ldap.ScopeSingleLevel, ldap.NeverDerefAliases, 0, 0, false, fmt.Sprintf(l.userLoginFilter, email), []string{"uid", "userPassword", "uidNumber", "gidNumber", "homeDirectory"}, nil)
data := &ldapData{}
err := l.db.QueryStructRow(req, data)
if err != nil {
Expand Down
24 changes: 24 additions & 0 deletions backendLDAPLogin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"github.com/robarchibald/configReader"
"testing"
)

func TestNewBackendLDAPLogin(t *testing.T) {
config := &authConf{}
err := configReader.ReadFile("nginxauth.conf", config)
if err != nil {
t.Fatal("unable to load config file", err)
}

l, err := NewBackendLDAPLogin(config.LdapServer, config.LdapPort, config.LdapBindDn, config.LdapPassword, config.LdapBaseDn, config.LdapUserFilter)
if err != nil {
t.Fatal("unable to login", err)
}

_, err = l.GetLogin("[email protected]", "")
if err == nil {
t.Fatal("Expected no results", err)
}
}
20 changes: 12 additions & 8 deletions nginxauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ import (
type authConf struct {
AuthServerListenPort int
StoragePrefix string
BackendType string
BackendServer string
BackendPort int
BackendUser string
BackendDatabase string
BackendPassword string
DbType string
DbServer string
DbPort int
DbUser string
DbDatabase string
DbPassword string
LdapServer string
LdapPort int
LdapBindDn string
LdapPassword string
LdapBaseDn string
LdapUserFilter string
GetUserLoginQuery string
Expand Down Expand Up @@ -88,11 +92,11 @@ func newNginxAuth() (*nginxauth, error) {
}

s := NewBackendRedisSession(config.RedisServer, config.RedisPort, config.RedisPassword, config.RedisMaxIdle, config.RedisMaxConnections, config.StoragePrefix)
l, err := NewBackendLDAPLogin(config.BackendServer, config.BackendPort, config.BackendUser, config.BackendPassword, config.LdapBaseDn)
l, err := NewBackendLDAPLogin(config.LdapServer, config.LdapPort, config.LdapBindDn, config.LdapPassword, config.LdapBaseDn, config.LdapUserFilter)
if err != nil {
return nil, err
}
u, err := newBackendDbUser(config.BackendServer, config.BackendPort, config.BackendUser, config.BackendPassword, config.BackendDatabase, config.GetUserLoginQuery, config.AddUserQuery, config.VerifyEmailQuery, config.UpdateUserQuery)
u, err := newBackendDbUser(config.DbServer, config.DbPort, config.DbUser, config.DbPassword, config.DbDatabase, config.GetUserLoginQuery, config.AddUserQuery, config.VerifyEmailQuery, config.UpdateUserQuery)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions nginxauth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

func TestNewRestServer(t *testing.T) {
_, err := newNginxAuth()
if err != nil { // will connect to the docker Postgres db specified in auth.conf
t.Error("expected success")
if err != nil { // will connect to the docker Postgres db and LDAP server specified in auth.conf
t.Error("expected success", err)
}
}

Expand Down

0 comments on commit 27a190f

Please sign in to comment.