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.

GithubAppConfiguration.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 com.google.common.base.MoreObjects;
  22. import java.util.regex.Pattern;
  23. import javax.annotation.CheckForNull;
  24. import javax.annotation.Nullable;
  25. import static java.lang.String.format;
  26. public class GithubAppConfiguration {
  27. private static final Pattern TRAILING_SLASHES = Pattern.compile("/+$");
  28. private final Long id;
  29. private final String privateKey;
  30. private final String apiEndpoint;
  31. public GithubAppConfiguration(@Nullable Long id, @Nullable String privateKey, @Nullable String apiEndpoint) {
  32. this.id = id;
  33. this.privateKey = privateKey;
  34. this.apiEndpoint = sanitizedEndPoint(apiEndpoint);
  35. }
  36. /**
  37. * Check configuration is complete with {@link #isComplete()} before calling this method.
  38. *
  39. * @throws IllegalStateException if configuration is not complete
  40. */
  41. public long getId() {
  42. checkConfigurationComplete();
  43. return id;
  44. }
  45. public String getApiEndpoint() {
  46. checkConfigurationComplete();
  47. return apiEndpoint;
  48. }
  49. /**
  50. * Check configuration is complete with {@link #isComplete()} before calling this method.
  51. *
  52. * @throws IllegalStateException if configuration is not complete
  53. */
  54. public String getPrivateKey() {
  55. checkConfigurationComplete();
  56. return privateKey;
  57. }
  58. private void checkConfigurationComplete() {
  59. if (!isComplete()) {
  60. throw new IllegalStateException(format("Configuration is not complete : %s", toString()));
  61. }
  62. }
  63. public boolean isComplete() {
  64. return id != null &&
  65. privateKey != null &&
  66. apiEndpoint != null;
  67. }
  68. @Override
  69. public String toString() {
  70. return MoreObjects.toStringHelper(GithubAppConfiguration.class)
  71. .add("id", id)
  72. .add("privateKey", secureToString(privateKey))
  73. .add("apiEndpoint", toString(apiEndpoint))
  74. .toString();
  75. }
  76. @CheckForNull
  77. private static String toString(@Nullable String s) {
  78. if (s == null) {
  79. return null;
  80. }
  81. return '\'' + s + '\'';
  82. }
  83. @CheckForNull
  84. private static String secureToString(@Nullable String token) {
  85. if (token == null) {
  86. return null;
  87. }
  88. return "'***(" + token.length() + ")***'";
  89. }
  90. private static String sanitizedEndPoint(@Nullable String endPoint) {
  91. if (endPoint == null) {
  92. return null;
  93. }
  94. return TRAILING_SLASHES.matcher(endPoint).replaceAll("");
  95. }
  96. }