Search in sources :

Example 1 with LdapUser

use of opengrok.auth.entity.LdapUser in project OpenGrok by OpenGrok.

the class LdapFilterPluginTest method expandFilterTest1.

@Test
void expandFilterTest1() {
    LdapUser ldapUser = new LdapUser();
    ldapUser.setAttribute("mail", new TreeSet<>(Collections.singletonList("james@bond")));
    ldapUser.setAttribute("uid", new TreeSet<>(Collections.singletonList("bondjame")));
    ldapUser.setAttribute("ou", new TreeSet<>(Arrays.asList("MI6", "MI7")));
    User user = new User("007", "123", null, true);
    assertEquals("(objectclass=james@bond)", plugin.expandFilter("(objectclass=%mail%)", ldapUser, user));
    assertEquals("(objectclass=bondjame)", plugin.expandFilter("(objectclass=%uid%)", ldapUser, user));
    assertEquals("(objectclass=007)", plugin.expandFilter("(objectclass=%username%)", ldapUser, user));
    assertEquals("(objectclass=123)", plugin.expandFilter("(objectclass=%guid%)", ldapUser, user));
    ldapUser.setAttribute("role", new TreeSet<>(Collections.singletonList("agent")));
    assertEquals("(objectclass=agent)", plugin.expandFilter("(objectclass=%role%)", ldapUser, user));
    // doesn't work for more than one value
    ldapUser.setAttribute("role", new TreeSet<>(Arrays.asList("agent", "double-agent")));
    assertEquals("(objectclass=%role%)", plugin.expandFilter("(objectclass=%role%)", ldapUser, user));
}
Also used : LdapUser(opengrok.auth.entity.LdapUser) User(opengrok.auth.plugin.entity.User) LdapUser(opengrok.auth.entity.LdapUser) Test(org.junit.jupiter.api.Test)

Example 2 with LdapUser

use of opengrok.auth.entity.LdapUser in project OpenGrok by OpenGrok.

the class LdapFilterPluginTest method expandFilterTest2.

@Test
void expandFilterTest2() {
    LdapUser ldapUser = new LdapUser();
    ldapUser.setAttribute("mail", new TreeSet<>(Collections.singletonList("james@bond")));
    ldapUser.setAttribute("uid", new TreeSet<>(Collections.singletonList("bondjame")));
    ldapUser.setAttribute("ou", new TreeSet<>(Arrays.asList("MI6", "MI7")));
    User user = new User("007", "123", null, true);
    assertEquals("(objectclass=%james@bond%)", plugin.expandFilter("(objectclass=%%mail%%)", ldapUser, user));
    assertEquals("(objectclass=%james@bond%)", plugin.expandFilter("(objectclass=\\%%mail%\\%)", ldapUser, user));
    assertEquals("(objectclass=%mail%)", plugin.expandFilter("(objectclass=\\%mail\\%)", ldapUser, user));
    assertEquals("(objectclass=%mail)", plugin.expandFilter("(objectclass=\\%mail)", ldapUser, user));
    assertEquals("(objectclass=mail)", plugin.expandFilter("(objectclass=mail)", ldapUser, user));
    assertEquals("(objectclass=%mail)", plugin.expandFilter("(objectclass=%mail)", ldapUser, user));
    assertEquals("(objectclass=%%%%)", plugin.expandFilter("(objectclass=\\%%\\%\\%)", ldapUser, user));
}
Also used : LdapUser(opengrok.auth.entity.LdapUser) User(opengrok.auth.plugin.entity.User) LdapUser(opengrok.auth.entity.LdapUser) Test(org.junit.jupiter.api.Test)

Example 3 with LdapUser

use of opengrok.auth.entity.LdapUser 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 4 with LdapUser

use of opengrok.auth.entity.LdapUser 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 5 with LdapUser

use of opengrok.auth.entity.LdapUser 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)

Aggregations

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