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.

Credentials.java 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.server.authentication;
  21. import java.util.Objects;
  22. import java.util.Optional;
  23. import javax.annotation.Nullable;
  24. import javax.annotation.concurrent.Immutable;
  25. import static com.google.common.base.Preconditions.checkArgument;
  26. import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
  27. @Immutable
  28. public class Credentials {
  29. private final String login;
  30. private final String password;
  31. public Credentials(String login, @Nullable String password) {
  32. checkArgument(login != null && !login.isEmpty(), "login must not be null nor empty");
  33. this.login = login;
  34. this.password = defaultIfEmpty(password, null);
  35. }
  36. /**
  37. * Non-empty login
  38. */
  39. public String getLogin() {
  40. return login;
  41. }
  42. /**
  43. * Non-empty password. {@code Optional.empty()} is returned if the password is not set
  44. * or initially empty. {@code Optional.of("")} is never returned.
  45. */
  46. public Optional<String> getPassword() {
  47. return Optional.ofNullable(password);
  48. }
  49. @Override
  50. public boolean equals(Object o) {
  51. if (this == o) {
  52. return true;
  53. }
  54. if (o == null || getClass() != o.getClass()) {
  55. return false;
  56. }
  57. Credentials that = (Credentials) o;
  58. return login.equals(that.login) && password.equals(that.password);
  59. }
  60. @Override
  61. public int hashCode() {
  62. return Objects.hash(login, password);
  63. }
  64. }