/testsuite/integration/src/test/java/org/jboss/as/testsuite/integration/security/CustomTestLoginModule.java

https://github.com/samuelo/jboss-as · Java · 137 lines · 94 code · 16 blank · 27 comment · 10 complexity · 21e58eeca24e0fdc3829a197a92c2688 MD5 · raw file

  1. /*
  2. * JBoss, Home of Professional Open Source.
  3. * Copyright 2011, Red Hat, Inc., and individual contributors
  4. * as indicated by the @author tags. See the copyright.txt file in the
  5. * distribution for a full listing of individual contributors.
  6. *
  7. * This is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as
  9. * published by the Free Software Foundation; either version 2.1 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This software is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this software; if not, write to the Free
  19. * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
  20. * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
  21. */
  22. package org.jboss.as.testsuite.integration.security;
  23. import java.io.IOException;
  24. import java.security.Principal;
  25. import java.security.acl.Group;
  26. import java.util.Map;
  27. import java.util.Set;
  28. import javax.security.auth.Subject;
  29. import javax.security.auth.callback.Callback;
  30. import javax.security.auth.callback.CallbackHandler;
  31. import javax.security.auth.callback.NameCallback;
  32. import javax.security.auth.callback.PasswordCallback;
  33. import javax.security.auth.callback.UnsupportedCallbackException;
  34. import javax.security.auth.login.LoginException;
  35. import javax.security.auth.spi.LoginModule;
  36. import org.jboss.security.SimpleGroup;
  37. import org.jboss.security.SimplePrincipal;
  38. /**
  39. * Simple custom login module.
  40. *
  41. * @author <a href="mailto:mmoyses@redhat.com">Marcus Moyses</a>
  42. */
  43. public class CustomTestLoginModule implements LoginModule {
  44. private Subject subject;
  45. private CallbackHandler callbackHandler;
  46. private String username;
  47. @Override
  48. public void initialize(Subject subject, CallbackHandler callbackHandler, Map<String, ?> sharedState, Map<String, ?> options) {
  49. this.subject = subject;
  50. this.callbackHandler = callbackHandler;
  51. }
  52. @Override
  53. public boolean login() throws LoginException {
  54. String[] s = getUsernameAndPassword();
  55. username = s[0];
  56. String password = s[1];
  57. if (username.equals("anil")) {
  58. if (password.equals("anil"))
  59. return true;
  60. }
  61. if (username.equals("marcus")) {
  62. if (password.equals("marcus"))
  63. return true;
  64. }
  65. return false;
  66. }
  67. @Override
  68. public boolean commit() throws LoginException {
  69. Set<Principal> principals = subject.getPrincipals();
  70. Group callerPrincipal = new SimpleGroup("CallerPrincipal");
  71. callerPrincipal.addMember(new SimplePrincipal(username));
  72. principals.add(callerPrincipal);
  73. Group roles = new SimpleGroup("Roles");
  74. if (username.equals("anil"))
  75. roles.addMember(new SimplePrincipal("gooduser"));
  76. if (username.equals("marcus"))
  77. roles.addMember(new SimplePrincipal("superuser"));
  78. principals.add(roles);
  79. return true;
  80. }
  81. @Override
  82. public boolean abort() throws LoginException {
  83. return true;
  84. }
  85. @Override
  86. public boolean logout() throws LoginException {
  87. return true;
  88. }
  89. protected String[] getUsernameAndPassword() throws LoginException {
  90. String[] info = { null, null };
  91. // prompt for a username and password
  92. if (callbackHandler == null) {
  93. throw new LoginException("Error: no CallbackHandler available " + "to collect authentication information");
  94. }
  95. NameCallback nc = new NameCallback("User name: ", "guest");
  96. PasswordCallback pc = new PasswordCallback("Password: ", false);
  97. Callback[] callbacks = { nc, pc };
  98. String username = null;
  99. String password = null;
  100. try {
  101. callbackHandler.handle(callbacks);
  102. username = nc.getName();
  103. char[] tmpPassword = pc.getPassword();
  104. if (tmpPassword != null) {
  105. pc.clearPassword();
  106. password = new String(tmpPassword);
  107. }
  108. } catch (IOException e) {
  109. LoginException le = new LoginException("Failed to get username/password");
  110. le.initCause(e);
  111. throw le;
  112. } catch (UnsupportedCallbackException e) {
  113. LoginException le = new LoginException("CallbackHandler does not support: " + e.getCallback());
  114. le.initCause(e);
  115. throw le;
  116. }
  117. info[0] = username;
  118. info[1] = password;
  119. return info;
  120. }
  121. }