Hi all,

I would like to use my Imap server as authentication service for drupal users, so they have no need and possibility to register with new usernames and passwords in portal. I hope there is a solution for this through some kind of hooked functions, but I didn't find a module doing this. Please, is there some recommended solution how to do it ? I already have a working code for imap authentication (needs username and password, returns different code on success and failure), but I do not know, how to connect it with drupal.

Replies with short step-by-step guide for dummies are preferrable :-)

Thanks in advance,

David Komanek

Comments

Cainan’s picture

I threw this one together for a project i am working on.

variable 'myauth_login_imap' is either 'hostname' or 'hostname:port'

function myauth_login_auth($username,$password,$server) {
global $user;

$imap_s = variable_get('myauth_login_imap','localhost');
if ($imap = imap_open('{'.$imap_s.'}INBOX',$username,$password)) {
imap_close($imap);
return TRUE;
} else {
return FALSE;
}
}

komanek’s picture

Thank you very much for the script. How do I connect this with drupal so that this will be used instead of standard password checking and will create "verified" users wihtout hte need to "pre-register" them ?

Thanks again,

David

yelvington’s picture

You need to write an authentication module. I found the webserver_auth module to be the easiest to follow as a guide when I needed to get a Drupal installation to authenticate with an external registration system. It wasn't hard to get it working; just an hour or two one afternoon.

There are several others you could examine as well.

I had to "hide" the "my account" function, as I have not yet had the time to figure out how to override the code that allows users to update their passwords (and even change their usernames!!) ... which means I also had to abandon use of the profile system. If anybody has a simple guide for doing that, I'd appreciate a tip.

komanek’s picture

Thank you for pointing me to the right direction. But still, I have problems. Authorization thorugh apache works fine, but webserver_auth module of drupal claims to have problems:

Notice: Trying to get property of non-object in /usr/local/apache2/htdocs/drupal/includes/bootstrap.inc on line 498

Notice: Undefined property: stdClass::$uid in /usr/local/apache2/htdocs/drupal/includes/session.inc on line 36

warning: array_merge() [function.array-merge]: Argument #2 is not an array in /usr/local/apache2/htdocs/drupal/modules/webserver_auth.module on line 28.

Notice: Trying to get property of non-object in /usr/local/apache2/htdocs/drupal/includes/bootstrap.inc on line 600

user error: Duplicate entry '' for key 2
query: INSERT INTO users (created, changed, uid) VALUES ('1131092667', '1131092667', '11') in /usr/local/apache2/htdocs/drupal/includes/database.mysql.inc on line 66.

Notice: Trying to get property of non-object in /usr/local/apache2/htdocs/drupal/includes/bootstrap.inc on line 600

warning: Invalid argument supplied for foreach() in /usr/local/apache2/htdocs/drupal/modules/user.module on line 174.

Notice: Trying to get property of non-object in /usr/local/apache2/htdocs/drupal/includes/bootstrap.inc on line 600
Home
drupal

Do I need some patch for this ? I use the latest version of the module (4.6.0).

David

Cainan’s picture

save the following code to a file called 'imap_auth.module'

<?php

/**
 * Implementation of hook_help
 */
function imap_auth_help($section) {
  switch ($section) {
    case 'admin/modules#description':
      return t('Oracle LDAP+IMAP authenicationg.');
  }
}

function imap_auth_settings() {

  $output .= form_textfield(t('IMAP Server[:Port]'),'imap_auth_server',
    variable_get('imap_auth_server','imapserver'), 80, 100,
    t('IMAP Server:port to authenticate against.'),NULL,TRUE);

  return $output;
}

function imap_auth_auth($username,$password,$server) {
  global $user;

  $imap_s = variable_get('imap_auth_server','localhost');
  if ($imap = imap_open('{'.$imap_s.'}INBOX',$username,$password)) {
    imap_close($imap);
    return TRUE;
  } else {
    return FALSE;
  }
}
?>
komanek’s picture

Thank you very much. I also enabled the module in administration pages of drupal and then I configuret the imap server hostname. Still, it seems there must be something to tell drupal to use this module instead of the standard auth module, because the module is not called. How to do this ? :-)

Sincerely,

David

Cainan’s picture

yes.
the _auth hooks require the users to login with user@email as the username.

so you would enter foo@foomail.com etc....

then the module will try to log in to the imap server with username foo and the passwd the user enters.

komanek’s picture

Well, thanks, it really helped to go through the hook, but I am still not able to login. Suppose I have an existing account "myreal" in both drupal and imap servers. If I login as "myreal" with drupal password, it works fine. But if I login as "myreal@mydomain.cz" with imap or drupal password, i get an authentication error. imap password on imap server works, even from this drupal module.

And now suppose I have another imap account "mytest" which has no corresponding drupal account. If I try to log in drupal as "mytest@mydomain.cz", drupal still claims about unrecogniyed username or password and the drupal account is not created for me.

But I am pretty sure I am going through the module now.

erk’s picture

I am getting the following error when trying to configure the imap_auth.module with 4.7 beta

Fatal error: Call to undefined function form_textfield() in /usr/local/www/apache22/data/drupal/modules/imap_auth.module on line 14

I presume this is due the the API changes from 4.6 to 4.7, can anyone suggest a work around? The legacy module doesn't help.

Cainan’s picture

now that i have CVS access, i will beat out a quick Imap auth module for 4.6 and 4.7 tonight/tomorrow, and upload it.

MattClare’s picture

@Cainan I created an account just to give you encouragement.

My department is looking to use Drupal to handle workshop registration and news and IMAP auth would solve so many issues.

Cainan’s picture

Just uploaded the 4.6.X branch version. i will convert what little needs to be changed tonight, and upload the 4.7 version.