import org.sonar.server.user.index.UserIndexer;
import org.sonar.server.user.ws.CurrentAction;
import org.sonar.server.user.ws.FavoritesWs;
+import org.sonar.server.user.ws.IdentityProvidersAction;
import org.sonar.server.user.ws.UserJsonWriter;
import org.sonar.server.user.ws.UserPropertiesWs;
import org.sonar.server.user.ws.UsersWs;
CurrentAction.class,
org.sonar.server.user.ws.SearchAction.class,
org.sonar.server.user.ws.GroupsAction.class,
+ IdentityProvidersAction.class,
FavoritesWs.class,
UserPropertiesWs.class,
UserIndexDefinition.class,
--- /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.server.user.ws;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
+import javax.annotation.Nonnull;
+import org.sonar.api.server.authentication.Display;
+import org.sonar.api.server.authentication.IdentityProvider;
+import org.sonar.api.server.ws.Request;
+import org.sonar.api.server.ws.Response;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.authentication.IdentityProviderRepository;
+import org.sonarqube.ws.WsUsers;
+import org.sonarqube.ws.WsUsers.IdentityProvidersWsResponse;
+
+import static org.sonar.server.ws.WsUtils.writeProtobuf;
+
+public class IdentityProvidersAction implements UsersWsAction {
+ private final IdentityProviderRepository identityProviderRepository;
+
+ public IdentityProvidersAction(IdentityProviderRepository identityProviderRepository) {
+ this.identityProviderRepository = identityProviderRepository;
+ }
+
+ @Override
+ public void define(WebService.NewController context) {
+ context.createAction("identity_providers")
+ .setDescription("List the external identity providers")
+ .setResponseExample(getClass().getResource("identity_providers-example.json"))
+ .setSince("5.5")
+ .setInternal(true)
+ .setHandler(this);
+ }
+
+ @Override
+ public void handle(Request request, Response response) throws Exception {
+ writeProtobuf(buildResponse(), request, response);
+ }
+
+ private IdentityProvidersWsResponse buildResponse() {
+ IdentityProvidersWsResponse.Builder response = IdentityProvidersWsResponse.newBuilder();
+ response.addAllIdentityProviders(Lists.transform(identityProviderRepository.getAllEnabledAndSorted(), IdentityProviderToWsResponse.INSTANCE));
+ return response.build();
+ }
+
+ private enum IdentityProviderToWsResponse implements Function<IdentityProvider, WsUsers.IdentityProvider> {
+ INSTANCE;
+
+ @Override
+ public WsUsers.IdentityProvider apply(@Nonnull IdentityProvider input) {
+ WsUsers.IdentityProvider.Builder builder = WsUsers.IdentityProvider.newBuilder()
+ .setKey(input.getKey())
+ .setName(input.getName());
+ Display display = input.getDisplay();
+ builder
+ .setIconPath(display.getIconPath())
+ .setBackgroundColor(display.getBackgroundColor());
+
+ return builder.build();
+ }
+ }
+}
--- /dev/null
+{
+ "identityProviders": [
+ {
+ "key": "bitbucket",
+ "name": "Bitbucket",
+ "iconPath": "/static/authbitbucket/bitbucket.svg",
+ "backgroundColor": "#205081"
+ },
+ {
+ "key": "github",
+ "name": "Github",
+ "iconPath": "/static/authgithub/github.svg",
+ "backgroundColor": "#444444"
+ }
+ ]
+}
--- /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.server.user.ws;
+
+import java.io.IOException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.server.authentication.Display;
+import org.sonar.api.server.authentication.IdentityProvider;
+import org.sonar.api.server.ws.WebService;
+import org.sonar.server.authentication.IdentityProviderRepositoryRule;
+import org.sonar.server.authentication.TestIdentityProvider;
+import org.sonar.server.ws.WsActionTester;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.test.JsonAssert.assertJson;
+
+public class IdentityProvidersActionTest {
+ static IdentityProvider GITHUB = new TestIdentityProvider()
+ .setKey("github")
+ .setName("Github")
+ .setDisplay(Display.builder()
+ .setIconPath("/static/authgithub/github.svg")
+ .setBackgroundColor("#444444")
+ .build())
+ .setEnabled(true);
+
+ static IdentityProvider BITBUCKET = new TestIdentityProvider()
+ .setKey("bitbucket")
+ .setName("Bitbucket")
+ .setDisplay(Display.builder()
+ .setIconPath("/static/authbitbucket/bitbucket.svg")
+ .setBackgroundColor("#205081")
+ .build())
+ .setEnabled(true);
+
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
+ @Rule
+ public IdentityProviderRepositoryRule identityProviderRepository = new IdentityProviderRepositoryRule()
+ .addIdentityProvider(GITHUB)
+ .addIdentityProvider(BITBUCKET);
+
+ WsActionTester ws = new WsActionTester(new IdentityProvidersAction(identityProviderRepository));
+
+ @Test
+ public void json_example() throws IOException {
+ String response = ws.newRequest().execute().getInput();
+
+ assertJson(response).isSimilarTo(getClass().getResource("identity_providers-example.json"));
+ }
+
+ @Test
+ public void ws_properties() {
+ WebService.Action webService = ws.getDef();
+
+ assertThat(webService.key()).isEqualTo("identity_providers");
+ assertThat(webService.responseExampleAsString()).isNotEmpty();
+ assertThat(webService.since()).isEqualTo("5.5");
+ assertThat(webService.isInternal()).isTrue();
+
+ }
+}
--- /dev/null
+// SonarQube, open source software quality management tool.
+// Copyright (C) 2008-2015 SonarSource
+// mailto:contact AT sonarsource DOT com
+//
+// SonarQube 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.
+//
+// SonarQube 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.
+
+syntax = "proto2";
+
+package sonarqube.ws.users;
+
+option java_package = "org.sonarqube.ws";
+option java_outer_classname = "WsUsers";
+option optimize_for = SPEED;
+
+// WS api/users/identity_providers
+message IdentityProvidersWsResponse {
+ repeated IdentityProvider identityProviders = 1;
+}
+
+message IdentityProvider {
+ optional string key = 1;
+ optional string name = 2;
+ optional string iconPath = 3;
+ optional string backgroundColor = 4;
+}