Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.auth.github;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import javax.annotation.CheckForNull;
  24. import javax.annotation.Nullable;
  25. import org.sonar.api.config.Configuration;
  26. import org.sonar.api.config.PropertyDefinition;
  27. import static java.lang.String.valueOf;
  28. import static org.sonar.api.PropertyType.BOOLEAN;
  29. import static org.sonar.api.PropertyType.PASSWORD;
  30. import static org.sonar.api.PropertyType.STRING;
  31. public class GitHubSettings {
  32. public static final String CLIENT_ID = "sonar.auth.github.clientId.secured";
  33. public static final String CLIENT_SECRET = "sonar.auth.github.clientSecret.secured";
  34. public static final String ENABLED = "sonar.auth.github.enabled";
  35. public static final String ALLOW_USERS_TO_SIGN_UP = "sonar.auth.github.allowUsersToSignUp";
  36. public static final String GROUPS_SYNC = "sonar.auth.github.groupsSync";
  37. public static final String API_URL = "sonar.auth.github.apiUrl";
  38. public static final String WEB_URL = "sonar.auth.github.webUrl";
  39. public static final String ORGANIZATIONS = "sonar.auth.github.organizations";
  40. private static final String CATEGORY = "authentication";
  41. private static final String SUBCATEGORY = "github";
  42. private final Configuration configuration;
  43. public GitHubSettings(Configuration configuration) {
  44. this.configuration = configuration;
  45. }
  46. String clientId() {
  47. return configuration.get(CLIENT_ID).orElse("");
  48. }
  49. String clientSecret() {
  50. return configuration.get(CLIENT_SECRET).orElse("");
  51. }
  52. boolean isEnabled() {
  53. return configuration.getBoolean(ENABLED).orElse(false) && !clientId().isEmpty() && !clientSecret().isEmpty();
  54. }
  55. boolean allowUsersToSignUp() {
  56. return configuration.getBoolean(ALLOW_USERS_TO_SIGN_UP).orElse(false);
  57. }
  58. boolean syncGroups() {
  59. return configuration.getBoolean(GROUPS_SYNC).orElse(false);
  60. }
  61. @CheckForNull
  62. String webURL() {
  63. return urlWithEndingSlash(configuration.get(WEB_URL).orElse(""));
  64. }
  65. @CheckForNull
  66. String apiURL() {
  67. return urlWithEndingSlash(configuration.get(API_URL).orElse(""));
  68. }
  69. String[] organizations() {
  70. return configuration.getStringArray(ORGANIZATIONS);
  71. }
  72. @CheckForNull
  73. private static String urlWithEndingSlash(@Nullable String url) {
  74. if (url != null && !url.endsWith("/")) {
  75. return url + "/";
  76. }
  77. return url;
  78. }
  79. public static List<PropertyDefinition> definitions() {
  80. return Arrays.asList(
  81. PropertyDefinition.builder(ENABLED)
  82. .name("Enabled")
  83. .description("Enable GitHub users to login. Value is ignored if client ID and secret are not defined.")
  84. .category(CATEGORY)
  85. .subCategory(SUBCATEGORY)
  86. .type(BOOLEAN)
  87. .defaultValue(valueOf(false))
  88. .index(1)
  89. .build(),
  90. PropertyDefinition.builder(CLIENT_ID)
  91. .name("Client ID")
  92. .description("Client ID provided by GitHub when registering the application.")
  93. .category(CATEGORY)
  94. .subCategory(SUBCATEGORY)
  95. .index(2)
  96. .build(),
  97. PropertyDefinition.builder(CLIENT_SECRET)
  98. .name("Client Secret")
  99. .description("Client password provided by GitHub when registering the application.")
  100. .category(CATEGORY)
  101. .subCategory(SUBCATEGORY)
  102. .type(PASSWORD)
  103. .index(3)
  104. .build(),
  105. PropertyDefinition.builder(ALLOW_USERS_TO_SIGN_UP)
  106. .name("Allow users to sign-up")
  107. .description("Allow new users to authenticate. When set to 'false', only existing users will be able to authenticate to the server.")
  108. .category(CATEGORY)
  109. .subCategory(SUBCATEGORY)
  110. .type(BOOLEAN)
  111. .defaultValue(valueOf(true))
  112. .index(4)
  113. .build(),
  114. PropertyDefinition.builder(GROUPS_SYNC)
  115. .name("Synchronize teams as groups")
  116. .description("For each team they belong to, the user will be associated to a group named 'Organisation/Team' (if it exists) in SonarQube.")
  117. .category(CATEGORY)
  118. .subCategory(SUBCATEGORY)
  119. .type(BOOLEAN)
  120. .defaultValue(valueOf(false))
  121. .index(6)
  122. .build(),
  123. PropertyDefinition.builder(API_URL)
  124. .name("The API url for a GitHub instance.")
  125. .description("The API url for a GitHub instance. https://api.github.com/ for Github.com, https://github.company.com/api/v3/ when using Github Enterprise")
  126. .category(CATEGORY)
  127. .subCategory(SUBCATEGORY)
  128. .type(STRING)
  129. .defaultValue("https://api.github.com/")
  130. .index(7)
  131. .build(),
  132. PropertyDefinition.builder(WEB_URL)
  133. .name("The WEB url for a GitHub instance.")
  134. .description("The WEB url for a GitHub instance. " +
  135. "https://github.com/ for Github.com, https://github.company.com/ when using GitHub Enterprise.")
  136. .category(CATEGORY)
  137. .subCategory(SUBCATEGORY)
  138. .type(STRING)
  139. .defaultValue("https://github.com/")
  140. .index(8)
  141. .build(),
  142. PropertyDefinition.builder(ORGANIZATIONS)
  143. .name("Organizations")
  144. .description("Only members of these organizations will be able to authenticate to the server. " +
  145. "If a user is a member of any of the organizations listed they will be authenticated.")
  146. .multiValues(true)
  147. .category(CATEGORY)
  148. .subCategory(SUBCATEGORY)
  149. .index(9)
  150. .build());
  151. }
  152. }