
org.ow2.bonita.identity.auth.PropertiesBasedAuthenticationService Maven / Gradle / Ivy
/**
* Copyright (C) 2007 Bull S. A. S.
* Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois
* This library is free software; you can redistribute it and/or modify it under the terms
* of the GNU Lesser General Public License as published by the Free Software Foundation
* version 2.1 of the License.
* This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
* Floor, Boston, MA 02110-1301, USA.
**/
package org.ow2.bonita.identity.auth;
import java.io.IOException;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import javax.management.InstanceNotFoundException;
import javax.management.MBeanException;
import javax.management.MalformedObjectNameException;
import javax.management.ReflectionException;
import org.ow2.bonita.identity.IdentityServiceMBean;
import org.ow2.bonita.identity.IdentityServiceOp;
import org.ow2.bonita.identity.UserNotFoundException;
import org.ow2.bonita.identity.UserOp;
import org.ow2.bonita.util.Misc;
/**
* {@link AuthenticationServiceOp} implementation that uses specific properties
* stored in an {@link IdentityServiceOp} for authentication purpose.
*
* @see #authenticateUser(String...)
*
* @author "Pierre Vigneras"
* @date Dec 14, 2007
*/
public class PropertiesBasedAuthenticationService implements
AuthenticationServiceOp {
/**
* Property key for which the user name is supposed to be stored in
* IdentityServiceOp UserOp. Value for this key is "Name"
*/
public static final String NAME_KEY = "Name";
/**
* Property key for which the password is supposed to be stored in
* IdentityServiceOp UserOp. Value for this key is "Password"
*/
public static final String PASSWORD_KEY = "Password";
private final IdentityServiceMBean idService;
public PropertiesBasedAuthenticationService(final String jmxUrl,
final String idServiceBindingName) throws MalformedObjectNameException,
InstanceNotFoundException, MBeanException,
ReflectionException, IOException {
this.idService = Misc.getMBeanProxy(IdentityServiceMBean.class, jmxUrl,
idServiceBindingName);
}
/**
* Properties based {@link AuthenticationServiceOp} implementation.
*
* When invoked, this method search for the first user registered into the
* related {@link IdentityServiceOp} implementation that has the property as a
* property set for the key {@link #NAME_KEY} set to params[0]
* and the property {@link #PASSWORD_KEY} set to params[1]
.
*
* @see #NAME_KEY
* @see #PASSWORD_KEY
* @see AuthenticationServiceOp#authenticateUser(String...)
* @see IdentityServiceOp
*
*/
public String authenticateUser(String... params) {
if (params.length != 2) {
throw new IllegalArgumentException(
"Two parameters are required: name and password in that order!");
}
final String name = params[0];
final String password = params[1];
final Collection users = idService.getAllUsers();
final Iterator i = users.iterator();
while (i.hasNext()) {
final String id = i.next();
try {
final UserOp user = idService.getUser(id);
final Properties properties = user.getProperties();
if (name.equals(properties.getProperty(NAME_KEY))
&& password.equals(properties.getProperty(PASSWORD_KEY))) {
return user.getId();
}
} catch (UserNotFoundException unfe) {
throw new RuntimeException("The user with id: " + id
+ " cannot be found! Concurrent access?", unfe);
}
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy