Search in sources :

Example 1 with DummyHttpServletRequestLdap

use of opengrok.auth.plugin.util.DummyHttpServletRequestLdap in project OpenGrok by OpenGrok.

the class LdapUserPluginTest method testFillSessionWithDnOff.

@Test
void testFillSessionWithDnOff() throws LdapException {
    AbstractLdapProvider mockprovider = mock(LdapFacade.class);
    Map<String, Set<String>> attrs = new HashMap<>();
    attrs.put("mail", Collections.singleton("foo@example.com"));
    final String dn = "cn=FOO_BAR,L=EMEA,DC=EXAMPLE,DC=COM";
    AbstractLdapProvider.LdapSearchResult<Map<String, Set<String>>> result = new AbstractLdapProvider.LdapSearchResult<>(dn, attrs);
    assertNotNull(result);
    when(mockprovider.lookupLdapContent(isNull(), isNull(), any(String[].class))).thenReturn(result);
    Map<String, Object> params = getParamsMap();
    params.put(LdapUserPlugin.ATTRIBUTES, "mail");
    params.put(LdapUserPlugin.USE_DN, false);
    LdapUserPlugin plugin = new LdapUserPlugin();
    plugin.load(params, mockprovider);
    assertSame(mockprovider, plugin.getLdapProvider());
    HttpServletRequest request = new DummyHttpServletRequestLdap();
    User user = new User("foo@example.com", "id");
    plugin.fillSession(request, user);
    assertNotNull(request.getSession().getAttribute(SESSION_ATTR));
    assertEquals(dn, ((LdapUser) request.getSession().getAttribute(SESSION_ATTR)).getDn());
}
Also used : Set(java.util.Set) LdapUser(opengrok.auth.entity.LdapUser) User(opengrok.auth.plugin.entity.User) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) DummyHttpServletRequestLdap(opengrok.auth.plugin.util.DummyHttpServletRequestLdap) AbstractLdapProvider(opengrok.auth.plugin.ldap.AbstractLdapProvider) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Test(org.junit.jupiter.api.Test)

Example 2 with DummyHttpServletRequestLdap

use of opengrok.auth.plugin.util.DummyHttpServletRequestLdap in project OpenGrok by OpenGrok.

the class LdapUserPluginTest method testInstance.

@Test
void testInstance() {
    Map<String, Object> params = getParamsMap();
    params.put(LdapUserPlugin.ATTRIBUTES, "mail");
    params.put(LdapUserPlugin.INSTANCE, "42");
    plugin.load(params);
    HttpServletRequest request = new DummyHttpServletRequestLdap();
    LdapUser ldapUser = new LdapUser();
    plugin.updateSession(request, ldapUser);
    assertEquals(request.getSession().getAttribute(SESSION_ATTR + "42"), ldapUser);
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) DummyHttpServletRequestLdap(opengrok.auth.plugin.util.DummyHttpServletRequestLdap) LdapUser(opengrok.auth.entity.LdapUser) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 3 with DummyHttpServletRequestLdap

use of opengrok.auth.plugin.util.DummyHttpServletRequestLdap in project OpenGrok by OpenGrok.

the class LdapAttrPluginTest method prepareRequest.

private void prepareRequest(String username, String mail, String... ous) {
    dummyRequest = new DummyHttpServletRequestLdap();
    dummyRequest.setAttribute(UserPlugin.REQUEST_ATTR, new User(username, "123"));
    LdapUser ldapUser = new LdapUser();
    ldapUser.setAttribute("mail", new TreeSet<>(Collections.singletonList(mail)));
    ldapUser.setAttribute("uid", new TreeSet<>(Collections.singletonList("123")));
    ldapUser.setAttribute("ou", new TreeSet<>(Arrays.asList(ous)));
    dummyRequest.getSession().setAttribute(LdapUserPlugin.SESSION_ATTR, ldapUser);
    plugin.setSessionEstablished(dummyRequest, true);
    plugin.setSessionUsername(dummyRequest, username);
}
Also used : DummyHttpServletRequestLdap(opengrok.auth.plugin.util.DummyHttpServletRequestLdap) LdapUser(opengrok.auth.entity.LdapUser) User(opengrok.auth.plugin.entity.User) LdapUser(opengrok.auth.entity.LdapUser)

Example 4 with DummyHttpServletRequestLdap

use of opengrok.auth.plugin.util.DummyHttpServletRequestLdap in project OpenGrok by OpenGrok.

the class LdapAttrPluginTest method testAttrLookup.

/**
 * Test the interaction between {@code LdapUserPlugin} and {@code LdapAttrPlugin}. Namely:
 * <ul>
 *     <li>use of DN from the <code>LdapUser</code> object cached in the session by <code>LdapUserPlugin</code></li>
 *     <li>configuration of the cached session attribute name</li>
 * </ul>
 */
@Test
void testAttrLookup() throws LdapException {
    String attr_to_get = "mail";
    String instance_num = "42";
    String mail_attr_value = "james@bond.com";
    // Create mock LDAP provider, simulating the work of LDAP server for LdapAttrPlugin#fillSession().
    AbstractLdapProvider mockProvider = mock(LdapFacade.class);
    Map<String, Set<String>> attrs = new HashMap<>();
    attrs.put(attr_to_get, Collections.singleton(mail_attr_value));
    final String dn = "cn=FOO_BAR,L=EMEA,DC=FOO,DC=COM";
    AbstractLdapProvider.LdapSearchResult<Map<String, Set<String>>> result = new AbstractLdapProvider.LdapSearchResult<>(dn, attrs);
    assertNotNull(result);
    when(mockProvider.lookupLdapContent(anyString(), any(String[].class))).thenReturn(result);
    // Load the LdapAttrPlugin using the mock LDAP provider.
    LdapAttrPlugin plugin = new LdapAttrPlugin();
    Map<String, Object> parameters = new TreeMap<>();
    parameters.put(LdapAttrPlugin.FILE_PARAM, whitelistFile.getAbsolutePath());
    parameters.put(LdapAttrPlugin.ATTR_PARAM, attr_to_get);
    parameters.put(LdapAttrPlugin.INSTANCE_PARAM, instance_num);
    plugin.load(parameters, mockProvider);
    LdapUser ldapUser = new LdapUser(dn, null);
    HttpServletRequest request = new DummyHttpServletRequestLdap();
    request.getSession().setAttribute(LdapUserPlugin.SESSION_ATTR + instance_num, ldapUser);
    // Here it comes all together.
    User user = new User("jbond", "007");
    plugin.fillSession(request, user);
    // See if LdapAttrPlugin set its own session attribute based on the mocked query.
    assertTrue((Boolean) request.getSession().getAttribute(plugin.getSessionAllowedAttrName()));
    assertTrue(ldapUser.getAttribute(attr_to_get).contains(mail_attr_value));
}
Also used : TreeSet(java.util.TreeSet) Set(java.util.Set) LdapUser(opengrok.auth.entity.LdapUser) User(opengrok.auth.plugin.entity.User) HashMap(java.util.HashMap) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TreeMap(java.util.TreeMap) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) DummyHttpServletRequestLdap(opengrok.auth.plugin.util.DummyHttpServletRequestLdap) LdapUser(opengrok.auth.entity.LdapUser) AbstractLdapProvider(opengrok.auth.plugin.ldap.AbstractLdapProvider) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) Test(org.junit.jupiter.api.Test)

Example 5 with DummyHttpServletRequestLdap

use of opengrok.auth.plugin.util.DummyHttpServletRequestLdap in project OpenGrok by OpenGrok.

the class LdapUserPluginTest method testNegativeCache.

@Test
void testNegativeCache() throws LdapException {
    AbstractLdapProvider mockprovider = mock(LdapFacade.class);
    when(mockprovider.lookupLdapContent(isNull(), isNull(), any(String[].class))).thenReturn(null);
    Map<String, Object> params = getParamsMap();
    params.put(LdapUserPlugin.ATTRIBUTES, "mail");
    params.put(LdapUserPlugin.USE_DN, false);
    LdapUserPlugin origPlugin = new LdapUserPlugin();
    LdapUserPlugin plugin = Mockito.spy(origPlugin);
    plugin.load(params, mockprovider);
    assertSame(mockprovider, plugin.getLdapProvider());
    HttpServletRequest dummyRequest = new DummyHttpServletRequestLdap();
    User user = new User("foo@example.com", "id");
    dummyRequest.setAttribute(UserPlugin.REQUEST_ATTR, new User("foo", "123"));
    plugin.fillSession(dummyRequest, user);
    assertNotNull(dummyRequest.getSession().getAttribute(SESSION_ATTR));
    assertFalse(plugin.isAllowed(dummyRequest, new Project("foo")));
    assertFalse(plugin.isAllowed(dummyRequest, new Group("bar")));
    // Make sure that the session was filled so that the second call to isAllowed() did not fill it again.
    verify(plugin, times(2)).updateSession(eq(dummyRequest), anyString(), anyBoolean());
}
Also used : HttpServletRequest(jakarta.servlet.http.HttpServletRequest) DummyHttpServletRequestLdap(opengrok.auth.plugin.util.DummyHttpServletRequestLdap) Project(org.opengrok.indexer.configuration.Project) Group(org.opengrok.indexer.configuration.Group) LdapUser(opengrok.auth.entity.LdapUser) User(opengrok.auth.plugin.entity.User) AbstractLdapProvider(opengrok.auth.plugin.ldap.AbstractLdapProvider) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Aggregations

LdapUser (opengrok.auth.entity.LdapUser)5 DummyHttpServletRequestLdap (opengrok.auth.plugin.util.DummyHttpServletRequestLdap)5 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)4 User (opengrok.auth.plugin.entity.User)4 Test (org.junit.jupiter.api.Test)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 AbstractLdapProvider (opengrok.auth.plugin.ldap.AbstractLdapProvider)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Set (java.util.Set)2 TreeMap (java.util.TreeMap)2 TreeSet (java.util.TreeSet)1 Group (org.opengrok.indexer.configuration.Group)1 Project (org.opengrok.indexer.configuration.Project)1