Skip to content

Commit

Permalink
Fix and Genericize web-ldap
Browse files Browse the repository at this point in the history
  • Loading branch information
tbarsballe committed Jan 18, 2016
1 parent fc8ba68 commit e6a1bba
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 84 deletions.
@@ -1,18 +1,14 @@
/* (c) 2014 - 2015 Open Source Geospatial Foundation - all rights reserved /* (c) 2014 - 2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans * (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root * This code is licensed under the GPL 2.0 license, available at the root
* application directory. * application directory.
*/ */
package org.geoserver.web.security.ldap; package org.geoserver.web.security.ldap;


import java.util.HashMap; import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;


import javax.naming.AuthenticationException; import javax.naming.AuthenticationException;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;

import org.apache.wicket.ajax.AjaxRequestTarget; import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox; import org.apache.wicket.ajax.markup.html.form.AjaxCheckBox;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink; import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
Expand All @@ -26,19 +22,14 @@
import org.apache.wicket.model.IModel; import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model; import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel; import org.apache.wicket.model.StringResourceModel;
import org.geoserver.security.ldap.GeoserverLdapBindAuthenticator;
import org.geoserver.security.ldap.LDAPAuthenticationProvider; import org.geoserver.security.ldap.LDAPAuthenticationProvider;
import org.geoserver.security.ldap.LDAPSecurityProvider; import org.geoserver.security.ldap.LDAPSecurityProvider;
import org.geoserver.security.ldap.LDAPSecurityServiceConfig; import org.geoserver.security.ldap.LDAPSecurityServiceConfig;
import org.geoserver.security.web.auth.AuthenticationProviderPanel; import org.geoserver.security.web.auth.AuthenticationProviderPanel;
import org.geoserver.security.web.usergroup.UserGroupServiceChoice; import org.geoserver.security.web.usergroup.UserGroupServiceChoice;
import org.geoserver.web.GeoServerBasePage;
import org.geoserver.web.util.MapModel; import org.geoserver.web.util.MapModel;
import org.springframework.ldap.core.support.DefaultTlsDirContextAuthenticationStrategy;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication; import org.springframework.security.core.Authentication;
import org.springframework.security.ldap.DefaultSpringSecurityContextSource;
import org.springframework.security.ldap.authentication.SpringSecurityAuthenticationSource;


/** /**
* Configuration panel for {@link LDAPAuthenticationProvider}. * Configuration panel for {@link LDAPAuthenticationProvider}.
Expand All @@ -47,17 +38,22 @@
*/ */
public class LDAPAuthProviderPanel extends AuthenticationProviderPanel<LDAPSecurityServiceConfig> { public class LDAPAuthProviderPanel extends AuthenticationProviderPanel<LDAPSecurityServiceConfig> {


private static final long serialVersionUID = 4772173006888418298L;

public LDAPAuthProviderPanel(String id, IModel<LDAPSecurityServiceConfig> model) { public LDAPAuthProviderPanel(String id, IModel<LDAPSecurityServiceConfig> model) {
super(id, model); super(id, model);


add(new TextField("serverURL").setRequired(true)); add(new TextField<String>("serverURL").setRequired(true));
add(new CheckBox("useTLS")); add(new CheckBox("useTLS"));
add(new TextField("userDnPattern")); add(new TextField<String>("userDnPattern"));
add(new TextField("userFilter")); add(new TextField<String>("userFilter"));
add(new TextField("userFormat")); add(new TextField<String>("userFormat"));


boolean useLdapAuth = model.getObject().getUserGroupServiceName() == null; boolean useLdapAuth = model.getObject().getUserGroupServiceName() == null;
add(new AjaxCheckBox("useLdapAuthorization", new Model(useLdapAuth)) { add(new AjaxCheckBox("useLdapAuthorization", new Model<Boolean>(useLdapAuth)) {

private static final long serialVersionUID = 2060279075143716273L;

@Override @Override
protected void onUpdate(AjaxRequestTarget target) { protected void onUpdate(AjaxRequestTarget target) {
WebMarkupContainer c = (WebMarkupContainer) WebMarkupContainer c = (WebMarkupContainer)
Expand Down Expand Up @@ -86,17 +82,21 @@ AuthorizationPanel createAuthorizationPanel(String id, boolean useLDAP) {
return useLDAP ? new LDAPAuthorizationPanel(id) : new UserGroupAuthorizationPanel(id); return useLDAP ? new LDAPAuthorizationPanel(id) : new UserGroupAuthorizationPanel(id);
} }


abstract class AuthorizationPanel extends FormComponentPanel { abstract class AuthorizationPanel extends FormComponentPanel<HashMap<String, Object>> {

private static final long serialVersionUID = -2021795762927385164L;


public AuthorizationPanel(String id) { public AuthorizationPanel(String id) {
super(id, new Model()); super(id, new Model<HashMap<String, Object>>());
} }


public abstract void resetModel(); public abstract void resetModel();
} }


class UserGroupAuthorizationPanel extends AuthorizationPanel { class UserGroupAuthorizationPanel extends AuthorizationPanel {


private static final long serialVersionUID = 2464048864034610244L;

public UserGroupAuthorizationPanel(String id) { public UserGroupAuthorizationPanel(String id) {
super(id); super(id);


Expand All @@ -111,50 +111,56 @@ public void resetModel() {


class LDAPAuthorizationPanel extends AuthorizationPanel { class LDAPAuthorizationPanel extends AuthorizationPanel {


private static final long serialVersionUID = 7541432269535150812L;

public LDAPAuthorizationPanel(String id) { public LDAPAuthorizationPanel(String id) {
super(id); super(id);
add(new CheckBox("bindBeforeGroupSearch")); add(new CheckBox("bindBeforeGroupSearch"));
add(new TextField("adminGroup")); add(new TextField<String>("adminGroup"));
add(new TextField("groupAdminGroup")); add(new TextField<String>("groupAdminGroup"));
add(new TextField("groupSearchBase")); add(new TextField<String>("groupSearchBase"));
add(new TextField("groupSearchFilter")); add(new TextField<String>("groupSearchFilter"));
} }


@Override @Override
public void resetModel() { public void resetModel() {
get("bindBeforeGroupSearch").setDefaultModelObject(null); get("bindBeforeGroupSearch").setDefaultModelObject(null);
get("adminGroup").setDefaultModelObject(null); get("adminGroup").setDefaultModelObject(null);
get("groupAdminGroup").setDefaultModelObject(null); get("groupAdminGroup").setDefaultModelObject(null);
get("groupSearchBase").setDefaultModelObject(null); get("groupSearchBase").setDefaultModelObject(null);
get("groupSearchFilter").setDefaultModelObject(null); get("groupSearchFilter").setDefaultModelObject(null);
} }
} }


class TestLDAPConnectionPanel extends FormComponentPanel { class TestLDAPConnectionPanel extends FormComponentPanel<HashMap<String, Object>> {

private static final long serialVersionUID = 5433983389877706266L;


public TestLDAPConnectionPanel(String id) { public TestLDAPConnectionPanel(String id) {
super(id, new Model(new HashMap())); super(id, new Model<HashMap<String, Object>>(new HashMap<String, Object>()));


add(new TextField("username", new MapModel(getModel(), "username"))); add(new TextField<HashMap<String, Object>>("username", new MapModel<HashMap<String, Object>>(getModel().getObject(), "username")));
add(new PasswordTextField("password", new MapModel(getModel(), "password")).setRequired(false)); add(new PasswordTextField("password", new MapModel<String>(getModel().getObject(), "password")).setRequired(false));
add(new AjaxSubmitLink("test") { add(new AjaxSubmitLink("test") {


private static final long serialVersionUID = 2373404292655355758L;

@Override @Override
protected void onSubmit(AjaxRequestTarget target, Form<?> form) { protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
//since this is not a regular form submit we have to manually update models //since this is not a regular form submit we have to manually update models
// of form components we care about // of form components we care about
((FormComponent)TestLDAPConnectionPanel.this.get("username")).processInput(); ((FormComponent<?>)TestLDAPConnectionPanel.this.get("username")).processInput();
((FormComponent)TestLDAPConnectionPanel.this.get("password")).processInput(); ((FormComponent<?>)TestLDAPConnectionPanel.this.get("password")).processInput();


((FormComponent)LDAPAuthProviderPanel.this.get("serverURL")).processInput(); ((FormComponent<?>)LDAPAuthProviderPanel.this.get("serverURL")).processInput();
((FormComponent)LDAPAuthProviderPanel.this.get("useTLS")).processInput(); ((FormComponent<?>)LDAPAuthProviderPanel.this.get("useTLS")).processInput();


((FormComponent)LDAPAuthProviderPanel.this.get("userDnPattern")).processInput(); ((FormComponent<?>)LDAPAuthProviderPanel.this.get("userDnPattern")).processInput();
((FormComponent)LDAPAuthProviderPanel.this.get("userFilter")).processInput(); ((FormComponent<?>)LDAPAuthProviderPanel.this.get("userFilter")).processInput();
((FormComponent)LDAPAuthProviderPanel.this.get("userFormat")).processInput(); ((FormComponent<?>)LDAPAuthProviderPanel.this.get("userFormat")).processInput();


String username = (String)((FormComponent)TestLDAPConnectionPanel.this.get("username")).getConvertedInput(); String username = (String)((FormComponent<?>)TestLDAPConnectionPanel.this.get("username")).getConvertedInput();
String password = (String)((FormComponent)TestLDAPConnectionPanel.this.get("password")).getConvertedInput(); String password = (String)((FormComponent<?>)TestLDAPConnectionPanel.this.get("password")).getConvertedInput();


LDAPSecurityServiceConfig ldapConfig = (LDAPSecurityServiceConfig) getForm().getModelObject(); LDAPSecurityServiceConfig ldapConfig = (LDAPSecurityServiceConfig) getForm().getModelObject();
doTest(ldapConfig, username, password); doTest(ldapConfig, username, password);
Expand Down Expand Up @@ -185,7 +191,7 @@ void doTest(LDAPSecurityServiceConfig ldapConfig, String username,


provider.destroy(null); provider.destroy(null);
info(new StringResourceModel(LDAPAuthProviderPanel.class.getSimpleName() + info(new StringResourceModel(LDAPAuthProviderPanel.class.getSimpleName() +
".connectionSuccessful", null).getObject()); ".connectionSuccessful").getObject());
} catch (Exception e) { } catch (Exception e) {
error(e); error(e);
LOGGER.log(Level.WARNING, e.getMessage(), e); LOGGER.log(Level.WARNING, e.getMessage(), e);
Expand Down
@@ -1,4 +1,4 @@
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved /* (c) 2014 - 2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans * (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root * This code is licensed under the GPL 2.0 license, available at the root
* application directory. * application directory.
Expand All @@ -17,6 +17,8 @@
public class LDAPAuthProviderPanelInfo public class LDAPAuthProviderPanelInfo
extends AuthenticationProviderPanelInfo<LDAPSecurityServiceConfig, LDAPAuthProviderPanel> { extends AuthenticationProviderPanelInfo<LDAPSecurityServiceConfig, LDAPAuthProviderPanel> {


private static final long serialVersionUID = 4754541712479614409L;

public LDAPAuthProviderPanelInfo() { public LDAPAuthProviderPanelInfo() {
setComponentClass(LDAPAuthProviderPanel.class); setComponentClass(LDAPAuthProviderPanel.class);
setServiceClass(LDAPAuthenticationProvider.class); setServiceClass(LDAPAuthenticationProvider.class);
Expand Down
@@ -1,4 +1,4 @@
/* (c) 2014 - 2015 Open Source Geospatial Foundation - all rights reserved /* (c) 2014 - 2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2013 OpenPlans * (c) 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root * This code is licensed under the GPL 2.0 license, available at the root
* application directory. * application directory.
Expand All @@ -16,18 +16,19 @@
import org.apache.wicket.model.Model; import org.apache.wicket.model.Model;
import org.geoserver.security.ldap.LDAPRoleServiceConfig; import org.geoserver.security.ldap.LDAPRoleServiceConfig;
import org.geoserver.security.web.role.RoleServicePanel; import org.geoserver.security.web.role.RoleServicePanel;
import org.geoserver.web.security.ldap.LDAPAuthProviderPanel.AuthorizationPanel;
import org.geoserver.web.security.ldap.LDAPAuthProviderPanel.LDAPAuthorizationPanel;
import org.geoserver.web.security.ldap.LDAPAuthProviderPanel.UserGroupAuthorizationPanel;


public class LDAPRoleServicePanel extends RoleServicePanel<LDAPRoleServiceConfig> { public class LDAPRoleServicePanel extends RoleServicePanel<LDAPRoleServiceConfig> {



private static final long serialVersionUID = -67788557484913489L;
class LDAPAuthenticationPanel extends FormComponentPanel {
class LDAPAuthenticationPanel extends FormComponentPanel<String> {


/** serialVersionUID */
private static final long serialVersionUID = 8919421089437979222L;

public LDAPAuthenticationPanel(String id) { public LDAPAuthenticationPanel(String id) {
super(id, new Model()); super(id, new Model<String>());
add(new TextField("user")); add(new TextField<String>("user"));


PasswordTextField pwdField = new PasswordTextField("password"); PasswordTextField pwdField = new PasswordTextField("password");
// avoid reseting the password which results in an // avoid reseting the password which results in an
Expand All @@ -44,13 +45,15 @@ public void resetModel() {


public LDAPRoleServicePanel(String id, IModel<LDAPRoleServiceConfig> model) { public LDAPRoleServicePanel(String id, IModel<LDAPRoleServiceConfig> model) {
super(id, model); super(id, model);
add(new TextField("serverURL").setRequired(true)); add(new TextField<String>("serverURL").setRequired(true));
add(new CheckBox("useTLS")); add(new CheckBox("useTLS"));
add(new TextField("groupSearchBase").setRequired(true)); add(new TextField<String>("groupSearchBase").setRequired(true));
add(new TextField("groupSearchFilter")); add(new TextField<String>("groupSearchFilter"));
add(new TextField("allGroupsSearchFilter")); add(new TextField<String>("allGroupsSearchFilter"));
add(new TextField("userFilter")); add(new TextField<String>("userFilter"));
add(new AjaxCheckBox("bindBeforeGroupSearch") { add(new AjaxCheckBox("bindBeforeGroupSearch") {
private static final long serialVersionUID = -1675695153498067857L;

@Override @Override
protected void onUpdate(AjaxRequestTarget target) { protected void onUpdate(AjaxRequestTarget target) {
WebMarkupContainer c = (WebMarkupContainer) WebMarkupContainer c = (WebMarkupContainer)
Expand Down
@@ -1,4 +1,4 @@
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved /* (c) 2014 - 2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2013 OpenPlans * (c) 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root * This code is licensed under the GPL 2.0 license, available at the root
* application directory. * application directory.
Expand All @@ -12,11 +12,11 @@


public class LDAPRoleServicePanelInfo extends RoleServicePanelInfo<LDAPRoleServiceConfig, LDAPRoleServicePanel> { public class LDAPRoleServicePanelInfo extends RoleServicePanelInfo<LDAPRoleServiceConfig, LDAPRoleServicePanel> {


public LDAPRoleServicePanelInfo() { private static final long serialVersionUID = 2157416730424175291L;
setComponentClass(LDAPRoleServicePanel.class);
setServiceClass(LDAPRoleService.class);
setServiceConfigClass(LDAPRoleServiceConfig.class);
}



public LDAPRoleServicePanelInfo() {
setComponentClass(LDAPRoleServicePanel.class);
setServiceClass(LDAPRoleService.class);
setServiceConfigClass(LDAPRoleServiceConfig.class);
}
} }
@@ -1,10 +1,11 @@
/* (c) 2014 Open Source Geospatial Foundation - all rights reserved /* (c) 2014 - 2016 Open Source Geospatial Foundation - all rights reserved
* (c) 2001 - 2013 OpenPlans * (c) 2001 - 2013 OpenPlans
* This code is licensed under the GPL 2.0 license, available at the root * This code is licensed under the GPL 2.0 license, available at the root
* application directory. * application directory.
*/ */
package org.geoserver.web.security.ldap; package org.geoserver.web.security.ldap;


import java.io.Serializable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;


Expand Down Expand Up @@ -55,14 +56,12 @@ public class LDAPAuthProviderPanelTest extends AbstractSecurityWicketTestSupport


@After @After
public void tearDown() throws Exception { public void tearDown() throws Exception {
LdapTestUtils LdapTestUtils.shutdownEmbeddedServer();
.destroyApacheDirectoryServer(LdapTestUtils.DEFAULT_PRINCIPAL,
LdapTestUtils.DEFAULT_PASSWORD);
} }




protected void setupPanel(final String userDnPattern, String userFilter, protected void setupPanel(final String userDnPattern, String userFilter,
String userFormat, String userGroupService) { String userFormat, String userGroupService) {
config = new LDAPSecurityServiceConfig(); config = new LDAPSecurityServiceConfig();
config.setName("test"); config.setName("test");
config.setServerURL(ldapServerUrl + "/" + basePath); config.setServerURL(ldapServerUrl + "/" + basePath);
Expand All @@ -85,13 +84,15 @@ protected void onSetUp(SystemTestData testData) throws Exception {
protected void setupPanel(LDAPSecurityServiceConfig theConfig) { protected void setupPanel(LDAPSecurityServiceConfig theConfig) {
this.config = theConfig; this.config = theConfig;
tester.startPage(new FormTestPage(new ComponentBuilder() { tester.startPage(new FormTestPage(new ComponentBuilder() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 7319919840443122283L;

public Component buildComponent(String id) { public Component buildComponent(String id) {


return current = new LDAPAuthProviderPanel(id, new Model(config)); return current = new LDAPAuthProviderPanel(id, new Model<LDAPSecurityServiceConfig>(config));
}; };
}, new CompoundPropertyModel(config)){ }, new CompoundPropertyModel<Object>(config)){

private static final long serialVersionUID = 3150973967583096118L;


@Override @Override
protected void onBeforeRender() { protected void onBeforeRender() {
Expand Down Expand Up @@ -124,8 +125,6 @@ public void testTestConnectionWithUserFilter() throws Exception {
setupPanel(null, USER_FILTER, USER_FORMAT, null); setupPanel(null, USER_FILTER, USER_FORMAT, null);
testSuccessfulConnection(); testSuccessfulConnection();
} }






@Test @Test
Expand All @@ -147,8 +146,8 @@ private void testSuccessfulConnection() throws Exception {


tester.assertNoErrorMessage(); tester.assertNoErrorMessage();
String success = new StringResourceModel(LDAPAuthProviderPanel.class.getSimpleName() + String success = new StringResourceModel(LDAPAuthProviderPanel.class.getSimpleName() +
".connectionSuccessful", null).getObject(); ".connectionSuccessful").getObject();
tester.assertInfoMessages(new String[] { success }); tester.assertInfoMessages((Serializable[]) new String[] { success });
} }




Expand All @@ -162,10 +161,10 @@ private void testFailedConnection() throws Exception {
} }


private void authenticate(String username, String password) { private void authenticate(String username, String password) {
TextField userField = ((TextField)tester.getComponentFromLastRenderedPage(base+ "testCx:username")); TextField<?> userField = ((TextField<?>)tester.getComponentFromLastRenderedPage(base+ "testCx:username"));
userField.setDefaultModel(new Model(username)); userField.setDefaultModel(new Model<String>(username));
TextField passwordField = ((TextField)tester.getComponentFromLastRenderedPage(base+ "testCx:password")); TextField<?> passwordField = ((TextField<?>)tester.getComponentFromLastRenderedPage(base+ "testCx:password"));
passwordField.setDefaultModel(new Model(password)); passwordField.setDefaultModel(new Model<String>(password));


Map<String, String> map = new HashMap<String, String>(); Map<String, String> map = new HashMap<String, String>();
map.put("username", username); map.put("username", username);
Expand Down

0 comments on commit e6a1bba

Please sign in to comment.