Click here to Skip to main content
15,881,882 members
Articles / Programming Languages / PHP

Authentication Using IMAP

Rate me:
Please Sign up or sign in to vote.
4.86/5 (4 votes)
27 May 2014CPOL2 min read 12K   6   1
Authenticating an application using IMAP

Introduction

Most applications have an authentication process, which gives a user authorization to use an application. Most commonly user credentials are stored in a database table or third party APIs are used such as Facebook to authenticate users. In this article, I explain how IMAP authentication can be used to give users access to your application.

If you are developing an application where user identity information is not necessary and you have email accounts on an IMAP server, then you can use the login credentials for the email accounts to authenticate your users.

Consider the following scenario, you maintain a company application. When a new employee joins the company, they are given an email account by the network administrator. Your application can authenticate the employee using the employees email account, which means your application doesn't need to create login accounts. When an employee leaves the company, the network administrator disables their email account, which will in turn deny the employee access to your application. With this approach, all user accounts as centralized. One of the disadvantages is that, no identity information for the user apart from the email address exists. In order for your application to be selective about which users can access your application, you will need a separate lookup data store such as a database or an XML data file. This approach will mainly suit applications where identity information is not required and anyone with an email account can access the application, such as a company message board where the email address can be used to identity the employee.

The code presented below uses the IMAP protocol and its LOGIN command to authenticate email accounts.

PHP
class ImapAuthentication {
    protected $con;

    public function __construct($host, $port){
        $this->con = fsockopen($host, $port, $errno, $errstr, 30);
        $this->getResponse();
    }

    public function authenticate($username, $password){
        fwrite($this->con, 'A1 LOGIN ' . $username . ' ' . $password . PHP_EOL);
        return $this->getResponse();
    }

    protected function getResponse(){
        while(true){
            $line = fgets($this->con);
            $segments = explode(' ', $line);
            if($segments[1] =='OK'){
                return true;
            }elseif($segments[1] =='NO'){
                return false;
            }
        }
    }
}

Using the Code

PHP
$imapAuth = new ImapAuthentication('ssl://imap.gmail.com', 993);
$auth = $imapAuth->authenticate('username', 'password');

if($auth){
    echo 'Login Success';
}else{
    echo 'Login failed';
}

Note: For SSL connections, you must have the php_openssl extension enabled.

This brings me to the end of this article. Please feel free to leave your comments and suggestions.

History

  • 27th May, 2014: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionor with ldap Pin
Member 104239697-Jul-20 21:47
Member 104239697-Jul-20 21:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.