From 2dc419fcaae78fdd2ad6922b5ffe3ca688588e30 Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Sun, 5 Dec 2010 15:59:25 +0100 Subject: [PATCH] improve authentication sample --- .../auth/SampleAuthenticationHandler.java | 92 +++++++++++++- .../sonia/scm/sample/auth/SampleConfig.java | 81 ++++++++++++ .../scm/sample/auth/SampleConfigResource.java | 119 ++++++++++++++++++ .../main/resources/META-INF/scm/plugin.xml | 4 + .../scm/sample/auth/sonia.sample-auth.js | 80 ++++++++++++ .../main/webapp/resources/js/sonia.config.js | 6 +- 6 files changed, 377 insertions(+), 5 deletions(-) create mode 100644 samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleConfig.java create mode 100644 samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleConfigResource.java create mode 100644 samples/scm-sample-auth/src/main/resources/sonia/scm/sample/auth/sonia.sample-auth.js diff --git a/samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleAuthenticationHandler.java b/samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleAuthenticationHandler.java index 1f8be29303..d51239bf05 100644 --- a/samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleAuthenticationHandler.java +++ b/samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleAuthenticationHandler.java @@ -35,20 +35,21 @@ package sonia.scm.sample.auth; //~--- non-JDK imports -------------------------------------------------------- -import com.google.inject.Inject; import com.google.inject.Singleton; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import sonia.scm.SCMContextProvider; import sonia.scm.plugin.ext.Extension; import sonia.scm.user.User; -import sonia.scm.user.UserManager; import sonia.scm.util.AssertUtil; import sonia.scm.web.security.AuthenticationHandler; import sonia.scm.web.security.AuthenticationResult; -import sonia.scm.web.security.AuthenticationState; //~--- JDK imports ------------------------------------------------------------ +import java.io.File; import java.io.IOException; import java.util.Map; @@ -57,6 +58,8 @@ import java.util.concurrent.ConcurrentHashMap; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import javax.xml.bind.JAXB; + /** * * @author Sebastian Sdorra @@ -69,6 +72,14 @@ public class SampleAuthenticationHandler implements AuthenticationHandler /** Field description */ public static final String TYPE = "sample"; + /** Field description */ + public static final String CONFIG_PATH = + "config".concat(File.separator).concat("sample-auth.xml"); + + /** the logger for SampleAuthenticationHandler */ + private static final Logger logger = + LoggerFactory.getLogger(SampleAuthenticationHandler.class); + //~--- methods -------------------------------------------------------------- /** @@ -114,6 +125,8 @@ public class SampleAuthenticationHandler implements AuthenticationHandler @Override public void init(SCMContextProvider context) { + configFile = new File(context.getBaseDirectory(), CONFIG_PATH); + loadConfig(); addUser(new User("dent", "Arthur Dent", "arthur.dent@hitchhiker.com")); addUser(new User("perfect", "Ford Prefect", "ford.perfect@hitchhiker.com")); addUser(new User("slarti", "Slartibartfaß", @@ -121,8 +134,60 @@ public class SampleAuthenticationHandler implements AuthenticationHandler addUser(new User("marvin", "Marvin", "paranoid.android@hitchhiker.com")); } + /** + * Method description + * + * + */ + public void loadConfig() + { + if (configFile.exists()) + { + try + { + config = JAXB.unmarshal(configFile, SampleConfig.class); + } + catch (Exception ex) + { + logger.error(ex.getMessage(), ex); + } + } + + if (config == null) + { + config = new SampleConfig(); + } + } + + /** + * Method description + * + */ + public void storeConfig() + { + try + { + JAXB.marshal(config, configFile); + } + catch (Exception ex) + { + logger.error(ex.getMessage(), ex); + } + } + //~--- get methods ---------------------------------------------------------- + /** + * Method description + * + * + * @return + */ + public SampleConfig getConfig() + { + return config; + } + /** * Method description * @@ -135,6 +200,19 @@ public class SampleAuthenticationHandler implements AuthenticationHandler return TYPE; } + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param config + */ + public void setConfig(SampleConfig config) + { + this.config = config; + } + //~--- methods -------------------------------------------------------------- /** @@ -165,7 +243,7 @@ public class SampleAuthenticationHandler implements AuthenticationHandler if (dbUser != null) { - if (password.equals(username.concat("123"))) + if (password.equals(username.concat(config.getPasswordSuffix()))) { result = new AuthenticationResult(dbUser); } @@ -184,6 +262,12 @@ public class SampleAuthenticationHandler implements AuthenticationHandler //~--- fields --------------------------------------------------------------- + /** Field description */ + private SampleConfig config; + + /** Field description */ + private File configFile; + /** Field description */ private Map userDB = new ConcurrentHashMap(); } diff --git a/samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleConfig.java b/samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleConfig.java new file mode 100644 index 0000000000..495f65c797 --- /dev/null +++ b/samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleConfig.java @@ -0,0 +1,81 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.sample.auth; + +//~--- JDK imports ------------------------------------------------------------ + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; + +/** + * + * @author Sebastian Sdorra + */ +@XmlRootElement(name = "sample-config") +@XmlAccessorType(XmlAccessType.FIELD) +public class SampleConfig +{ + + /** + * Method description + * + * + * @return + */ + public String getPasswordSuffix() + { + return passwordSuffix; + } + + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param passwordSuffix + */ + public void setPasswordSuffix(String passwordSuffix) + { + this.passwordSuffix = passwordSuffix; + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + @XmlElement(name = "password-suffix") + private String passwordSuffix = "123"; +} diff --git a/samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleConfigResource.java b/samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleConfigResource.java new file mode 100644 index 0000000000..7f40b34105 --- /dev/null +++ b/samples/scm-sample-auth/src/main/java/sonia/scm/sample/auth/SampleConfigResource.java @@ -0,0 +1,119 @@ +/** + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + + +package sonia.scm.sample.auth; + +//~--- non-JDK imports -------------------------------------------------------- + +import com.google.inject.Inject; +import com.google.inject.Singleton; + +//~--- JDK imports ------------------------------------------------------------ + +import java.io.IOException; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.Produces; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; + +/** + * + * @author Sebastian Sdorra + */ +@Singleton +@Path("config/auth/sample") +public class SampleConfigResource +{ + + /** + * Constructs ... + * + * + * @param authenticationHandler + */ + @Inject + public SampleConfigResource(SampleAuthenticationHandler authenticationHandler) + { + this.authenticationHandler = authenticationHandler; + } + + //~--- get methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @return + */ + @GET + @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public SampleConfig getConfig() + { + return authenticationHandler.getConfig(); + } + + //~--- set methods ---------------------------------------------------------- + + /** + * Method description + * + * + * @param uriInfo + * @param config + * + * @return + * + * @throws IOException + */ + @POST + @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON }) + public Response setConfig(@Context UriInfo uriInfo, SampleConfig config) + throws IOException + { + authenticationHandler.setConfig(config); + authenticationHandler.storeConfig(); + + return Response.created(uriInfo.getRequestUri()).build(); + } + + //~--- fields --------------------------------------------------------------- + + /** Field description */ + private SampleAuthenticationHandler authenticationHandler; +} diff --git a/samples/scm-sample-auth/src/main/resources/META-INF/scm/plugin.xml b/samples/scm-sample-auth/src/main/resources/META-INF/scm/plugin.xml index 8a15e05738..8d240fe27e 100644 --- a/samples/scm-sample-auth/src/main/resources/META-INF/scm/plugin.xml +++ b/samples/scm-sample-auth/src/main/resources/META-INF/scm/plugin.xml @@ -42,4 +42,8 @@ ${project.url} + + + + diff --git a/samples/scm-sample-auth/src/main/resources/sonia/scm/sample/auth/sonia.sample-auth.js b/samples/scm-sample-auth/src/main/resources/sonia/scm/sample/auth/sonia.sample-auth.js new file mode 100644 index 0000000000..d4ae17e69e --- /dev/null +++ b/samples/scm-sample-auth/src/main/resources/sonia/scm/sample/auth/sonia.sample-auth.js @@ -0,0 +1,80 @@ +/* * + * Copyright (c) 2010, Sebastian Sdorra + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of SCM-Manager; nor the names of its + * contributors may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * http://bitbucket.org/sdorra/scm-manager + * + */ + + +registerGeneralConfigPanel({ + xtype : 'configForm', + title : 'Sample Authentication', + items : [{ + xtype : 'textfield', + fieldLabel : 'Password-Suffix', + name : 'password-suffix', + allowBlank : false + }], + + onSubmit: function(values){ + this.el.mask('Submit ...'); + Ext.Ajax.request({ + url: restUrl + 'config/auth/sample.json', + method: 'POST', + jsonData: values, + scope: this, + disableCaching: true, + success: function(response){ + this.el.unmask(); + }, + failure: function(){ + this.el.unmask(); + } + }); + }, + + onLoad: function(el){ + var tid = setTimeout( function(){ el.mask('Loading ...'); }, 100); + Ext.Ajax.request({ + url: restUrl + 'config/auth/sample.json', + method: 'GET', + scope: this, + disableCaching: true, + success: function(response){ + var obj = Ext.decode(response.responseText); + this.load(obj); + clearTimeout(tid); + el.unmask(); + }, + failure: function(){ + el.unmask(); + clearTimeout(tid); + alert('failure'); + } + }); + } +}); \ No newline at end of file diff --git a/scm-webapp/src/main/webapp/resources/js/sonia.config.js b/scm-webapp/src/main/webapp/resources/js/sonia.config.js index f6c5925668..06a73f3d3c 100644 --- a/scm-webapp/src/main/webapp/resources/js/sonia.config.js +++ b/scm-webapp/src/main/webapp/resources/js/sonia.config.js @@ -31,11 +31,15 @@ // config form panels var repositoryConfigPanels = []; +var generalConfigPanels =[]; function registerConfigPanel(panel){ repositoryConfigPanels.push( panel ); } +function registerGeneralConfigPanel(panel){ + generalConfigPanels.push(panel); +} Ext.ns("Sonia.config"); @@ -133,7 +137,7 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ } }); } - }] + }, generalConfigPanels] } Ext.apply(this, Ext.apply(this.initialConfig, config));