import java.io.IOException;
import org.sonar.api.config.Settings;
import org.sonar.api.server.authentication.BaseIdentityProvider;
+import org.sonar.api.server.authentication.Display;
import org.sonar.api.server.authentication.UserIdentity;
public class FakeBaseIdProvider implements BaseIdentityProvider {
}
@Override
- public String getIconPath() {
- return "/static/baseauthplugin/base.png";
+ public Display getDisplay() {
+ return Display.builder()
+ .setIconPath("/static/baseauthplugin/base.png")
+ .setBackgroundColor("#205081")
+ .build();
}
@Override
*/
import org.sonar.api.config.Settings;
+import org.sonar.api.server.authentication.Display;
import org.sonar.api.server.authentication.OAuth2IdentityProvider;
import org.sonar.api.server.authentication.UserIdentity;
}
@Override
- public String getIconPath() {
- return "/static/oauth2authplugin/oauth2.png";
+ public Display getDisplay() {
+ return Display.builder()
+ .setIconPath("/static/oauth2authplugin/oauth2.png")
+ .setBackgroundColor("#444444")
+ .build();
}
@Override
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.server.authentication.BaseIdentityProvider;
+import org.sonar.api.server.authentication.Display;
import org.sonar.api.server.authentication.IdentityProvider;
import org.sonar.api.server.authentication.OAuth2IdentityProvider;
import org.sonar.api.utils.log.LogTester;
}
@Override
- public String getIconPath() {
+ public Display getDisplay() {
return null;
}
*/
package org.sonar.server.authentication;
+import org.sonar.api.server.authentication.Display;
import org.sonar.api.server.authentication.IdentityProvider;
public class TestIdentityProvider implements IdentityProvider {
private String key;
private String name;
- private String iconPatch;
+ private Display display;
private boolean enabled;
private boolean allowsUsersToSignUp;
}
@Override
- public String getIconPath() {
- return iconPatch;
+ public Display getDisplay() {
+ return display;
}
- public TestIdentityProvider setIconPatch(String iconPatch) {
- this.iconPatch = iconPatch;
+ public TestIdentityProvider setDisplay(Display display) {
+ this.display = display;
return this;
}
<ul>
<% auth_providers.each do |provider| %>
<li>
- <a href="<%= ApplicationController.root_context -%>/sessions/init/<%= provider.getKey().to_s %>"
+ <a href="/sessions/init/<%= provider.getKey().to_s %>"
+ style="background-color: <%= provider.getDisplay().getBackgroundColor().to_s %>"
title="Log in with <%= provider.getName().to_s -%>">
<img alt="<%= provider.getName().to_s -%>" width="20" height="20"
- src="<%= ApplicationController.root_context + provider.getIconPath().to_s -%>">
+ src="<%= provider.getDisplay().getIconPath().to_s -%>">
<span>Log in with <%= provider.getName().to_s -%></span>
</a>
</li>
</p>
<div>
- <div class="pull-left">
- <% auth_providers = Api::Utils.java_facade.getIdentityProviders().to_a %>
- <ul class="list-inline">
- <% auth_providers.each do |provider| %>
- <li>
- <a class="oauth-link"
- href="/sessions/init/<%= provider.getKey().to_s %>"
- title="Login with <%= provider.getName().to_s -%>">
- <img alt="<%= provider.getName().to_s -%>" width="24" height="24"
- src="<%= provider.getIconPath().to_s -%>">
- </a>
- </li>
- <% end %>
- </ul>
- </div>
<div class="text-right overflow-hidden">
<button name="commit"><%= message('sessions.log_in') -%></button>
<a class="spacer-left" href="<%= home_path -%>"><%= message('cancel') -%></a>
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.server.authentication;
+
+import javax.annotation.concurrent.Immutable;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static org.apache.commons.lang.StringUtils.isNotBlank;
+
+/**
+ * Display information provided by the Identity Provider to be displayed into the login form.
+ *
+ * @since 5.4
+ */
+@Immutable
+public final class Display {
+
+ private final String iconPath;
+ private final String backgroundColor;
+
+ private Display(Builder builder) {
+ this.iconPath = builder.iconPath;
+ this.backgroundColor = builder.backgroundColor;
+ }
+
+ /**
+ * URL path to the provider icon, as deployed at runtime, for example "/static/authgithub/github.svg" (in this
+ * case "authgithub" is the plugin key. Source file is "src/main/resources/static/github.svg").
+ * It can also be an external URL, for example "http://www.mydomain/myincon.png".
+ *
+ * Must not be blank.
+ * <p/>
+ * The recommended format is SVG with a size of 24x24 pixels.
+ * Other supported format is PNG, with a size of 40x40 pixels.
+ */
+ public String getIconPath() {
+ return iconPath;
+ }
+
+ /**
+ * Background color for the provider button displayed in the login form.
+ * It's a Hexadecimal value, for instance #205081.
+ * <p/>
+ * If not provided, the default value is black (#000000)
+ */
+ public String getBackgroundColor() {
+ return backgroundColor;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static class Builder {
+
+ private String iconPath;
+ private String backgroundColor = "#000000";
+
+ private Builder() {
+ }
+
+ /**
+ * @see Display#getIconPath()
+ */
+ public Builder setIconPath(String iconPath) {
+ this.iconPath = iconPath;
+ return this;
+ }
+
+ /**
+ * @see Display#getBackgroundColor()
+ */
+ public Builder setBackgroundColor(String backgroundColor) {
+ this.backgroundColor = backgroundColor;
+ return this;
+ }
+
+ public Display build() {
+ checkArgument(isNotBlank(iconPath), "Icon path must not be blank");
+ validateBackgroundColor();
+ return new Display(this);
+ }
+
+ private void validateBackgroundColor() {
+ checkArgument(isNotBlank(backgroundColor), "Background color must not be blank");
+ checkArgument(backgroundColor.length() == 7 && backgroundColor.startsWith("#"),
+ "Background color must begin with a sharp followed by 6 characters");
+ }
+ }
+}
String getName();
/**
- * URL path to the provider icon, as deployed at runtime, for example "/static/authgithub/github.svg" (in this
- * case "authgithub" is the plugin key. Source file is "src/main/resources/static/github.svg"). Must not be blank.
- * <p/>
- * The recommended format is SVG with a size of 24x24 pixels.
- * Other supported format is PNG, with a size of 48x48 pixels.
+ * Display information for the login form
*/
- String getIconPath();
+ Display getDisplay();
/**
* Is the provider fully configured and enabled ? If {@code true}, then
* registered users can login.
*/
boolean allowsUsersToSignUp();
+
}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2016 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ */
+package org.sonar.api.server.authentication;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class DisplayTest {
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @Test
+ public void create_display() throws Exception {
+ Display display = Display.builder()
+ .setIconPath("/static/authgithub/github.svg")
+ .setBackgroundColor("#123456")
+ .build();
+
+ assertThat(display.getIconPath()).isEqualTo("/static/authgithub/github.svg");
+ assertThat(display.getBackgroundColor()).isEqualTo("#123456");
+ }
+
+ @Test
+ public void create_display_woth_default_background_color() throws Exception {
+ Display display = Display.builder()
+ .setIconPath("/static/authgithub/github.svg")
+ .build();
+
+ assertThat(display.getBackgroundColor()).isEqualTo("#000000");
+ }
+
+ @Test
+ public void fail_when_icon_path_is_null() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Icon path must not be blank");
+
+ Display.builder()
+ .setIconPath(null)
+ .setBackgroundColor("#123456")
+ .build();
+ }
+
+ @Test
+ public void fail_when_icon_path_is_blank() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Icon path must not be blank");
+
+ Display.builder()
+ .setIconPath("")
+ .setBackgroundColor("#123456")
+ .build();
+ }
+
+ @Test
+ public void fail_when_background_color_is_null() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Background color must not be blank");
+
+ Display.builder()
+ .setIconPath("/static/authgithub/github.svg")
+ .setBackgroundColor(null)
+ .build();
+ }
+
+ @Test
+ public void fail_when_background_color_is_blank() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Background color must not be blank");
+
+ Display.builder()
+ .setIconPath("/static/authgithub/github.svg")
+ .setBackgroundColor("")
+ .build();
+ }
+
+ @Test
+ public void fail_when_background_color_has_wrong_size() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Background color must begin with a sharp followed by 6 characters");
+
+ Display.builder()
+ .setIconPath("/static/authgithub/github.svg")
+ .setBackgroundColor("#1234")
+ .build();
+ }
+
+ @Test
+ public void fail_when_background_color_doesnt_begin_with_sharp() throws Exception {
+ thrown.expect(IllegalArgumentException.class);
+ thrown.expectMessage("Background color must begin with a sharp followed by 6 characters");
+
+ Display.builder()
+ .setIconPath("/static/authgithub/github.svg")
+ .setBackgroundColor("*123456")
+ .build();
+ }
+}