Skip to content

Commit

Permalink
Merge pull request #227 from EmeryEx/2.22.x
Browse files Browse the repository at this point in the history
Added auth plugin for Xoauth2
  • Loading branch information
Slamdunk committed Jan 18, 2023
2 parents 6d197aa + 6ed0bee commit 1d307ff
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
123 changes: 123 additions & 0 deletions src/Protocol/Smtp/Auth/Xoauth2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace Laminas\Mail\Protocol\Smtp\Auth;

use Laminas\Mail\Protocol\Smtp;
use Laminas\Mail\Protocol\Xoauth2\Xoauth2 as Xoauth2AuthEncoder;

use function array_replace_recursive;
use function is_array;

/**
* Performs Xoauth2 authentication
*
* @psalm-suppress PropertyNotSetInConstructor
*/
final class Xoauth2 extends Smtp
{
/**
* SMTP username
*
* @var string
*/
protected $username;

/**
* Xoauth2 access token
*
* @var string
*/
protected $accessToken;

/**
* @param string|array $host (Default: 127.0.0.1)
* @param int|null $port (Default: null)
* @param array|null $config Auth-specific parameters
*/
public function __construct($host = '127.0.0.1', $port = null, ?array $config = null)
{
// Did we receive a configuration array?
$origConfig = $config;
if (is_array($host)) {
// Merge config array with principal array, if provided
if (is_array($config)) {
$config = array_replace_recursive($host, $config);
} else {
$config = $host;
}
}

if (is_array($config)) {
if (isset($config['username'])) {
$this->setUsername((string) $config['username']);
}
if (isset($config['access_token'])) {
$this->setAccessToken((string) $config['access_token']);
}
}

// Call parent with original arguments
parent::__construct($host, $port, $origConfig);
}

/**
* Perform XOAUTH2 authentication with supplied credentials
*
* @return void
*/
public function auth()
{
// Ensure AUTH has not already been initiated.
parent::auth();

$this->_send('AUTH XOAUTH2');
$this->_expect('334');
$this->_send(Xoauth2AuthEncoder::encodeXoauth2Sasl($this->getUsername(), $this->getAccessToken()));
$this->_expect('235');
$this->auth = true;
}

/**
* Set value for username
*
* @param string $username
* @return Xoauth2
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}

/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}

/**
* Set value for access token
*
* @param string $token
* @return Xoauth2
*/
public function setAccessToken($token)
{
$this->accessToken = $token;
return $this;
}

/**
* Get access token
*
* @return string
*/
public function getAccessToken()
{
return $this->accessToken;
}
}
3 changes: 3 additions & 0 deletions src/Protocol/SmtpPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ class SmtpPluginManager extends AbstractPluginManager
'Login' => Smtp\Auth\Login::class,
'plain' => Smtp\Auth\Plain::class,
'Plain' => Smtp\Auth\Plain::class,
'xoauth2' => Smtp\Auth\Xoauth2::class,
'Xoauth2' => Smtp\Auth\Xoauth2::class,
'smtp' => Smtp::class,
'Smtp' => Smtp::class,
'SMTP' => Smtp::class,
Expand Down Expand Up @@ -69,6 +71,7 @@ class SmtpPluginManager extends AbstractPluginManager
Smtp\Auth\Crammd5::class => InvokableFactory::class,
Smtp\Auth\Login::class => InvokableFactory::class,
Smtp\Auth\Plain::class => InvokableFactory::class,
Smtp\Auth\Xoauth2::class => InvokableFactory::class,
Smtp::class => InvokableFactory::class,
];

Expand Down

0 comments on commit 1d307ff

Please sign in to comment.