//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: protected String bindDNAuthentication(javax.naming.ldap.InitialLdapContext paramInitialLdapContext, String paramString1, String paramString2, String paramString3, String paramString4) throws javax.naming.NamingException protected internal virtual string bindDNAuthentication(InitialLdapContext paramInitialLdapContext, string paramString1, string paramString2, string paramString3, string paramString4) { SearchControls searchControls = new SearchControls(); searchControls.SearchScope = 2; searchControls.ReturningAttributes = new string[0]; searchControls.TimeLimit = 5000; NamingEnumeration namingEnumeration = null; object[] arrayOfObject = new object[] { paramString1 }; namingEnumeration = paramInitialLdapContext.search(paramString3, paramString4, arrayOfObject, searchControls); if (!namingEnumeration.hasMore()) { namingEnumeration.close(); throw new NamingException("Search of baseDN(" + paramString3 + ") found no matches"); } SearchResult searchResult = (SearchResult)namingEnumeration.next(); string str1 = searchResult.Name; string str2 = null; if (searchResult.Relative == true) { str2 = str1 + "," + paramString3; } else { throw new NamingException("Can't follow referal for authentication: " + str1); } namingEnumeration.close(); namingEnumeration = null; InitialLdapContext initialLdapContext = constructInitialLdapContext(str2, paramString2); initialLdapContext.close(); return(str2); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: private org.apache.shiro.authc.AuthenticationInfo queryForAuthenticationInfoSAM(org.apache.shiro.authc.AuthenticationToken token, org.apache.shiro.realm.ldap.LdapContextFactory ldapContextFactory) throws javax.naming.NamingException private AuthenticationInfo QueryForAuthenticationInfoSAM(AuthenticationToken token, LdapContextFactory ldapContextFactory) { object principal = token.Principal; object credentials = token.Credentials; LdapContext ctx = null; try { ctx = _useStartTls ? GetSystemLdapContextUsingStartTls(ldapContextFactory) : ldapContextFactory.SystemLdapContext; string[] attrs = new string[] { "cn" }; SearchControls searchCtls = new SearchControls(SearchControls.SUBTREE_SCOPE, 1, 0, attrs, false, false); object[] searchArguments = new object[] { principal }; string filter = "sAMAccountName={0}"; NamingEnumeration <SearchResult> search = ctx.search(_userSearchBase, filter, searchArguments, searchCtls); if (search.hasMore()) { //JAVA TO C# CONVERTER WARNING: The original Java variable was marked 'final': //ORIGINAL LINE: final javax.naming.directory.SearchResult next = search.next(); SearchResult next = search.next(); string loginUser = next.NameInNamespace; if (search.hasMore()) { _securityLog.error("More than one user matching: " + principal); throw new AuthenticationException("More than one user matching: " + principal); } else { LdapContext ctx2 = ldapContextFactory.getLdapContext(loginUser, credentials); LdapUtils.closeContext(ctx2); } } else { throw new AuthenticationException("No user matching: " + principal); } return(CreateAuthenticationInfo(token, principal, credentials, ctx)); } finally { LdapUtils.closeContext(ctx); } }
//JAVA TO C# CONVERTER TODO TASK: Most Java annotations will not have direct .NET equivalent attributes: //ORIGINAL LINE: @Test public void shouldAllowMultipleGroupMembershipAttributes() throws javax.naming.NamingException //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: public virtual void ShouldAllowMultipleGroupMembershipAttributes() { when(Config.get(SecuritySettings.ldap_authorization_user_search_filter)).thenReturn("{0}"); when(Config.get(SecuritySettings.ldap_authorization_group_membership_attribute_names)).thenReturn(asList("attr0", "attr1", "attr2")); when(Config.get(SecuritySettings.ldap_authorization_group_to_role_mapping)).thenReturn("group1=role1;group2=role2,role3"); LdapContext ldapContext = mock(typeof(LdapContext)); NamingEnumeration result = mock(typeof(NamingEnumeration)); SearchResult searchResult = mock(typeof(SearchResult)); Attributes attributes = mock(typeof(Attributes)); Attribute attribute1 = mock(typeof(Attribute)); Attribute attribute2 = mock(typeof(Attribute)); Attribute attribute3 = mock(typeof(Attribute)); NamingEnumeration attributeEnumeration = mock(typeof(NamingEnumeration)); NamingEnumeration groupEnumeration1 = mock(typeof(NamingEnumeration)); NamingEnumeration groupEnumeration2 = mock(typeof(NamingEnumeration)); NamingEnumeration groupEnumeration3 = mock(typeof(NamingEnumeration)); // Mock ldap search result "attr1" contains "group1" and "attr2" contains "group2" (a bit brittle...) // "attr0" is non-existing and should have no effect when(ldapContext.search(anyString(), anyString(), any(), any())).thenReturn(result); when(result.hasMoreElements()).thenReturn(true, false); when(result.next()).thenReturn(searchResult); when(searchResult.Attributes).thenReturn(attributes); when(attributes.All).thenReturn(attributeEnumeration); when(attributeEnumeration.hasMore()).thenReturn(true, true, false); when(attributeEnumeration.next()).thenReturn(attribute1, attribute2, attribute3); when(attribute1.ID).thenReturn("attr1"); // This attribute should yield role1 when(attribute1.All).thenReturn(groupEnumeration1); when(groupEnumeration1.hasMore()).thenReturn(true, false); when(groupEnumeration1.next()).thenReturn("group1"); when(attribute2.ID).thenReturn("attr2"); // This attribute should yield role2 and role3 when(attribute2.All).thenReturn(groupEnumeration2); when(groupEnumeration2.hasMore()).thenReturn(true, false); when(groupEnumeration2.next()).thenReturn("group2"); when(attribute3.ID).thenReturn("attr3"); // This attribute should have no effect when(attribute3.All).thenReturn(groupEnumeration3); when(groupEnumeration3.hasMore()).thenReturn(true, false); when(groupEnumeration3.next()).thenReturn("groupWithNoRole"); // When LdapRealm realm = new LdapRealm(Config, _securityLog, _secureHasher); ISet <string> roles = realm.FindRoleNamesForUser("username", ldapContext); // Then assertThat(roles, hasItems("role1", "role2", "role3")); }
//JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: private java.util.Set<String> authorize(javax.naming.ldap.LdapContext ctx, String username) throws javax.naming.NamingException private ISet <string> Authorize(LdapContext ctx, string username) { ISet <string> roleNames = new LinkedHashSet <string>(); // Setup our search controls SearchControls searchCtls = new SearchControls(); searchCtls.SearchScope = SearchControls.SUBTREE_SCOPE; searchCtls.ReturningAttributes = new string[] { GROUP_ID }; // Use a search argument to prevent potential code injection object[] searchArguments = new object[] { username }; // Search for groups that has the user as a member NamingEnumeration result = ctx.search(GROUP_SEARCH_BASE, GROUP_SEARCH_FILTER, searchArguments, searchCtls); if (result.hasMoreElements()) { SearchResult searchResult = ( SearchResult )result.next(); Attributes attributes = searchResult.Attributes; if (attributes != null) { NamingEnumeration attributeEnumeration = attributes.All; while (attributeEnumeration.hasMore()) { Attribute attribute = ( Attribute )attributeEnumeration.next(); string attributeId = attribute.ID; if (attributeId.Equals(GROUP_ID, StringComparison.OrdinalIgnoreCase)) { // We found a group that the user is a member of. See if it has a role mapped to it string groupId = ( string )attribute.get(); string neo4jGroup = GetNeo4jRoleForGroupId(groupId); if (!string.ReferenceEquals(neo4jGroup, null)) { // Yay! Add it to our set of roles roleNames.Add(neo4jGroup); } } } } } return(roleNames); }
// TODO: Extract to an LdapAuthorizationStrategy ? This ("group by attribute") is one of multiple possible strategies //JAVA TO C# CONVERTER WARNING: Method 'throws' clauses are not available in C#: //ORIGINAL LINE: java.util.Set<String> findRoleNamesForUser(String username, javax.naming.ldap.LdapContext ldapContext) throws javax.naming.NamingException internal virtual ISet <string> FindRoleNamesForUser(string username, LdapContext ldapContext) { ISet <string> roleNames = new LinkedHashSet <string>(); SearchControls searchCtls = new SearchControls(); searchCtls.SearchScope = SearchControls.SUBTREE_SCOPE; searchCtls.ReturningAttributes = _membershipAttributeNames.ToArray(); // Use search argument to prevent potential code injection object[] searchArguments = new object[] { username }; NamingEnumeration result = ldapContext.search(_userSearchBase, _userSearchFilter, searchArguments, searchCtls); if (result.hasMoreElements()) { SearchResult searchResult = ( SearchResult )result.next(); if (result.hasMoreElements()) { _securityLog.warn(_securityLog.DebugEnabled ? WithRealm("LDAP user search for user principal '%s' is ambiguous. The first match that will " + "be checked for group membership is '%s' but the search also matches '%s'. " + "Please check your LDAP realm configuration.", username, searchResult.ToString(), result.next().ToString()) : WithRealm("LDAP user search for user principal '%s' is ambiguous. The search matches more " + "than one entry. Please check your LDAP realm configuration.", username)); } Attributes attributes = searchResult.Attributes; if (attributes != null) { NamingEnumeration attributeEnumeration = attributes.All; while (attributeEnumeration.hasMore()) { Attribute attribute = ( Attribute )attributeEnumeration.next(); string attributeId = attribute.ID; if (_membershipAttributeNames.Any(attributeId.equalsIgnoreCase)) { ICollection <string> groupNames = LdapUtils.getAllAttributeValues(attribute); ICollection <string> rolesForGroups = GetRoleNamesForGroups(groupNames); roleNames.addAll(rolesForGroups); } } } } return(roleNames); }