From 387f39bfe180efdfd5426292356fb1d7ec0f0da0 Mon Sep 17 00:00:00 2001 From: Sergey Averkiev Date: Wed, 29 Jan 2014 16:26:57 +0400 Subject: [PATCH] Add feature to set custom realm description --- .../sonia/scm/config/ScmConfiguration.java | 31 ++++++++++++++ .../main/java/sonia/scm/util/HttpUtil.java | 41 +++++++++++++++++-- .../web/filter/BasicAuthenticationFilter.java | 2 +- .../scm/web/filter/PermissionFilter.java | 2 +- .../js/config/sonia.config.scmconfigpanel.js | 8 ++++ 5 files changed, 78 insertions(+), 6 deletions(-) diff --git a/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java b/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java index 40f662969f..d65589db82 100644 --- a/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java +++ b/scm-core/src/main/java/sonia/scm/config/ScmConfiguration.java @@ -44,6 +44,7 @@ import org.slf4j.LoggerFactory; import sonia.scm.ConfigChangedListener; import sonia.scm.ListenerSupport; import sonia.scm.event.ScmEventBus; +import sonia.scm.util.HttpUtil; import sonia.scm.xml.XmlSetStringAdapter; //~--- JDK imports ------------------------------------------------------------ @@ -157,6 +158,7 @@ public class ScmConfiguration */ public void load(ScmConfiguration other) { + this.realmDescription = other.realmDescription; this.dateFormat = other.dateFormat; this.pluginUrl = other.pluginUrl; this.anonymousAccessEnabled = other.anonymousAccessEnabled; @@ -231,6 +233,17 @@ public class ScmConfiguration return baseUrl; } + /** + * Returns the realm description. + * + * + * @return realm description + */ + public String getRealmDescription() + { + return realmDescription; + } + /** * Returns the date format for the user interface. This format is a * JavaScript date format, from the library moment.js. @@ -516,6 +529,17 @@ public class ScmConfiguration this.baseUrl = baseUrl; } + /** + * Sets the realm description. + * + * + * @param realmDescription + */ + public void setRealmDescription(String realmDescription) + { + this.realmDescription = realmDescription; + } + /** * Sets the date format for the ui. * @@ -824,6 +848,13 @@ public class ScmConfiguration /** Field description */ private boolean disableGroupingGrid = false; + /** + * + * Authentication realm for basic authentication. + * + */ + private String realmDescription = HttpUtil.AUTHENTICATION_REALM; + /** * JavaScript date format from moment.js * @see http://momentjs.com/docs/#/parsing/ diff --git a/scm-core/src/main/java/sonia/scm/util/HttpUtil.java b/scm-core/src/main/java/sonia/scm/util/HttpUtil.java index f1ce453cc1..69f913d74d 100644 --- a/scm-core/src/main/java/sonia/scm/util/HttpUtil.java +++ b/scm-core/src/main/java/sonia/scm/util/HttpUtil.java @@ -416,7 +416,7 @@ public final class HttpUtil throws IOException { - sendUnauthorized(null, response); + sendUnauthorized(null, response, AUTHENTICATION_REALM); } /** @@ -427,17 +427,50 @@ public final class HttpUtil * @param response http response * * @throws IOException - * - * @since 1.19 */ public static void sendUnauthorized(HttpServletRequest request, HttpServletResponse response) throws IOException + { + sendUnauthorized(request, response, AUTHENTICATION_REALM); + } + + /** + * Send an unauthorized header back to the client + * + * + * @param response - the http response + * @param realmDescription - realm description + * + * @throws IOException + */ + public static void sendUnauthorized(HttpServletResponse response, String realmDescription) + throws IOException + { + sendUnauthorized(null, response, realmDescription); + } + + /** + * Send an unauthorized header back to the client + * + * + * @param request http request + * @param response http response + * @param realmDescription realm description + * + * @throws IOException + * + * @since 1.19 + */ + public static void sendUnauthorized(HttpServletRequest request, + HttpServletResponse response, + String realmDescription) + throws IOException { if ((request == null) ||!isWUIRequest(request)) { response.setHeader(HEADER_WWW_AUTHENTICATE, - "Basic realm=\"".concat(AUTHENTICATION_REALM).concat("\"")); + "Basic realm=\"".concat(realmDescription).concat("\"")); } else if (logger.isTraceEnabled()) diff --git a/scm-core/src/main/java/sonia/scm/web/filter/BasicAuthenticationFilter.java b/scm-core/src/main/java/sonia/scm/web/filter/BasicAuthenticationFilter.java index 5a81a7c53c..44801de4d2 100644 --- a/scm-core/src/main/java/sonia/scm/web/filter/BasicAuthenticationFilter.java +++ b/scm-core/src/main/java/sonia/scm/web/filter/BasicAuthenticationFilter.java @@ -216,7 +216,7 @@ public class BasicAuthenticationFilter extends AutoLoginFilter if (Strings.isNullOrEmpty(authentication)) { - HttpUtil.sendUnauthorized(request, response); + HttpUtil.sendUnauthorized(request, response, configuration.getRealmDescription()); } else { diff --git a/scm-core/src/main/java/sonia/scm/web/filter/PermissionFilter.java b/scm-core/src/main/java/sonia/scm/web/filter/PermissionFilter.java index da3a8ead39..07051e5154 100644 --- a/scm-core/src/main/java/sonia/scm/web/filter/PermissionFilter.java +++ b/scm-core/src/main/java/sonia/scm/web/filter/PermissionFilter.java @@ -262,7 +262,7 @@ public abstract class PermissionFilter extends HttpFilter } else { - HttpUtil.sendUnauthorized(response); + HttpUtil.sendUnauthorized(response, configuration.getRealmDescription()); } } diff --git a/scm-webapp/src/main/webapp/resources/js/config/sonia.config.scmconfigpanel.js b/scm-webapp/src/main/webapp/resources/js/config/sonia.config.scmconfigpanel.js index 72b85e2e8a..9f80b5eed8 100644 --- a/scm-webapp/src/main/webapp/resources/js/config/sonia.config.scmconfigpanel.js +++ b/scm-webapp/src/main/webapp/resources/js/config/sonia.config.scmconfigpanel.js @@ -34,6 +34,7 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ titleText: 'General Settings', servnameText: 'Servername', + realmDescriptionText: 'Realm description', dateFormatText: 'Date format', enableForwardingText: 'Enable forwarding (mod_proxy)', forwardPortText: 'Forward Port', @@ -67,6 +68,7 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ // help servernameHelpText: 'The name of this server. This name will be part of the repository url.', + realmDescriptionHelpText: 'Enter authentication realm description', // TODO i18n dateFormatHelpText: 'Moments date format. Please have a look at \n\ http://momentjs.com/docs/#/displaying/format/.
\n\ @@ -129,6 +131,12 @@ Sonia.config.ScmConfigPanel = Ext.extend(Sonia.config.ConfigPanel,{ name: 'enableRepositoryArchive', inputValue: 'true', helpText: this.enableRepositoryArchiveHelpText + },{ + xtype: 'textfield', + fieldLabel: this.realmDescriptionText, + name: 'realmDescription', + allowBlank: false, + helpText: this.realmDescriptionHelpText },{ xtype: 'textfield', fieldLabel: this.dateFormatText,