External database authentication

We encourage users to post events happening in the community to the community events group on https://www.drupal.org.
tmuras's picture

I'm considering writing a module for Drupal 7 that will allow for authenticating users against external database. I like the way this is handled in Moodle - see the attached screen-shot. The only similar module I've found is:
* http://drupal.org/project/sql_auth

What do you think?

AttachmentSize
moodle_ext_auth.jpg154.09 KB

Comments

Two checks

peterx's picture

There is a need in multisite type applications to check if a login is for a master user. Check the local database. if they are not there, check the central database. Or, in other cases, check the central database first and the local ones only if not in the central database.

If that's no more that two DB

tmuras's picture

If that's no more that two DB checks always, then the module I have in mind could potentially solve your need. As I want to authenticate against "external DB", you could set the connection details for your "master" DB.

similar requiremenrt - your progress?

Jons's picture

I have similar requirement - how is your dev going?

Seems like LDAP would be a

cleaver's picture

Seems like LDAP would be a more standard way to do this. If you need single sign-on between different systems, the something like CAS or one of the other SSO solutions might be better all round.

@cleaverI agree, if you can

tmuras's picture

@cleaver
I agree, if you can, use LDAP or better yet LDAP + some SSO.
If you can't have LDAP and/or SSO, or if it's an overkill for you, then external database authentication may be a viable option.

@jons
I have not started any dev yet. I would like to be reasonably confident that a module will get reviewed & accepted into Drupal modules before I start.

This would be helpful to alot of people

chuey's picture

This potential module would help alot of people. There are so many ways to tackle this in Drupal 7, but no actual contributed modules yet. Only example code from various users in the community and elsewhere in InternetLand. This would be extremely helpful.

I'm not so sure...

tmuras's picture

I'm not so sure if many people are interested in this - this thread is not very active :).
@chuey would you maybe be interested in doing some research/review of the existing modules?

Trying this out

chuey's picture

I'm a noob in the module development game. I've gone through every drupal.org posting and external websites i could find on the topic. My main sources of help were:

https://www.touchnoc.com/node/83
http://omegadelta.net/2011/03/23/custom-authentication-with-drupal-7/
http://drupal.stackexchange.com/questions/4346/logging-into-drupal-from-...

Here's is what I'm trying to accomplish and what I've done so far.

I've added an additional database definition to the array in settings.php:

$databases = array (
  'default' =>
  array (
    'default' => // default database definition
    array (
      'database' => 'default_db',
      'username' => 'defusername',
      'password' => 'passone',
      'host' => 'defaulthost',
      'port' => '5432',
      'driver' => 'pgsql',
      'prefix' => '',
    ),
  'external' => // external authenticate database definition
  array (
      'database' => 'external_db',
      'username' => 'extusername',
      'password' => 'passtwo',
      'host' => 'externalhost',
      'port' => '5432',
      'driver' => 'pgsql',
  ),
),
);

I've created a module called mycustomextauth and properly have the .info and .module files created. Here's what I have in the .module file:

<?php

/<strong>
* Implements
hook_form_FORM_ID_alter().
*/

/</
strong>
*
Alter the user login block form
*/

        function
mycustomextauth_form_user_login_block_alter(&$form, &$form_state) {
           
_mycustomextauth_user_login_form_alter($form, $form_state);
        }

/<
strong>
*
Alter the user login page
*/

        function
mycustomextauth_form_user_login_alter(&$form, &$form_state) {
           
_mycustomextauth_user_login_form_alter($form, $form_state);
        }



        function
_mycustomextauth_user_login_form_alter(&$form, &$form_state) {
           
$saveForm = $form;

           
$form = array();

           
// overrides Drupals default validator
           
foreach( $saveForm as $key => $value ) {
                if(
$key == '#validate' ) {

                   
$form[ $key ] = array();
                    foreach(
$value as $validator ) {
                        if(
$validator == 'user_login_authenticate_validate' ) {
                           
$validator = 'mycustomextauth_authenticate_validate';
                        }
                       
$form[ $key ][] = $validator;
                    }
                } else {
                   
$form[ $key ] = $value;
                }
            }
        }

/</
strong>
*
Custom Validation
*/


        function
mycustomextauth_authenticate_validate( $form, &$form_state ) {

           
$name = $form_state[ 'values' ][ 'name' ];
           
$pass = $form_state[ 'values' ][ 'pass' ];

            if(
$name == 'root' )
            {
                return
user_login_authenticate_validate( $form, $form_state );
            }

           
$authenticated = your_awesome_authentication_method($name, $pass)

            if (
$authenticated)
            {
               
// this sets up the external user with Drupal by creating a local entry. If they don't exist they are added
               
user_external_login_register($name, "mycustomextauth");

               
// we must set the 'uid' to pass back.  this looks up the logged in user and sets the Drupal UID
               
$account = user_external_load($name);
               
$form_state['uid'] = $account->uid;
            }
            else
            {
               
// do nothing, Drupal will handle the errors

           
}
        }


?>

My question is what do I do with this line?

$authenticated = your_awesome_authentication_method($name, $pass)

I know I need to call the external DB with the following (In the DB table, the usr_email field is the username field):

$records = db_select( 'users', 'u', array( 'target' => 'other' ))
->fields( 'u', array( 'usr_email', 'usr_password' ))
->condition( 'u.usr_email', $usr_email, '=' )
->execute();

But exactly where and how? Any help you can provide is deeply appreciated.

@chuey Did you have a look at

External authentication

Anisorf's picture

Hi, I'm also working on a system where i have Drupal7 site used mainly like a portal, Moodle site for courses , CAS for Single Sign On/Off between the drupal and the moodle and an external DB where i have all the user information and the course information.
I notice that moodle2 has two authentication methods CAS+LDAP or External DB, but not CAS + External DB , and here my problem raise : is the only solution to use CAS + LDAP for the authentication/authorization ?
I've try also the solution CAS + LDAP but in this case,since LDAP is not a DB and i need to memorize not only user infos, i need however to use External DB on which ldap will connect, and everything is getting more and more complicated.
Is there something that I'm missing here? Any suggestion will means a lot to me.
Thanks in advance.

P.S. i also think that external DB authentication module would help a lot of ppl.

@Anisorf I think you could

tmuras's picture

@Anisorf

I think you could configure CAS itself to use DB as it's back-end. Moodle/Drupal would only use CAS then. You should refer to CAS documentation if that's possible - would you let me know how did you go?

Tomek

@tmuras

Anisorf's picture

I've done the configuration of the CAS web-app to use DB as it's back-end and is working perfectly with drupal, but i think that the plugin of Moodle for CAS is made to be dependent on LDAP server as back-end...Like i sad maybe i'm missing something, but i think the CAS config is ok...I'm waithing on replay in the moodle forum to see if i'm right for the CAS plugin.
Thanks for your suggestions, i let you know if i get an moodle answer.

Have you tried Passkey

ngreenup's picture

Have you tried Passkey http://drupal.org/project/passkey ? I have not yet tried it but maybe joining that project to help tackle the "Planned enhancements" on this module to fit your vision. Not yet implemented features are listed as:

  • Namespace external users, allowing local and external auth to mix
  • cleaner integration of user management functions
  • allow the external system to return data about the user

Passkey looks like having a

tmuras's picture

Passkey looks like having a similar goal, but there is very little in that project - merely few lines of code. It doesn't seem very active as well. But - if it grows into something reasonable, I think I could consider joining Passkey instead of starting a new module.

Thanks for sharing it @ngreenup.

Tomek