/AuthenticRoast/AuthenticRoast-Samples/src/main/java/name/aikesommer/authenticator/samples/FormAndTicketSample.java

http://authenticroast.googlecode.com/ · Java · 91 lines · 39 code · 15 blank · 37 comment · 0 complexity · 611ab78cdb69ec27907b163dfa94b369 MD5 · raw file

  1. /**
  2. * Copyright (C) 2007-2010 Aike J Sommer (http://aikesommer.name/)
  3. *
  4. * This file is part of AuthenticRoast.
  5. *
  6. * This file is just a sample. You are free to use and modify the
  7. * code in this file to your liking!
  8. *
  9. * This file is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. *
  13. * You can reach the author and get more information about this
  14. * project at: http://aikesommer.name/
  15. */
  16. package name.aikesommer.authenticator.samples;
  17. import java.util.ArrayList;
  18. import java.util.Collection;
  19. import java.util.List;
  20. import name.aikesommer.authenticator.AuthenticationRequest;
  21. import name.aikesommer.authenticator.CompositeAuthenticator;
  22. import name.aikesommer.authenticator.FormAuthenticator;
  23. import name.aikesommer.authenticator.LogoutManager;
  24. import name.aikesommer.authenticator.PluggableAuthenticator;
  25. import name.aikesommer.authenticator.SimplePrincipal;
  26. import name.aikesommer.authenticator.TicketAuthenticator;
  27. /**
  28. * This is just a sample, make sure to actually check credentials and such!!
  29. *
  30. * @author Aike J Sommer
  31. */
  32. public class FormAndTicketSample extends CompositeAuthenticator {
  33. @Override
  34. protected Collection<PluggableAuthenticator> createAuthenticators() {
  35. List<PluggableAuthenticator> result = new ArrayList();
  36. /**
  37. * Allow form-based logins.
  38. */
  39. result.add(new FormAuthenticator() {
  40. @Override
  41. protected boolean checkCredentials(AuthenticationManager manager, AuthenticationRequest request, String username, String password) {
  42. // check the credentials with some config-files, db-data
  43. // or a realm in the app-server
  44. // we just return true here, so everything will be accepted
  45. return true;
  46. }
  47. @Override
  48. protected SimplePrincipal loadPrincipal(AuthenticationManager manager, AuthenticationRequest request, String username) {
  49. // load user-data from config-files, db or where ever you
  50. // have it stored :-)
  51. return new SimplePrincipal(username, "user");
  52. }
  53. });
  54. /**
  55. * Allow ticket-based logins.
  56. */
  57. result.add(new TicketAuthenticator() {
  58. @Override
  59. protected boolean checkTicket(AuthenticationManager manager, AuthenticationRequest request, String ticket) {
  60. // check the ticket, for example with some secret and a
  61. // hash
  62. // we just return true here, so everything will be accepted
  63. return true;
  64. }
  65. @Override
  66. protected SimplePrincipal loadPrincipal(AuthenticationManager manager, AuthenticationRequest request, String ticket) {
  67. return new SimplePrincipal("guest", "guest");
  68. }
  69. });
  70. /**
  71. * Allow a user to "logout".
  72. */
  73. result.add(new LogoutManager());
  74. return result;
  75. }
  76. }