Skip to content

Commit 1d307ff

Browse files
authored
Merge pull request #227 from EmeryEx/2.22.x
Added auth plugin for Xoauth2
2 parents 6d197aa + 6ed0bee commit 1d307ff

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

src/Protocol/Smtp/Auth/Xoauth2.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
namespace Laminas\Mail\Protocol\Smtp\Auth;
4+
5+
use Laminas\Mail\Protocol\Smtp;
6+
use Laminas\Mail\Protocol\Xoauth2\Xoauth2 as Xoauth2AuthEncoder;
7+
8+
use function array_replace_recursive;
9+
use function is_array;
10+
11+
/**
12+
* Performs Xoauth2 authentication
13+
*
14+
* @psalm-suppress PropertyNotSetInConstructor
15+
*/
16+
final class Xoauth2 extends Smtp
17+
{
18+
/**
19+
* SMTP username
20+
*
21+
* @var string
22+
*/
23+
protected $username;
24+
25+
/**
26+
* Xoauth2 access token
27+
*
28+
* @var string
29+
*/
30+
protected $accessToken;
31+
32+
/**
33+
* @param string|array $host (Default: 127.0.0.1)
34+
* @param int|null $port (Default: null)
35+
* @param array|null $config Auth-specific parameters
36+
*/
37+
public function __construct($host = '127.0.0.1', $port = null, ?array $config = null)
38+
{
39+
// Did we receive a configuration array?
40+
$origConfig = $config;
41+
if (is_array($host)) {
42+
// Merge config array with principal array, if provided
43+
if (is_array($config)) {
44+
$config = array_replace_recursive($host, $config);
45+
} else {
46+
$config = $host;
47+
}
48+
}
49+
50+
if (is_array($config)) {
51+
if (isset($config['username'])) {
52+
$this->setUsername((string) $config['username']);
53+
}
54+
if (isset($config['access_token'])) {
55+
$this->setAccessToken((string) $config['access_token']);
56+
}
57+
}
58+
59+
// Call parent with original arguments
60+
parent::__construct($host, $port, $origConfig);
61+
}
62+
63+
/**
64+
* Perform XOAUTH2 authentication with supplied credentials
65+
*
66+
* @return void
67+
*/
68+
public function auth()
69+
{
70+
// Ensure AUTH has not already been initiated.
71+
parent::auth();
72+
73+
$this->_send('AUTH XOAUTH2');
74+
$this->_expect('334');
75+
$this->_send(Xoauth2AuthEncoder::encodeXoauth2Sasl($this->getUsername(), $this->getAccessToken()));
76+
$this->_expect('235');
77+
$this->auth = true;
78+
}
79+
80+
/**
81+
* Set value for username
82+
*
83+
* @param string $username
84+
* @return Xoauth2
85+
*/
86+
public function setUsername($username)
87+
{
88+
$this->username = $username;
89+
return $this;
90+
}
91+
92+
/**
93+
* Get username
94+
*
95+
* @return string
96+
*/
97+
public function getUsername()
98+
{
99+
return $this->username;
100+
}
101+
102+
/**
103+
* Set value for access token
104+
*
105+
* @param string $token
106+
* @return Xoauth2
107+
*/
108+
public function setAccessToken($token)
109+
{
110+
$this->accessToken = $token;
111+
return $this;
112+
}
113+
114+
/**
115+
* Get access token
116+
*
117+
* @return string
118+
*/
119+
public function getAccessToken()
120+
{
121+
return $this->accessToken;
122+
}
123+
}

src/Protocol/SmtpPluginManager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ class SmtpPluginManager extends AbstractPluginManager
4141
'Login' => Smtp\Auth\Login::class,
4242
'plain' => Smtp\Auth\Plain::class,
4343
'Plain' => Smtp\Auth\Plain::class,
44+
'xoauth2' => Smtp\Auth\Xoauth2::class,
45+
'Xoauth2' => Smtp\Auth\Xoauth2::class,
4446
'smtp' => Smtp::class,
4547
'Smtp' => Smtp::class,
4648
'SMTP' => Smtp::class,
@@ -69,6 +71,7 @@ class SmtpPluginManager extends AbstractPluginManager
6971
Smtp\Auth\Crammd5::class => InvokableFactory::class,
7072
Smtp\Auth\Login::class => InvokableFactory::class,
7173
Smtp\Auth\Plain::class => InvokableFactory::class,
74+
Smtp\Auth\Xoauth2::class => InvokableFactory::class,
7275
Smtp::class => InvokableFactory::class,
7376
];
7477

0 commit comments

Comments
 (0)