Skip to content

Commit

Permalink
[mms] Added Horde_Smtp_Password_Xoauth2 class.
Browse files Browse the repository at this point in the history
  • Loading branch information
slusarz committed Oct 25, 2013
1 parent e7c90bb commit 7d989fe
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 10 deletions.
13 changes: 11 additions & 2 deletions framework/Smtp/doc/Horde/Smtp/UPGRADING
Expand Up @@ -11,8 +11,17 @@
This lists the API changes between releases of the package.


Upgrading to 1.3.0
==================

- Horde_Smtp_Password_Xoauth2

Added class to abstract production of the necessary token for XOAUTH2 SASL
authentication.


Upgrading to 1.2.0
===================
==================

- Horde_Smtp

Expand All @@ -23,7 +32,7 @@ Upgrading to 1.2.0


Upgrading to 1.1.0
===================
==================

- Horde_Smtp

Expand Down
70 changes: 70 additions & 0 deletions framework/Smtp/lib/Horde/Smtp/Password/Xoauth2.php
@@ -0,0 +1,70 @@
<?php
/**
* Copyright 2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Smtp
*/

/**
* Generates an OAuth 2.0 authentication token as used in the Gmail XOAUTH2
* authentication mechanism.
*
* See: https://developers.google.com/gmail/xoauth2_protocol
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Smtp
* @since 1.3.0
*/
class Horde_Smtp_Password_Xoauth2 implements Horde_Smtp_Password
{
/**
* Access token.
*
* @var string
*/
public $access_token;

/**
* Username.
*
* @var string
*/
public $username;

/**
* Constructor.
*
* @param string $username The username.
* @param string $access_token The access token.
*/
public function __construct($username, $access_token)
{
$this->username = $username;
$this->access_token = $access_token;
}

/**
* Return the password to use for the server connection.
*
* @return string The password.
*/
public function getPassword()
{
// base64("user=" {User} "^Aauth=Bearer " {Access Token} "^A^A")
// ^A represents a Control+A (\001)
return base64_encode(
'user=' . $this->username . "\1" .
'auth=Bearer ' . $this->access_token . "\1\1"
);
}

}
22 changes: 14 additions & 8 deletions framework/Smtp/package.xml
Expand Up @@ -10,18 +10,18 @@
<email>slusarz@horde.org</email>
<active>yes</active>
</lead>
<date>2013-10-22</date>
<date>2013-10-25</date>
<version>
<release>1.2.7</release>
<api>1.2.0</api>
<release>1.3.0</release>
<api>1.3.0</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Added Horde_Smtp_Password_Xoauth2 class.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
Expand All @@ -39,6 +39,9 @@
<dir name="Filter">
<file name="Data.php" role="php" />
</dir> <!-- /lib/Horde/Smtp/Filter -->
<dir name="Password">
<file name="Xoauth2.php" role="php" />
</dir> <!-- /lib/Horde/Smtp/Password -->
<file name="Connection.php" role="php" />
<file name="Debug.php" role="php" />
<file name="Exception.php" role="php" />
Expand All @@ -62,6 +65,7 @@
<file name="FilterDataTest.php" role="test" />
<file name="phpunit.xml" role="test" />
<file name="RemoteServerTest.php" role="test" />
<file name="Xoauth2Test.php" role="test" />
</dir> <!-- /test/Horde/Smtp -->
</dir> <!-- /test/Horde -->
</dir> <!-- /test -->
Expand Down Expand Up @@ -139,13 +143,15 @@
<install as="Horde/Smtp/Password.php" name="lib/Horde/Smtp/Password.php" />
<install as="Horde/Smtp/Translation.php" name="lib/Horde/Smtp/Translation.php" />
<install as="Horde/Smtp/Filter/Data.php" name="lib/Horde/Smtp/Filter/Data.php" />
<install as="Horde/Smtp/Password/Xoauth2.php" name="lib/Horde/Smtp/Password/Xoauth2.php" />
<install as="locale/Horde_Smtp.pot" name="locale/Horde_Smtp.pot" />
<install as="Horde/Smtp/AllTests.php" name="test/Horde/Smtp/AllTests.php" />
<install as="Horde/Smtp/bootstrap.php" name="test/Horde/Smtp/bootstrap.php" />
<install as="Horde/Smtp/conf.php.dist" name="test/Horde/Smtp/conf.php.dist" />
<install as="Horde/Smtp/FilterDataTest.php" name="test/Horde/Smtp/FilterDataTest.php" />
<install as="Horde/Smtp/phpunit.xml" name="test/Horde/Smtp/phpunit.xml" />
<install as="Horde/Smtp/RemoteServerTest.php" name="test/Horde/Smtp/RemoteServerTest.php" />
<install as="Horde/Smtp/Xoauth2Test.php" name="test/Horde/Smtp/Xoauth2Test.php" />
</filelist>
</phprelease>
<changelog>
Expand Down Expand Up @@ -276,15 +282,15 @@
</release>
<release>
<version>
<release>1.2.7</release>
<api>1.2.0</api></version>
<release>1.3.0</release>
<api>1.3.0</api></version>
<stability>
<release>stable</release>
<api>stable</api></stability>
<date>2013-10-22</date>
<date>2013-10-25</date>
<license uri="http://www.horde.org/licenses/lgpl21">LGPL-2.1</license>
<notes>
*
* [mms] Added Horde_Smtp_Password_Xoauth2 class.
</notes>
</release>
</changelog>
Expand Down
43 changes: 43 additions & 0 deletions framework/Smtp/test/Horde/Smtp/Xoauth2Test.php
@@ -0,0 +1,43 @@
<?php
/**
* Copyright 2013 Horde LLC (http://www.horde.org/)
*
* See the enclosed file COPYING for license information (LGPL). If you
* did not receive this file, see http://www.horde.org/licenses/lgpl21.
*
* @category Horde
* @copyright 2013 Horde LLC
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Smtp
* @subpackage UnitTests
*/

/**
* Tests for the mailbox object.
*
* @author Michael Slusarz <slusarz@horde.org>
* @category Horde
* @copyright 2013 Horde LLC
* @ignore
* @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
* @package Smtp
* @subpackage UnitTests
*/
class Horde_Smtp_Xoauth2Test extends PHPUnit_Framework_TestCase
{

public function testTokenGeneration()
{
// Example from https://developers.google.com/gmail/xoauth2_protocol
$xoauth2 = new Horde_Smtp_Password_Xoauth2(
'someuser@example.com',
'vF9dft4qmTc2Nvb3RlckBhdHRhdmlzdGEuY29tCg=='
);

$this->assertEquals(
'dXNlcj1zb21ldXNlckBleGFtcGxlLmNvbQFhdXRoPUJlYXJlciB2RjlkZnQ0cW1UYzJOdmIzUmxja0JoZEhSaGRtbHpkR0V1WTI5dENnPT0BAQ==',
$xoauth2->getPassword()
);
}

}

0 comments on commit 7d989fe

Please sign in to comment.