You are here

function mailhandler_authenticate_tokenauth in Mailhandler 6

Same name and namespace in other branches
  1. 7 mailhandler.module \mailhandler_authenticate_tokenauth()

Authenticate message based on token from tokenauth module If the user's token is found somewhere in the "to" field, assign that user's uid and name to the node object. A rough search for the token somewhere in the "toaddress" is performed instead of an exact, ordered match in order to allow some freedom in the format of allowed "toaddress". For example, if using a catchall email address, the toaddress could be:

f93ksj35dx@example.com - where f93ksj35dx is the user's token or alternatively: f93ksj35dx-foo@example.com - where f93ksj35dx is the user's token and foo is the name of an Organic Group to which the message should be assigned.

A rough search allows for different approaches to use this single authentication method.

Parameters

$node: Object a node object

$header: Object of message header information

$origbody: String message body text

$mailbox: Array of mailbox configuration

Return value

object, the node object

1 string reference to 'mailhandler_authenticate_tokenauth'
mailhandler_mailhandler_authenticate_info in ./mailhandler.module
Implementation of hook_mailhandler_authenticate_info()

File

./mailhandler.module, line 1207
Mailhandler module code.

Code

function mailhandler_authenticate_tokenauth($node, $header, $origbody, $mailbox) {
  if (module_exists('tokenauth')) {
    list($fromaddress, $fromname) = mailhandler_get_fromaddress($header, $mailbox);

    // If user with given email address exists and their token is in the toaddress, allow.
    if (($from_user = mailhandler_user_load($fromaddress, $node->pass, $mailbox)) && strpos($header->to[0]->mailbox, tokenauth_get_token($from_user->uid)) !== FALSE) {
      $node->uid = $from_user->uid;
      $node->name = $from_user->name;
    }
    else {
      if (function_exists('mailalias_user') && ($from_user = mailhandler_user_load_alias($fromaddress, $node, $mailbox)) && strpos($header->to[0]->mailbox, tokenauth_get_token($from_user->uid)) !== FALSE) {
        $node->uid = $from_user->uid;
        $node->name = $from_user->name;
      }
      else {

        // If token authentication fails, try as anonymous.
        $node->uid = 0;
        $node->name = $fromname;
      }
    }
  }
  return $node;
}