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.

SamlSettings.java 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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.saml;
  21. import java.util.Arrays;
  22. import java.util.List;
  23. import java.util.Optional;
  24. import org.sonar.api.config.Configuration;
  25. import org.sonar.api.config.PropertyDefinition;
  26. import org.sonar.api.server.ServerSide;
  27. import static java.lang.String.valueOf;
  28. import static org.sonar.api.PropertyType.BOOLEAN;
  29. import static org.sonar.api.PropertyType.PASSWORD;
  30. @ServerSide
  31. public class SamlSettings {
  32. public static final String ENABLED = "sonar.auth.saml.enabled";
  33. public static final String PROVIDER_ID = "sonar.auth.saml.providerId";
  34. public static final String PROVIDER_NAME = "sonar.auth.saml.providerName";
  35. public static final String APPLICATION_ID = "sonar.auth.saml.applicationId";
  36. public static final String LOGIN_URL = "sonar.auth.saml.loginUrl";
  37. public static final String CERTIFICATE = "sonar.auth.saml.certificate.secured";
  38. public static final String USER_LOGIN_ATTRIBUTE = "sonar.auth.saml.user.login";
  39. public static final String USER_NAME_ATTRIBUTE = "sonar.auth.saml.user.name";
  40. public static final String USER_EMAIL_ATTRIBUTE = "sonar.auth.saml.user.email";
  41. public static final String GROUP_NAME_ATTRIBUTE = "sonar.auth.saml.group.name";
  42. public static final String SIGN_REQUESTS_ENABLED = "sonar.auth.saml.signature.enabled";
  43. public static final String SERVICE_PROVIDER_CERTIFICATE = "sonar.auth.saml.sp.certificate.secured";
  44. public static final String SERVICE_PROVIDER_PRIVATE_KEY = "sonar.auth.saml.sp.privateKey.secured";
  45. public static final String CATEGORY = "authentication";
  46. public static final String SUBCATEGORY = "saml";
  47. private final Configuration configuration;
  48. public SamlSettings(Configuration configuration) {
  49. this.configuration = configuration;
  50. }
  51. String getProviderId() {
  52. return configuration.get(PROVIDER_ID).orElseThrow(() -> new IllegalArgumentException("Provider ID is missing"));
  53. }
  54. String getProviderName() {
  55. return configuration.get(PROVIDER_NAME).orElseThrow(() -> new IllegalArgumentException("Provider Name is missing"));
  56. }
  57. String getApplicationId() {
  58. return configuration.get(APPLICATION_ID).orElseThrow(() -> new IllegalArgumentException("Application ID is missing"));
  59. }
  60. String getLoginUrl() {
  61. return configuration.get(LOGIN_URL).orElseThrow(() -> new IllegalArgumentException("Login URL is missing"));
  62. }
  63. String getCertificate() {
  64. return configuration.get(CERTIFICATE).orElseThrow(() -> new IllegalArgumentException("Identity provider certificate is missing"));
  65. }
  66. String getUserLogin() {
  67. return configuration.get(USER_LOGIN_ATTRIBUTE).orElseThrow(() -> new IllegalArgumentException("User login attribute is missing"));
  68. }
  69. String getUserName() {
  70. return configuration.get(USER_NAME_ATTRIBUTE).orElseThrow(() -> new IllegalArgumentException("User name attribute is missing"));
  71. }
  72. boolean isSignRequestsEnabled() {
  73. return configuration.getBoolean(SIGN_REQUESTS_ENABLED).orElse(false);
  74. }
  75. Optional<String> getServiceProviderPrivateKey() {
  76. return configuration.get(SERVICE_PROVIDER_PRIVATE_KEY);
  77. }
  78. String getServiceProviderCertificate() {
  79. return configuration.get(SERVICE_PROVIDER_CERTIFICATE).orElseThrow(() -> new IllegalArgumentException("Service provider certificate is missing"));
  80. }
  81. Optional<String> getUserEmail() {
  82. return configuration.get(USER_EMAIL_ATTRIBUTE);
  83. }
  84. Optional<String> getGroupName() {
  85. return configuration.get(GROUP_NAME_ATTRIBUTE);
  86. }
  87. boolean isEnabled() {
  88. return configuration.getBoolean(ENABLED).orElse(false) &&
  89. configuration.get(PROVIDER_ID).isPresent() &&
  90. configuration.get(APPLICATION_ID).isPresent() &&
  91. configuration.get(LOGIN_URL).isPresent() &&
  92. configuration.get(CERTIFICATE).isPresent() &&
  93. configuration.get(USER_LOGIN_ATTRIBUTE).isPresent() &&
  94. configuration.get(USER_NAME_ATTRIBUTE).isPresent();
  95. }
  96. static List<PropertyDefinition> definitions() {
  97. return Arrays.asList(
  98. PropertyDefinition.builder(ENABLED)
  99. .name("Enabled")
  100. .description("Enable SAML users to login. Value is ignored if provider ID, login url, certificate, login, name attributes are not defined.")
  101. .category(CATEGORY)
  102. .subCategory(SUBCATEGORY)
  103. .type(BOOLEAN)
  104. .defaultValue(valueOf(false))
  105. .index(1)
  106. .build(),
  107. PropertyDefinition.builder(APPLICATION_ID)
  108. .name("Application ID")
  109. .description("Identifier of the application.")
  110. .defaultValue("sonarqube")
  111. .category(CATEGORY)
  112. .subCategory(SUBCATEGORY)
  113. .index(2)
  114. .build(),
  115. PropertyDefinition.builder(PROVIDER_NAME)
  116. .name("Provider Name")
  117. .description("Name displayed for the provider in the login page.")
  118. .defaultValue("SAML")
  119. .category(CATEGORY)
  120. .subCategory(SUBCATEGORY)
  121. .index(3)
  122. .build(),
  123. PropertyDefinition.builder(PROVIDER_ID)
  124. .name("Provider ID")
  125. .description("Identifier of the identity provider, the entity that provides SAML authentication.")
  126. .category(CATEGORY)
  127. .subCategory(SUBCATEGORY)
  128. .index(4)
  129. .build(),
  130. PropertyDefinition.builder(LOGIN_URL)
  131. .name("SAML login url")
  132. .description("SAML login URL for the identity provider.")
  133. .category(CATEGORY)
  134. .subCategory(SUBCATEGORY)
  135. .index(5)
  136. .build(),
  137. PropertyDefinition.builder(CERTIFICATE)
  138. .name("Identity provider certificate")
  139. .description("X.509 certificate for the identity provider.")
  140. .category(CATEGORY)
  141. .subCategory(SUBCATEGORY)
  142. .type(PASSWORD)
  143. .index(6)
  144. .build(),
  145. PropertyDefinition.builder(USER_LOGIN_ATTRIBUTE)
  146. .name("SAML user login attribute")
  147. .description("Attribute defining the user login in SAML.")
  148. .category(CATEGORY)
  149. .subCategory(SUBCATEGORY)
  150. .index(7)
  151. .build(),
  152. PropertyDefinition.builder(USER_NAME_ATTRIBUTE)
  153. .name("SAML user name attribute")
  154. .description("Attribute defining the user name in SAML.")
  155. .category(CATEGORY)
  156. .subCategory(SUBCATEGORY)
  157. .index(8)
  158. .build(),
  159. PropertyDefinition.builder(USER_EMAIL_ATTRIBUTE)
  160. .name("SAML user email attribute")
  161. .description("Attribute defining the user email in SAML.")
  162. .category(CATEGORY)
  163. .subCategory(SUBCATEGORY)
  164. .index(9)
  165. .build(),
  166. PropertyDefinition.builder(GROUP_NAME_ATTRIBUTE)
  167. .name("SAML group attribute")
  168. .description("Attribute defining the user groups in SAML. " +
  169. "Users are associated to the default group only if no attribute is defined.")
  170. .category(CATEGORY)
  171. .subCategory(SUBCATEGORY)
  172. .index(10)
  173. .build(),
  174. PropertyDefinition.builder(SIGN_REQUESTS_ENABLED)
  175. .name("Sign requests")
  176. .description("Enables signature of SAML requests. It requires both service provider private key and certificate to be set.")
  177. .category(CATEGORY)
  178. .subCategory(SUBCATEGORY)
  179. .type(BOOLEAN)
  180. .defaultValue(valueOf(false))
  181. .index(11)
  182. .build(),
  183. PropertyDefinition.builder(SERVICE_PROVIDER_PRIVATE_KEY)
  184. .name("Service provider private key")
  185. .description("PKCS8 stored private key used for signing the requests and decrypting responses from the identity provider. ")
  186. .category(CATEGORY)
  187. .subCategory(SUBCATEGORY)
  188. .type(PASSWORD)
  189. .index(12)
  190. .build(),
  191. PropertyDefinition.builder(SERVICE_PROVIDER_CERTIFICATE)
  192. .name("Service provider certificate")
  193. .description("X.509 certificate for the service provider, used for signing the requests.")
  194. .category(CATEGORY)
  195. .subCategory(SUBCATEGORY)
  196. .type(PASSWORD)
  197. .index(13)
  198. .build());
  199. }
  200. }