Skip to content

Commit 4c25593

Browse files
committed
Preserve LDAP DNs during Active Directory authorization
1 parent 3b046b7 commit 4c25593

2 files changed

Lines changed: 39 additions & 5 deletions

File tree

core/src/main/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealm.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ protected Set<String> getRoleNamesForUser(String username, LdapContext ldapConte
169169
SearchControls searchControls = new SearchControls();
170170
searchControls.setSearchScope(SearchControls.SUBTREE_SCOPE);
171171

172-
String userPrincipalName = getUsernameWithSuffix(username);
172+
String userPrincipalName = getUsernameForAuthentication(username);
173173

174174
Object[] searchArguments = new Object[] {userPrincipalName};
175175

@@ -238,8 +238,7 @@ protected Collection<String> getRoleNamesForGroups(Collection<String> groupNames
238238
protected String getUsernameWithSuffix(String username) {
239239
String sanitizedUsername = Rdn.escapeValue(username);
240240
if (principalSuffix != null
241-
&& !sanitizedUsername.toLowerCase(Locale.ROOT)
242-
.endsWith(principalSuffix.toLowerCase(Locale.ROOT))) {
241+
&& !sanitizedUsername.toLowerCase(Locale.ROOT).endsWith(principalSuffix.toLowerCase(Locale.ROOT))) {
243242
return sanitizedUsername + principalSuffix;
244243
}
245244
return sanitizedUsername;

core/src/test/java/org/apache/shiro/realm/activedirectory/ActiveDirectoryRealmTest.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,9 @@ void testInitialization() {
154154

155155
@Test
156156
void testUsernameForAuthenticationWithDn() {
157-
ActiveDirectoryRealm activeDirectoryRealm = new ActiveDirectoryRealm();
157+
ActiveDirectoryRealm activeDirectoryRealm = new ActiveDirectoryRealm() {{
158+
this.principalSuffix = "@example.com";
159+
}};
158160

159161
String dn = "CN=my_name,OU=Development,OU=Special Accounts,DC=mycompany,DC=com";
160162

@@ -172,7 +174,9 @@ void testUsernameForAuthenticationWithUsername() {
172174

173175
@Test
174176
void testAuthenticationUsesDnWithoutEscaping() throws Exception {
175-
ActiveDirectoryRealm activeDirectoryRealm = new ActiveDirectoryRealm();
177+
ActiveDirectoryRealm activeDirectoryRealm = new ActiveDirectoryRealm() {{
178+
this.principalSuffix = "@example.com";
179+
}};
176180
LdapContextFactory factory = createMock(LdapContextFactory.class);
177181
LdapContext ldapContext = createNiceMock(LdapContext.class);
178182

@@ -188,6 +192,37 @@ void testAuthenticationUsesDnWithoutEscaping() throws Exception {
188192
verify(factory);
189193
}
190194

195+
@Test
196+
void testAuthorizationUsesDnWithoutEscaping() throws Exception {
197+
ActiveDirectoryRealm activeDirectoryRealm = new ActiveDirectoryRealm() {{
198+
this.principalSuffix = "@example.com";
199+
}};
200+
201+
LdapContext ldapContext = createNiceMock(LdapContext.class);
202+
NamingEnumeration<SearchResult> results = createNiceMock(NamingEnumeration.class);
203+
204+
String dn = "CN=my_name,OU=Development,OU=Special Accounts,DC=mycompany,DC=com";
205+
206+
Capture<Object[]> captureArgs = Capture.newInstance(CaptureType.ALL);
207+
208+
expect(ldapContext.search(
209+
anyString(),
210+
anyString(),
211+
capture(captureArgs),
212+
anyObject(SearchControls.class)))
213+
.andReturn(results);
214+
215+
replay(ldapContext);
216+
217+
activeDirectoryRealm.getRoleNamesForUser(dn, ldapContext);
218+
219+
Object[] searchArguments = captureArgs.getValue();
220+
221+
assertThat(searchArguments[0]).isEqualTo(dn);
222+
223+
verify(ldapContext);
224+
}
225+
191226
public void assertExistingUserSuffix(String username, String expectedPrincipalName) throws Exception {
192227

193228
LdapContext ldapContext = createMock(LdapContext.class);

0 commit comments

Comments
 (0)