You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BitbucketSettings.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.bitbucket;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import java.util.function.Supplier;
  24. import javax.annotation.CheckForNull;
  25. import org.sonar.api.CoreProperties;
  26. import org.sonar.api.PropertyType;
  27. import org.sonar.api.config.Configuration;
  28. import org.sonar.api.config.PropertyDefinition;
  29. import org.sonar.api.server.ServerSide;
  30. @ServerSide
  31. public class BitbucketSettings {
  32. private static final Supplier<? extends IllegalStateException> DEFAULT_VALUE_MISSING = () -> new IllegalStateException("Should have a default value");
  33. public static final String CONSUMER_KEY = "sonar.auth.bitbucket.clientId.secured";
  34. public static final String CONSUMER_SECRET = "sonar.auth.bitbucket.clientSecret.secured";
  35. public static final String ENABLED = "sonar.auth.bitbucket.enabled";
  36. public static final String ALLOW_USERS_TO_SIGN_UP = "sonar.auth.bitbucket.allowUsersToSignUp";
  37. public static final String WORKSPACE_ALLOWED_LIST = "sonar.auth.bitbucket.workspaces";
  38. public static final String DEFAULT_API_URL = "https://api.bitbucket.org/";
  39. public static final String DEFAULT_WEB_URL = "https://bitbucket.org/";
  40. public static final String SUBCATEGORY = "bitbucket";
  41. private final Configuration config;
  42. public BitbucketSettings(Configuration config) {
  43. this.config = config;
  44. }
  45. @CheckForNull
  46. public String clientId() {
  47. return config.get(CONSUMER_KEY).orElse(null);
  48. }
  49. @CheckForNull
  50. public String clientSecret() {
  51. return config.get(CONSUMER_SECRET).orElse(null);
  52. }
  53. public boolean isEnabled() {
  54. return config.getBoolean(ENABLED).orElseThrow(DEFAULT_VALUE_MISSING) && clientId() != null && clientSecret() != null;
  55. }
  56. public boolean allowUsersToSignUp() {
  57. return config.getBoolean(ALLOW_USERS_TO_SIGN_UP).orElseThrow(DEFAULT_VALUE_MISSING);
  58. }
  59. public String[] workspaceAllowedList() {
  60. return config.getStringArray(WORKSPACE_ALLOWED_LIST);
  61. }
  62. public String webURL() {
  63. return DEFAULT_WEB_URL;
  64. }
  65. public String apiURL() {
  66. return DEFAULT_API_URL;
  67. }
  68. public static List<PropertyDefinition> definitions() {
  69. return Arrays.asList(
  70. PropertyDefinition.builder(ENABLED)
  71. .name("Enabled")
  72. .description("Enable Bitbucket users to login. Value is ignored if consumer key and secret are not defined.")
  73. .category(CoreProperties.CATEGORY_ALM_INTEGRATION)
  74. .subCategory(SUBCATEGORY)
  75. .type(PropertyType.BOOLEAN)
  76. .defaultValue(String.valueOf(false))
  77. .index(1)
  78. .build(),
  79. PropertyDefinition.builder(CONSUMER_KEY)
  80. .name("OAuth consumer key")
  81. .description("Consumer key provided by Bitbucket when registering the consumer.")
  82. .category(CoreProperties.CATEGORY_ALM_INTEGRATION)
  83. .subCategory(SUBCATEGORY)
  84. .index(2)
  85. .build(),
  86. PropertyDefinition.builder(CONSUMER_SECRET)
  87. .name("OAuth consumer secret")
  88. .description("Consumer secret provided by Bitbucket when registering the consumer.")
  89. .category(CoreProperties.CATEGORY_ALM_INTEGRATION)
  90. .subCategory(SUBCATEGORY)
  91. .index(3)
  92. .build(),
  93. PropertyDefinition.builder(ALLOW_USERS_TO_SIGN_UP)
  94. .name("Allow users to sign-up")
  95. .description("Allow new users to authenticate. When set to 'false', only existing users will be able to authenticate.")
  96. .category(CoreProperties.CATEGORY_ALM_INTEGRATION)
  97. .subCategory(SUBCATEGORY)
  98. .type(PropertyType.BOOLEAN)
  99. .defaultValue(String.valueOf(true))
  100. .index(4)
  101. .build(),
  102. PropertyDefinition.builder(WORKSPACE_ALLOWED_LIST)
  103. .name("Workspaces")
  104. .description("Only members of at least one of these workspace will be able to authenticate. Keep empty to disable workspace restriction. You can use either the workspace name, or the workspace slug (ex: https://bitbucket.org/{workspace-slug}).")
  105. .category(CoreProperties.CATEGORY_ALM_INTEGRATION)
  106. .subCategory(SUBCATEGORY)
  107. .multiValues(true)
  108. .index(5)
  109. .build()
  110. );
  111. }
  112. }