Skip to content

Commit 7c3b7f6

Browse files
authored
Merge pull request dexidp#1380 from dkess/emailsuffix
LDAP connector - add emailSuffix config option
2 parents 5138a7e + cf9a765 commit 7c3b7f6

2 files changed

Lines changed: 74 additions & 5 deletions

File tree

connector/ldap/ldap.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ type Config struct {
107107
IDAttr string `json:"idAttr"` // Defaults to "uid"
108108
EmailAttr string `json:"emailAttr"` // Defaults to "mail"
109109
NameAttr string `json:"nameAttr"` // No default.
110+
111+
// If this is set, the email claim of the id token will be constructed from the idAttr and
112+
// value of emailSuffix. This should not include the @ character.
113+
EmailSuffix string `json:"emailSuffix"` // No default.
110114
} `json:"userSearch"`
111115

112116
// Group search configuration.
@@ -331,18 +335,21 @@ func (c *ldapConnector) identityFromEntry(user ldap.Entry) (ident connector.Iden
331335
if ident.UserID = getAttr(user, c.UserSearch.IDAttr); ident.UserID == "" {
332336
missing = append(missing, c.UserSearch.IDAttr)
333337
}
334-
if ident.Email = getAttr(user, c.UserSearch.EmailAttr); ident.Email == "" {
335-
missing = append(missing, c.UserSearch.EmailAttr)
336-
}
337-
// TODO(ericchiang): Let this value be set from an attribute.
338-
ident.EmailVerified = true
339338

340339
if c.UserSearch.NameAttr != "" {
341340
if ident.Username = getAttr(user, c.UserSearch.NameAttr); ident.Username == "" {
342341
missing = append(missing, c.UserSearch.NameAttr)
343342
}
344343
}
345344

345+
if c.UserSearch.EmailSuffix != "" {
346+
ident.Email = ident.Username + "@" + c.UserSearch.EmailSuffix
347+
} else if ident.Email = getAttr(user, c.UserSearch.EmailAttr); ident.Email == "" {
348+
missing = append(missing, c.UserSearch.EmailAttr)
349+
}
350+
// TODO(ericchiang): Let this value be set from an attribute.
351+
ident.EmailVerified = true
352+
346353
if len(missing) != 0 {
347354
err := fmt.Errorf("ldap: entry %q missing following required attribute(s): %q", user.DN, missing)
348355
return connector.Identity{}, err

connector/ldap/ldap_test.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,68 @@ userpassword: bar
123123
runTests(t, schema, connectLDAP, c, tests)
124124
}
125125

126+
func TestQueryWithEmailSuffix(t *testing.T) {
127+
schema := `
128+
dn: dc=example,dc=org
129+
objectClass: dcObject
130+
objectClass: organization
131+
o: Example Company
132+
dc: example
133+
134+
dn: ou=People,dc=example,dc=org
135+
objectClass: organizationalUnit
136+
ou: People
137+
138+
dn: cn=jane,ou=People,dc=example,dc=org
139+
objectClass: person
140+
objectClass: inetOrgPerson
141+
sn: doe
142+
cn: jane
143+
mail: janedoe@example.com
144+
userpassword: foo
145+
146+
dn: cn=john,ou=People,dc=example,dc=org
147+
objectClass: person
148+
objectClass: inetOrgPerson
149+
sn: doe
150+
cn: john
151+
userpassword: bar
152+
`
153+
c := &Config{}
154+
c.UserSearch.BaseDN = "ou=People,dc=example,dc=org"
155+
c.UserSearch.NameAttr = "cn"
156+
c.UserSearch.EmailSuffix = "test.example.com"
157+
c.UserSearch.IDAttr = "DN"
158+
c.UserSearch.Username = "cn"
159+
160+
tests := []subtest{
161+
{
162+
name: "ignoremailattr",
163+
username: "jane",
164+
password: "foo",
165+
want: connector.Identity{
166+
UserID: "cn=jane,ou=People,dc=example,dc=org",
167+
Username: "jane",
168+
Email: "jane@test.example.com",
169+
EmailVerified: true,
170+
},
171+
},
172+
{
173+
name: "nomailattr",
174+
username: "john",
175+
password: "bar",
176+
want: connector.Identity{
177+
UserID: "cn=john,ou=People,dc=example,dc=org",
178+
Username: "john",
179+
Email: "john@test.example.com",
180+
EmailVerified: true,
181+
},
182+
},
183+
}
184+
185+
runTests(t, schema, connectLDAP, c, tests)
186+
}
187+
126188
func TestGroupQuery(t *testing.T) {
127189
schema := `
128190
dn: dc=example,dc=org

0 commit comments

Comments
 (0)