@@ -709,14 +709,17 @@ public function starttls()
709
709
710
710
return true ;
711
711
}
712
-
712
+
713
713
/**
714
714
* Attempt to do SMTP authentication.
715
715
*
716
716
* @param string $uid The userid to authenticate as.
717
717
* @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
719
719
* 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.
720
723
* @param bool $tls Flag indicating whether or not TLS should be attempted.
721
724
* @param string $authz An optional authorization identifier. If specified, this
722
725
* identifier will be used as the authorization proxy.
@@ -750,6 +753,19 @@ public function auth($uid, $pwd , $method = '', $tls = true, $authz = '')
750
753
/* Return the PEAR_Error object from _getBestAuthMethod(). */
751
754
return $ method ;
752
755
}
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
+ }
753
769
} else {
754
770
$ method = strtoupper ($ method );
755
771
if (!array_key_exists ($ method , $ this ->auth_methods )) {
@@ -1102,25 +1118,28 @@ protected function authGSSAPI($uid, $pwd, $authz = '')
1102
1118
* Authenticates the user using the XOAUTH2 method.
1103
1119
*
1104
1120
* @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".
1106
1123
* @param string $authz The optional authorization proxy identifier.
1107
1124
* @param object $conn The current object
1108
1125
*
1109
1126
* @return mixed Returns a PEAR_Error with an error message on any
1110
1127
* kind of failure, or true on success.
1111
1128
* @since 1.9.0
1112
1129
*/
1130
+ //FIXME: to switch into protected method on next major release
1113
1131
public function authXOAuth2 ($ uid , $ token , $ authz , $ conn )
1114
1132
{
1115
1133
$ 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 );
1117
1135
}
1118
1136
1119
1137
/**
1120
1138
* Authenticates the user using the OAUTHBEARER method.
1121
1139
*
1122
1140
* @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".
1124
1143
* @param string $authz The optional authorization proxy identifier.
1125
1144
* @param object $conn The current object
1126
1145
*
@@ -1129,10 +1148,10 @@ public function authXOAuth2($uid, $token, $authz, $conn)
1129
1148
* @since 1.9.3
1130
1149
* @see https://www.rfc-editor.org/rfc/rfc7628.html
1131
1150
*/
1132
- public function authOAuthBearer ($ uid , $ token , $ authz , $ conn )
1151
+ protected function authOAuthBearer ($ uid , $ token , $ authz , $ conn )
1133
1152
{
1134
1153
$ 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 );
1136
1155
}
1137
1156
1138
1157
/**
@@ -1146,7 +1165,7 @@ public function authOAuthBearer($uid, $token, $authz, $conn)
1146
1165
* @return mixed Returns a PEAR_Error with an error message on any
1147
1166
* kind of failure, or true on success.
1148
1167
*/
1149
- protected function _authOAuth ( $ method , $ auth , $ authz , $ conn )
1168
+ protected function authenticateOAuth ( $ method , $ auth , $ authz , $ conn )
1150
1169
{
1151
1170
// Maximum length of the base64-encoded token to be sent in the initial response is 504 - strlen($method) bytes,
1152
1171
// according to RFC 4954 (https://datatracker.ietf.org/doc/html/rfc4954); for longer tokens an empty initial
0 commit comments