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.

WebUserRealm.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (c) 2006-2014 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.addons.ui_web;
  23. import com.dmdirc.interfaces.config.IdentityController;
  24. import java.math.BigInteger;
  25. import java.security.MessageDigest;
  26. import java.security.NoSuchAlgorithmException;
  27. import java.security.Principal;
  28. import java.util.ArrayList;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;
  32. import org.mortbay.jetty.Request;
  33. import org.mortbay.jetty.security.UserRealm;
  34. /**
  35. * Describes the users allowed to access the web UI.
  36. */
  37. public class WebUserRealm implements UserRealm {
  38. /** A map of known principals. */
  39. private final Map<String, Principal> principals = new HashMap<>();
  40. /** The config source to retrieve user information from. */
  41. private final IdentityController identityController;
  42. /** The domain to use when retrieving configuration. */
  43. private final String domain;
  44. public WebUserRealm(final IdentityController identityController, final String domain) {
  45. this.identityController = identityController;
  46. this.domain = domain;
  47. }
  48. /** {@inheritDoc} */
  49. @Override
  50. public String getName() {
  51. if (identityController.getGlobalConfiguration().hasOptionString(domain, "users")) {
  52. return "DMDirc web UI";
  53. } else {
  54. return "DMDirc web UI first run -- "
  55. + "enter the username and password you wish to use in "
  56. + "the future";
  57. }
  58. }
  59. /** {@inheritDoc} */
  60. @Override
  61. public Principal getPrincipal(final String username) {
  62. return principals.get(username);
  63. }
  64. /** {@inheritDoc} */
  65. @Override
  66. public Principal authenticate(final String username,
  67. final Object credentials, final Request request) {
  68. if (!identityController.getGlobalConfiguration().hasOptionString(domain, "users")) {
  69. final List<String> users = new ArrayList<>();
  70. users.add(username + ":" + getHash(username, credentials));
  71. identityController.getUserSettings().setOption(domain, "users", users);
  72. }
  73. for (String userinfo : identityController.getGlobalConfiguration().getOptionList(domain,
  74. "users")) {
  75. if (userinfo.startsWith(username + ":")) {
  76. final String pass = userinfo.substring(username.length() + 1);
  77. if (pass.equals(getHash(username, credentials))) {
  78. principals.put(username, new WebPrincipal(username));
  79. return getPrincipal(username);
  80. }
  81. }
  82. }
  83. return null;
  84. }
  85. /** {@inheritDoc} */
  86. @Override
  87. public boolean reauthenticate(final Principal user) {
  88. return principals.containsValue(user);
  89. }
  90. /** {@inheritDoc} */
  91. @Override
  92. public boolean isUserInRole(final Principal user, final String role) {
  93. return true;
  94. }
  95. /** {@inheritDoc} */
  96. @Override
  97. public void disassociate(final Principal user) {
  98. // Do nothing
  99. }
  100. /** {@inheritDoc} */
  101. @Override
  102. public Principal pushRole(final Principal user, final String role) {
  103. // Do nothing
  104. return user;
  105. }
  106. /** {@inheritDoc} */
  107. @Override
  108. public Principal popRole(final Principal user) {
  109. // Do nothing
  110. return user;
  111. }
  112. /** {@inheritDoc} */
  113. @Override
  114. public void logout(final Principal user) {
  115. principals.remove(user.getName());
  116. }
  117. private String getHash(final String username, final Object credentials) {
  118. final String target = username + "--" + (String) credentials;
  119. try {
  120. final MessageDigest md = MessageDigest.getInstance("SHA-512");
  121. return new BigInteger(md.digest(target.getBytes())).toString(16);
  122. } catch (NoSuchAlgorithmException ex) {
  123. // Don't hash
  124. return target;
  125. }
  126. }
  127. }