Quotero Wiki » Development » How to create Authentication Source

How to create Authentication Source

Last modified by XWikiGuest on 2011/01/19 13:11

Description

Here is the class diagram about Authentication Source :

diag.PNG

If you want to create a new Authentication Source type, you must build a new AuthenticationSourceImpl inherited class which implements methods from the interface: org.coretechs.quotero.user.AuthenticationSource.

Method

Description

getParams()

Return the authentication source parameters

createParams()

Add authentication source parameters

updateParams()

Update authentication source parameters

getUserFactory()

Return an user factory to get users from authentication source

getGroupFactory()

Return a group factory to get groups from authentication source

toPojo()

Return a plain old Java object from authentication source

Server implementation

Here is a concrete example with Joomla Authentication Source implementation. Methods have been deliberately simplified for understganding (do not use as it).

Implementing new AuthenticationSource

AuthenticationSource inteface allows to manage current Authentication Source by getting user and group, and setting various parameters.

To make your own Authentication Source implementation, you have to inherit AuthenticationSourceImpl.

public class AuthenticationSourceJoomla15 extends AuthenticationSourceImpl {

       /*

             The fields that describe this authentication source.

       */

       private String databaseUrl;

       private String databaseLogin;    

       private String databasePassword

       private String tablePrefix;      

      

       /*

             Get users from your authentication source,

             create a new UserFactory instance and return it.

       */

       public UserFactory getUserFactory() {

             DBManagerMysql.init(databaseUrldatabaseLogindatabasePassword);

return new UserJoomla15Factory(DBManagerMysql.getInstance(), this);

       }     

 

       /*

Get groups from your authentication source,

create a new GroupFactory instance and return it.

       */

       public GroupFactory getGroupFactory() {       

DBManagerMysql.init(databaseUrldatabaseLogindatabasePassword);

return new GroupJoomla15Factory(DBManagerMysql.getInstance(), this);

       }

 

/*

Getters methods used in this implementation.

       */

       public String getDatabaseUrl() { return databaseUrl;  }

       public String getDatabaseLogin() { return databaseLogin; }

       public String getDatabasePassword() { return databasePassword; }

       public String getTablePrefix() { return tablePrefix;}

UserFactory

This factory class is used to get users list from affected Authentication Source.

public class UserJoomla15Factory implements UserFactory {

       private AuthenticationSourceJoomla15 source;

       private DBManagerMysql dbm;

      

       public UserJoomla15Factory(DBManagerMysql _dbm, AuthenticationSourceJoomla15 _source){

             this.dbm = _dbm;

             this.source = _source;

       }

 

       /**

Add new user to specified group.

*/

       public void addUserToGroup(User user, Group group) {

             // Implementation if needed

       }

 

       /**

Check if uid/password are correctly authentified.

Return true if the uid/password are correct, else false.

*/

       public boolean authenticate(String uid, String password) {

String rq = "SELECT ... FROM ... WHERE ...";

             String pwd = null;

             PreparedStatement ps = dbm.getPreparedStatement(rq);

             ResultSet rs = ps.executeQuery();

             if(rs.next()) {

                    pwd = rs.getString("pwd");

             }

             if(pwd != null){

                    return Joomla15PasswordHash.check(password, pwd);

             } else {

                    return false;

             }

       }

 

       /**

             Delete existing user.

*/

       public void deleteUser(User user) {

// Implementation if needed

}

 

/**

Get user from his uid and create new User and return it.

*/

       public User getUser(String uid) {

String rq = "SELECT ... FROM ... WHERE ...";

             PreparedStatement ps = dbm.getPreparedStatement(rq);

             ps.setString(1, uid);

             ResultSet rs = ps.executeQuery();

             rs.next();

             User user = new User();

             user.setUid(uid);

             user.setName(rs.getString("nom"));

             user.setMail(rs.getString("mail"));    

             user.setAuthenticationSourceName(this.authenticationSourceName);

              return user;

       }

/**   

Get users from a Group and create new Vector of User and return it.

*/

       public Vector<User> getUsers(Group group) {

       String rq = "SELECT ... FROM ... WHERE ...";

                    PreparedStatement ps = dbm.getPreparedStatement(rq);

                    ps.setInt(1, Integer.parseInt(group.getGid()));

                    ResultSet rs = ps.executeQuery();

Vector<User> vUsers = new Vector<User>();

                    while(rs.next()){

                           User user = new User();

                           user.setUid(rs.getString("uname"));

                           user.setName(rs.getString("nom"));

                           user.setMail(rs.getString("mail"));    

                           user.setAuthenticationSourceName(this.authenticationSourceName);

                           vUsers.add(user);

                    }

                    return vUsers;                   

       }

 

       /**                    

Get all users and create new Vector of User and return it.

*/

       public Vector<User> getUsers() throws DataSourceException, ConfigException {

// Implementation if needed

       }

 

       /**                    

Remove User from a Group.

*/

       public void removeUserFromGroup(User user, Group group) {

// Implementation if needed

}

 

       /**                    

Save User with his password.

*/

       public void saveUser(User user, String password) {

// Implementation if needed

       }

 

       /**                    

Update User with his password.

*/

       public void updateUser(User user, String password) {

// Implementation if needed

       }

}

GroupFactory

This other factory is used to get groups list and it must be implemented on the same design as UserFactory.

public class GroupJoomla15Factory implements GroupFactory {

       private AuthenticationSourceJoomla15 source;

       private DBManagerMysql dbm;

      

       public GroupJoomla15Factory(DBManagerMysql _dbm, AuthenticationSourceJoomla15 _source){

             this.dbm = _dbm;

             this.source = _source;

       }

      

 

/**

Delete existing group.

*/

       public void deleteGroup(Group group) throws DataSourceException, ConfigException {

// Implementation if needed

}

 

       /**

             Get group from its gid.

             Create a new Group instance and return it.

       */

       public Group getGroup(String gid) throws DataSourceException, ConfigException {

String rq = "SELECT ... FROM ... WHERE ...";

             PreparedStatement ps = dbm.getPreparedStatement(rq);

             ps.setInt(1, Integer.parseInt(gid));

             ResultSet rs = ps.executeQuery();

             rs.next();

             Group group = new Group();

             group.setGid(gid);

             group.setName(rs.getString("nom"));

             group.setAuthenticationSourceName(authenticationSourceName);

             return group;

       }

 

       /**

             Get all groups.

             Create a new Vector of Group and return it.

       */

       public Vector<Group> getGroups() {

String rq = "SELECT ... FROM ... WHERE ...";

             Vector<Group> vGroups = new Vector<Group>();

             PreparedStatement ps = dbm.getPreparedStatement(rq);

             ResultSet rs = ps.executeQuery();

             while(rs.next()){

                    Group group = new Group();

                    group.setGid(rs.getString("gid"));

                    group.setName(rs.getString("nom"));

                    group.setAuthenticationSourceName(authenticationSourceName);

                    vGroups.add(group);

             }

             return vGroups;

       }

       /**

             Get groups owned by an user uid.

             Create a new Vector of Group and return it.

       */

       public Vector<Group> getGroups(String userUid) {

String rq = "SELECT ... FROM ... WHERE ...";

             Vector<Group> vGroups = new Vector<Group>();

             PreparedStatement ps = dbm.getPreparedStatement(rq);

             ps.setString(1, userUid);

             ResultSet rs = ps.executeQuery();

             while(rs.next()){

                    Group group = new Group();

                    group.setGid(rs.getString("gid"));

                    group.setName(rs.getString("nom"));

                    group.setAuthenticationSourceName(authenticationSourceName);

                    vGroups.add(group);

             }

              return vGroups;

       }

 

/**

Save group.

*/

       public void saveGroup(Group group) {

// Implementation if needed

}

 

       /**

             Update group.

       */

       public void updateGroup(Group group) {

// Implementation if needed

}

}

Using the API with AuthenitcationSourceUtil

Once you implement a new Authentication Source, you can use it from any client by using AuthenitcationSourceUtil from the client API.

dd.PNG

Authentication Sources are manageable in AdministrationController.

Create or update an Authentication Source

AuthenticationSourceUtil allow to register a new Authentication Source and to generate the XML parameters based over your implementation.

To do this, there are two methods that be used : addField() and generateXml().

AdministrationController controller = new AdministrationController();

AuthenticationSourceUtil source = new AuthenticationSourceUtil("Joomla Test");

source.addField("databaseUrl""jdbc:mysql://localhost/joomla_db");

source.addField("databaseLogin""root");

source.addField("databasePassword""");

source.addField("tablePrefix""jos_");

controller.createAuthenticationSource(

sessionUid,

source.getName(),

"org.coretechs.quotero.user.joomla.AuthenticationSourceJoomla15", source.generateXml());

Once you implement a new Authentication Source, you can use it from any client by using the client API.

Created by Jérôme Ludmann on 2010/10/06 12:22

This wiki is licensed under a Creative Commons 2.0 license
XWiki Enterprise 2.6.1.33884 - Documentation