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

Support for full DN of the user in the member field when used with groupOfNames object class #5

Merged
merged 10 commits into from
Sep 30, 2016
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
*.iml
.idea/*
*.swp
target/*
+*.iml
+.idea/*
+*.swp
+target/*
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ private boolean filterByGroup(InitialDirContext context, String sanitizedUsernam
}

private Set<String> getGroupMembershipsIntersectingWithRestrictedGroups(InitialDirContext context, String userName) throws NamingException {

userName = userNameBaseOnGroupClass(userName);

final String filter = String.format("(&(%s=%s)(objectClass=%s))", configuration.getGroupMembershipAttribute(), userName, configuration.getGroupClassName());
final NamingEnumeration<SearchResult> result = context.search(configuration.getGroupFilter(), filter, new SearchControls());

Expand All @@ -84,6 +87,14 @@ private Set<String> getGroupMembershipsIntersectingWithRestrictedGroups(InitialD
}
}

private String userNameBaseOnGroupClass(String userName) {
if ("groupOfNames".equalsIgnoreCase(configuration.getGroupClassName())
&& "member".equalsIgnoreCase(configuration.getGroupMembershipAttribute())) {
Copy link
Contributor

Choose a reason for hiding this comment

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

"groupOfNames" and "member" seem pretty specific to your LDAP setup use case. Other setups can use other fields for their member identity :(

return toUserDN(userName);
}
return userName;
}

@Timed
public boolean authenticate(BasicCredentials credentials) throws io.dropwizard.auth.AuthenticationException {
final String sanitizedUsername = sanitizeEntity(credentials.getUsername());
Expand All @@ -101,7 +112,7 @@ public boolean authenticate(BasicCredentials credentials) throws io.dropwizard.a
}

private AutoclosingDirContext buildContext(String sanitizedUsername, String password) throws NamingException {
final String userDN = String.format("%s=%s,%s", configuration.getUserNameAttribute(), sanitizedUsername, configuration.getUserFilter());
final String userDN = toUserDN(sanitizedUsername);

final Hashtable<String, String> env = contextConfiguration();

Expand All @@ -111,6 +122,10 @@ private AutoclosingDirContext buildContext(String sanitizedUsername, String pass
return new AutoclosingDirContext(env);
}

private String toUserDN(String username) {
return String.format("%s=%s,%s", configuration.getUserNameAttribute(), username, configuration.getUserFilter());
}

@Timed
public Optional<User> authenticateAndReturnPermittedGroups(BasicCredentials credentials) throws io.dropwizard.auth.AuthenticationException {
final String sanitizedUsername = sanitizeEntity(credentials.getUsername());
Expand Down Expand Up @@ -141,4 +156,4 @@ private Hashtable<String, String> contextConfiguration() {

return env;
}
}
}