From d927ef2f4096f3d925f28397aa6edc597b2ce45d Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Tue, 29 Mar 2016 16:34:18 +0200 Subject: [PATCH] SONAR-7478 WS api/users/identity_providers return external identity providers --- .../platformlevel/PlatformLevel4.java | 2 + .../user/ws/IdentityProvidersAction.java | 81 ++++++++++++++++++ .../user/ws/identity_providers-example.json | 16 ++++ .../user/ws/IdentityProvidersActionTest.java | 82 +++++++++++++++++++ sonar-ws/src/main/protobuf/ws-users.proto | 37 +++++++++ 5 files changed, 218 insertions(+) create mode 100644 server/sonar-server/src/main/java/org/sonar/server/user/ws/IdentityProvidersAction.java create mode 100644 server/sonar-server/src/main/resources/org/sonar/server/user/ws/identity_providers-example.json create mode 100644 server/sonar-server/src/test/java/org/sonar/server/user/ws/IdentityProvidersActionTest.java create mode 100644 sonar-ws/src/main/protobuf/ws-users.proto diff --git a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index f5d88c657fc..3bebe3a0f79 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-server/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -294,6 +294,7 @@ import org.sonar.server.user.index.UserIndexDefinition; 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; @@ -524,6 +525,7 @@ public class PlatformLevel4 extends PlatformLevel { CurrentAction.class, org.sonar.server.user.ws.SearchAction.class, org.sonar.server.user.ws.GroupsAction.class, + IdentityProvidersAction.class, FavoritesWs.class, UserPropertiesWs.class, UserIndexDefinition.class, diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/IdentityProvidersAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/IdentityProvidersAction.java new file mode 100644 index 00000000000..48edb2397d8 --- /dev/null +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/IdentityProvidersAction.java @@ -0,0 +1,81 @@ +/* + * 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 { + 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(); + } + } +} diff --git a/server/sonar-server/src/main/resources/org/sonar/server/user/ws/identity_providers-example.json b/server/sonar-server/src/main/resources/org/sonar/server/user/ws/identity_providers-example.json new file mode 100644 index 00000000000..06cb4310c0a --- /dev/null +++ b/server/sonar-server/src/main/resources/org/sonar/server/user/ws/identity_providers-example.json @@ -0,0 +1,16 @@ +{ + "identityProviders": [ + { + "key": "bitbucket", + "name": "Bitbucket", + "iconPath": "/static/authbitbucket/bitbucket.svg", + "backgroundColor": "#205081" + }, + { + "key": "github", + "name": "Github", + "iconPath": "/static/authgithub/github.svg", + "backgroundColor": "#444444" + } + ] +} diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/IdentityProvidersActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/IdentityProvidersActionTest.java new file mode 100644 index 00000000000..54b8d1e642d --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/IdentityProvidersActionTest.java @@ -0,0 +1,82 @@ +/* + * 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(); + + } +} diff --git a/sonar-ws/src/main/protobuf/ws-users.proto b/sonar-ws/src/main/protobuf/ws-users.proto new file mode 100644 index 00000000000..20b6fff0d77 --- /dev/null +++ b/sonar-ws/src/main/protobuf/ws-users.proto @@ -0,0 +1,37 @@ +// 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; +} -- 2.39.5