Improve solution for #758 and #759

This commit is contained in:
the-djmaze 2022-12-08 12:13:35 +01:00
parent a2ec25fbd4
commit 33a4692c06
3 changed files with 22 additions and 7 deletions

View file

@ -21,9 +21,6 @@ abstract class SASL
final public static function factory(string $type)
{
if (\preg_match('/^([A-Z2]+)(?:-(.+))?$/Di', $type, $m)) {
if ('XOAUTH2' === $m[1] || 'OAUTHBEARER' === $m[1]) {
$m[1] = 'OAUTH';
}
$class = __CLASS__ . "\\{$m[1]}";
if (\class_exists($class) && $class::isSupported($m[2] ?? '')) {
return new $class($m[2] ?? '');
@ -35,9 +32,6 @@ abstract class SASL
public static function isSupported(string $type) : bool
{
if (\preg_match('/^([A-Z2]+)(?:-(.+))?$/Di', $type, $m)) {
if ('XOAUTH2' === $m[1] || 'OAUTHBEARER' === $m[1]) {
$m[1] = 'OAUTH';
}
$class = __CLASS__ . "\\{$m[1]}";
return \class_exists($class) && $class::isSupported($m[2] ?? '');
}

View file

@ -7,7 +7,7 @@
namespace SnappyMail\SASL;
class OAuth extends \SnappyMail\SASL
class OAuthBearer extends \SnappyMail\SASL
{
public function authenticate(string $username, string $passphrase, ?string $authzid = null) : string
{

View file

@ -0,0 +1,21 @@
<?php
/**
* https://datatracker.ietf.org/doc/html/rfc7628
* https://developers.google.com/gmail/imap/xoauth2-protocol
*/
namespace SnappyMail\SASL;
class XOAuth2 extends \SnappyMail\SASL
{
public function authenticate(string $username, string $passphrase, ?string $authzid = null) : string
{
return $this->encode("user={$username}\x01auth=Bearer {$passphrase}\x01\x01");
}
public static function isSupported(string $param) : bool
{
return true;
}
}