001/*
002 * Copyright (C) 2012 eXo Platform SAS.
003 *
004 * This is free software; you can redistribute it and/or modify it
005 * under the terms of the GNU Lesser General Public License as
006 * published by the Free Software Foundation; either version 2.1 of
007 * the License, or (at your option) any later version.
008 *
009 * This software is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * You should have received a copy of the GNU Lesser General Public
015 * License along with this software; if not, write to the Free
016 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018 */
019
020package org.crsh.auth;
021
022import org.crsh.plugin.CRaSHPlugin;
023import org.crsh.plugin.PluginContext;
024import org.crsh.plugin.PropertyDescriptor;
025
026import java.util.Arrays;
027
028public class SimpleAuthenticationPlugin extends
029  CRaSHPlugin<AuthenticationPlugin> implements
030  AuthenticationPlugin<String> {
031
032  /** The username. */
033  public static final PropertyDescriptor<String> SIMPLE_USERNAME =
034    PropertyDescriptor.create(
035      "auth.simple.username",
036      "admin",
037      "The username");
038
039  /** The password. */
040  public static final PropertyDescriptor<String> SIMPLE_PASSWORD =
041    PropertyDescriptor.create(
042      "auth.simple.password",
043      "admin",
044      "The password",
045      true);
046
047  /** . */
048  private String username;
049
050  /** . */
051  private String password;
052
053  @Override
054  protected Iterable<PropertyDescriptor<?>> createConfigurationCapabilities() {
055    return Arrays.<PropertyDescriptor<?>>asList(
056      SIMPLE_USERNAME,
057      SIMPLE_PASSWORD);
058  }
059
060  public Class<String> getCredentialType() {
061    return String.class;
062  }
063
064  @Override
065  public AuthenticationPlugin getImplementation() {
066    return this;
067  }
068
069  @Override
070  public void init() {
071    PluginContext context = getContext();
072    this.username = context.getProperty(SIMPLE_USERNAME);
073    this.password = context.getProperty(SIMPLE_PASSWORD);
074  }
075
076  public String getName() {
077    return "simple";
078  }
079
080  public boolean authenticate(String username, String password)
081    throws Exception {
082    return this.username != null &&
083      this.password != null &&
084      this.username.equals(username) &&
085      this.password.equals(password);
086  }
087}