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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (c) 2006-2011 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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.config.ConfigManager;
  24. import com.dmdirc.config.IdentityManager;
  25. import java.math.BigInteger;
  26. import java.security.MessageDigest;
  27. import java.security.NoSuchAlgorithmException;
  28. import java.security.Principal;
  29. import java.util.ArrayList;
  30. import java.util.HashMap;
  31. import java.util.List;
  32. import java.util.Map;
  33. import org.mortbay.jetty.Request;
  34. import org.mortbay.jetty.security.UserRealm;
  35. /**
  36. * Describes the users allowed to access the web UI.
  37. *
  38. * @author chris
  39. */
  40. public class WebUserRealm implements UserRealm {
  41. private final Map<String, Principal> principals
  42. = new HashMap<String, Principal>();
  43. private final ConfigManager config = IdentityManager.getGlobalConfig();
  44. /** {@inheritDoc} */
  45. @Override
  46. public String getName() {
  47. if (config.hasOptionString(WebInterfaceUI.DOMAIN, "users")) {
  48. return "DMDirc web UI";
  49. } else {
  50. return "DMDirc web UI first run -- "
  51. + "enter the username and password you wish to use in "
  52. + "the future";
  53. }
  54. }
  55. /** {@inheritDoc} */
  56. @Override
  57. public Principal getPrincipal(final String username) {
  58. return principals.get(username);
  59. }
  60. /** {@inheritDoc} */
  61. @Override
  62. public Principal authenticate(final String username,
  63. final Object credentials, final Request request) {
  64. if (!config.hasOptionString(WebInterfaceUI.DOMAIN, "users")) {
  65. final List<String> users = new ArrayList<String>();
  66. users.add(username + ":" + getHash(username, credentials));
  67. IdentityManager.getConfigIdentity().setOption(WebInterfaceUI.DOMAIN,
  68. "users", users);
  69. }
  70. for (String userinfo : config.getOptionList(WebInterfaceUI.DOMAIN,
  71. "users")) {
  72. if (userinfo.startsWith(username + ":")) {
  73. final String pass = userinfo.substring(username.length() + 1);
  74. if (pass.equals(getHash(username, credentials))) {
  75. principals.put(username, new WebPrincipal(username));
  76. return getPrincipal(username);
  77. }
  78. }
  79. }
  80. return null;
  81. }
  82. /** {@inheritDoc} */
  83. @Override
  84. public boolean reauthenticate(final Principal user) {
  85. return principals.containsValue(user);
  86. }
  87. /** {@inheritDoc} */
  88. @Override
  89. public boolean isUserInRole(final Principal user, final String role) {
  90. return true;
  91. }
  92. /** {@inheritDoc} */
  93. @Override
  94. public void disassociate(final Principal user) {
  95. // Do nothing
  96. }
  97. /** {@inheritDoc} */
  98. @Override
  99. public Principal pushRole(final Principal user, final String role) {
  100. // Do nothing
  101. return user;
  102. }
  103. /** {@inheritDoc} */
  104. @Override
  105. public Principal popRole(final Principal user) {
  106. // Do nothing
  107. return user;
  108. }
  109. /** {@inheritDoc} */
  110. @Override
  111. public void logout(final Principal user) {
  112. principals.remove(user.getName());
  113. }
  114. private String getHash(final String username, final Object credentials) {
  115. final String target = username + "--" + (String) credentials;
  116. try {
  117. final MessageDigest md = MessageDigest.getInstance("SHA-512");
  118. return new BigInteger(md.digest(target.getBytes())).toString(16);
  119. } catch (NoSuchAlgorithmException ex) {
  120. // Don't hash
  121. return target;
  122. }
  123. }
  124. }