001 package org.crsh.auth;
002
003 import org.crsh.plugin.CRaSHPlugin;
004 import org.crsh.plugin.PluginContext;
005 import org.crsh.plugin.PropertyDescriptor;
006
007 import java.util.Arrays;
008
009 /**
010 * A simple authentication plugin based on user a username and password.
011 *
012 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a>
013 */
014 public class SimpleAuthenticationPlugin extends
015 CRaSHPlugin<AuthenticationPlugin> implements
016 AuthenticationPlugin {
017
018 /** The username. */
019 public static final PropertyDescriptor<String> SIMPLE_USERNAME =
020 PropertyDescriptor.create(
021 "auth.simple.username",
022 "admin",
023 "The username");
024
025 /** The password. */
026 public static final PropertyDescriptor<String> SIMPLE_PASSWORD =
027 PropertyDescriptor.create(
028 "auth.simple.password",
029 "admin",
030 "The password");
031
032 /** . */
033 private String username;
034
035 /** . */
036 private String password;
037
038 @Override
039 protected Iterable<PropertyDescriptor<?>> createConfigurationCapabilities() {
040 return Arrays.<PropertyDescriptor<?>>asList(
041 SIMPLE_USERNAME,
042 SIMPLE_PASSWORD);
043 }
044
045 @Override
046 public AuthenticationPlugin getImplementation() {
047 return this;
048 }
049
050 @Override
051 public void init() {
052 PluginContext context = getContext();
053 this.username = context.getProperty(SIMPLE_USERNAME);
054 this.password = context.getProperty(SIMPLE_PASSWORD);
055 }
056
057 public String getName() {
058 return "simple";
059 }
060
061 public boolean authenticate(String username, String password)
062 throws Exception {
063 return this.username != null &&
064 this.password != null &&
065 this.username.equals(username) &&
066 this.password.equals(password);
067 }
068 }