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 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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. @ServerSide
  30. public class SamlSettings {
  31. private static final String ENABLED = "sonar.auth.saml.enabled";
  32. private static final String PROVIDER_ID = "sonar.auth.saml.providerId";
  33. private static final String PROVIDER_NAME = "sonar.auth.saml.providerName";
  34. private static final String APPLICATION_ID = "sonar.auth.saml.applicationId";
  35. private static final String LOGIN_URL = "sonar.auth.saml.loginUrl";
  36. private static final String CERTIFICATE = "sonar.auth.saml.certificate.secured";
  37. private static final String USER_LOGIN_ATTRIBUTE = "sonar.auth.saml.user.login";
  38. private static final String USER_NAME_ATTRIBUTE = "sonar.auth.saml.user.name";
  39. private static final String USER_EMAIL_ATTRIBUTE = "sonar.auth.saml.user.email";
  40. private static final String GROUP_NAME_ATTRIBUTE = "sonar.auth.saml.group.name";
  41. private static final String CATEGORY = "security";
  42. private static final String SUBCATEGORY = "saml";
  43. private final Configuration configuration;
  44. public SamlSettings(Configuration configuration) {
  45. this.configuration = configuration;
  46. }
  47. String getProviderId() {
  48. return configuration.get(PROVIDER_ID).orElseThrow(() -> new IllegalArgumentException("Provider ID is missing"));
  49. }
  50. String getProviderName() {
  51. return configuration.get(PROVIDER_NAME).orElseThrow(() -> new IllegalArgumentException("Provider Name is missing"));
  52. }
  53. String getApplicationId() {
  54. return configuration.get(APPLICATION_ID).orElseThrow(() -> new IllegalArgumentException("Application ID is missing"));
  55. }
  56. String getLoginUrl() {
  57. return configuration.get(LOGIN_URL).orElseThrow(() -> new IllegalArgumentException("Login URL is missing"));
  58. }
  59. String getCertificate() {
  60. return configuration.get(CERTIFICATE).orElseThrow(() -> new IllegalArgumentException("Certificate is missing"));
  61. }
  62. String getUserLogin() {
  63. return configuration.get(USER_LOGIN_ATTRIBUTE).orElseThrow(() -> new IllegalArgumentException("User login attribute is missing"));
  64. }
  65. String getUserName() {
  66. return configuration.get(USER_NAME_ATTRIBUTE).orElseThrow(() -> new IllegalArgumentException("User name attribute is missing"));
  67. }
  68. Optional<String> getUserEmail() {
  69. return configuration.get(USER_EMAIL_ATTRIBUTE);
  70. }
  71. Optional<String> getGroupName() {
  72. return configuration.get(GROUP_NAME_ATTRIBUTE);
  73. }
  74. boolean isEnabled() {
  75. return configuration.getBoolean(ENABLED).orElse(false) &&
  76. configuration.get(PROVIDER_ID).isPresent() &&
  77. configuration.get(APPLICATION_ID).isPresent() &&
  78. configuration.get(LOGIN_URL).isPresent() &&
  79. configuration.get(CERTIFICATE).isPresent() &&
  80. configuration.get(USER_LOGIN_ATTRIBUTE).isPresent() &&
  81. configuration.get(USER_NAME_ATTRIBUTE).isPresent();
  82. }
  83. static List<PropertyDefinition> definitions() {
  84. return Arrays.asList(
  85. PropertyDefinition.builder(ENABLED)
  86. .name("Enabled")
  87. .description("Enable SAML users to login. Value is ignored if provider ID, login url, certificate, login, name attributes are not defined.")
  88. .category(CATEGORY)
  89. .subCategory(SUBCATEGORY)
  90. .type(BOOLEAN)
  91. .defaultValue(valueOf(false))
  92. .index(1)
  93. .build(),
  94. PropertyDefinition.builder(APPLICATION_ID)
  95. .name("Application ID")
  96. .description("Identifier of the application.")
  97. .defaultValue("sonarqube")
  98. .category(CATEGORY)
  99. .subCategory(SUBCATEGORY)
  100. .index(2)
  101. .build(),
  102. PropertyDefinition.builder(PROVIDER_NAME)
  103. .name("Provider Name")
  104. .description("Name displayed for the provider in the login page.")
  105. .defaultValue("SAML")
  106. .category(CATEGORY)
  107. .subCategory(SUBCATEGORY)
  108. .index(3)
  109. .build(),
  110. PropertyDefinition.builder(PROVIDER_ID)
  111. .name("Provider ID")
  112. .description("Identifier of the identity provider, the entity that provides SAML authentication.")
  113. .category(CATEGORY)
  114. .subCategory(SUBCATEGORY)
  115. .index(4)
  116. .build(),
  117. PropertyDefinition.builder(LOGIN_URL)
  118. .name("SAML login url")
  119. .description("SAML login URL for the identity provider.")
  120. .category(CATEGORY)
  121. .subCategory(SUBCATEGORY)
  122. .index(5)
  123. .build(),
  124. PropertyDefinition.builder(CERTIFICATE)
  125. .name("Provider certificate")
  126. .description("X.509 certificate for the identity provider.")
  127. .category(CATEGORY)
  128. .subCategory(SUBCATEGORY)
  129. .index(6)
  130. .build(),
  131. PropertyDefinition.builder(USER_LOGIN_ATTRIBUTE)
  132. .name("SAML user login attribute")
  133. .description("Attribute defining the user login in SAML.")
  134. .category(CATEGORY)
  135. .subCategory(SUBCATEGORY)
  136. .index(7)
  137. .build(),
  138. PropertyDefinition.builder(USER_NAME_ATTRIBUTE)
  139. .name("SAML user name attribute")
  140. .description("Attribute defining the user name in SAML.")
  141. .category(CATEGORY)
  142. .subCategory(SUBCATEGORY)
  143. .index(8)
  144. .build(),
  145. PropertyDefinition.builder(USER_EMAIL_ATTRIBUTE)
  146. .name("SAML user email attribute")
  147. .description("Attribute defining the user email in SAML.")
  148. .category(CATEGORY)
  149. .subCategory(SUBCATEGORY)
  150. .index(9)
  151. .build(),
  152. PropertyDefinition.builder(GROUP_NAME_ATTRIBUTE)
  153. .name("SAML group attribute")
  154. .description("Attribute defining the user groups in SAML. " +
  155. "Users are associated to the default group only if no attribute is defined.")
  156. .category(CATEGORY)
  157. .subCategory(SUBCATEGORY)
  158. .index(10)
  159. .build());
  160. }
  161. }