Skip to content

Commit ca1be38

Browse files
committedFeb 3, 2024
add OAUTH pseudo method
* OAUTH pseudo method will elect either XOAUTH2 or OAUTHBEARER according to server's capabilities Signed-off-by: Edouard Vanbelle <edouard@vanbelle.fr>
1 parent 8a06d24 commit ca1be38

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed
 

‎Net/SMTP.php

+27-8
Original file line numberDiff line numberDiff line change
@@ -709,14 +709,17 @@ public function starttls()
709709

710710
return true;
711711
}
712-
712+
713713
/**
714714
* Attempt to do SMTP authentication.
715715
*
716716
* @param string $uid The userid to authenticate as.
717717
* @param string $pwd The password to authenticate with.
718-
* @param string $method The requested authentication method. If none is
718+
* @param string $method The requested authentication method. If none is
719719
* specified, the best supported method will be used.
720+
* If you use the special method `OAUTH`, library
721+
* will choose between OAUTHBEARER or XOAUTH2
722+
* according the server's capabilities.
720723
* @param bool $tls Flag indicating whether or not TLS should be attempted.
721724
* @param string $authz An optional authorization identifier. If specified, this
722725
* identifier will be used as the authorization proxy.
@@ -750,6 +753,19 @@ public function auth($uid, $pwd , $method = '', $tls = true, $authz = '')
750753
/* Return the PEAR_Error object from _getBestAuthMethod(). */
751754
return $method;
752755
}
756+
} elseif ($method === 'OAUTH') {
757+
// special case of OAUTH, use the supported method
758+
$found = false;
759+
$available_methods = explode(' ', $this->esmtp['AUTH']);
760+
foreach (['OAUTHBEARER', 'XOAUTH2'] as $method) {
761+
if (in_array($method, $available_methods)) {
762+
$found = true;
763+
break;
764+
}
765+
}
766+
if (!$found) {
767+
return PEAR::raiseError("neither OAUTHBEARER nor XOAUTH2 is a supported authentication method");
768+
}
753769
} else {
754770
$method = strtoupper($method);
755771
if (!array_key_exists($method, $this->auth_methods)) {
@@ -1102,25 +1118,28 @@ protected function authGSSAPI($uid, $pwd, $authz = '')
11021118
* Authenticates the user using the XOAUTH2 method.
11031119
*
11041120
* @param string $uid The userid to authenticate as.
1105-
* @param string $token The access token to authenticate with.
1121+
* @param string $token The access token prefixed by it's type
1122+
* example: "Bearer $access_token".
11061123
* @param string $authz The optional authorization proxy identifier.
11071124
* @param object $conn The current object
11081125
*
11091126
* @return mixed Returns a PEAR_Error with an error message on any
11101127
* kind of failure, or true on success.
11111128
* @since 1.9.0
11121129
*/
1130+
//FIXME: to switch into protected method on next major release
11131131
public function authXOAuth2($uid, $token, $authz, $conn)
11141132
{
11151133
$auth = base64_encode("user=$uid\1auth=$token\1\1");
1116-
return $this->_authOAuth('XOAUTH2', $auth, $authz, $conn);
1134+
return $this->authenticateOAuth('XOAUTH2', $auth, $authz, $conn);
11171135
}
11181136

11191137
/**
11201138
* Authenticates the user using the OAUTHBEARER method.
11211139
*
11221140
* @param string $uid The userid to authenticate as.
1123-
* @param string $token The access token to authenticate with.
1141+
* @param string $token The access token prefixed by it's type
1142+
* example: "Bearer $access_token".
11241143
* @param string $authz The optional authorization proxy identifier.
11251144
* @param object $conn The current object
11261145
*
@@ -1129,10 +1148,10 @@ public function authXOAuth2($uid, $token, $authz, $conn)
11291148
* @since 1.9.3
11301149
* @see https://www.rfc-editor.org/rfc/rfc7628.html
11311150
*/
1132-
public function authOAuthBearer($uid, $token, $authz, $conn)
1151+
protected function authOAuthBearer($uid, $token, $authz, $conn)
11331152
{
11341153
$auth = base64_encode("n,a=$uid\1auth=$token\1\1");
1135-
return $this->_authOAuth('OAUTHBEARER', $auth, $authz, $conn);
1154+
return $this->authenticateOAuth('OAUTHBEARER', $auth, $authz, $conn);
11361155
}
11371156

11381157
/**
@@ -1146,7 +1165,7 @@ public function authOAuthBearer($uid, $token, $authz, $conn)
11461165
* @return mixed Returns a PEAR_Error with an error message on any
11471166
* kind of failure, or true on success.
11481167
*/
1149-
protected function _authOAuth( $method, $auth, $authz, $conn)
1168+
protected function authenticateOAuth( $method, $auth, $authz, $conn)
11501169
{
11511170
// Maximum length of the base64-encoded token to be sent in the initial response is 504 - strlen($method) bytes,
11521171
// according to RFC 4954 (https://datatracker.ietf.org/doc/html/rfc4954); for longer tokens an empty initial

0 commit comments

Comments
 (0)