branch: externals/auth-source-xoauth2-plugin
commit c1641945af25684fc5e22ccdf96198b2aa2478c5
Author: Xiyue Deng <manp...@gmail.com>
Commit: Xiyue Deng <manp...@gmail.com>

    Add special handling to work with smtpmail
    
    When sending mails, the auth-source queries from smtpmail doesn't
    contain the :user field (meanwhile queries from Gnus seems to always
    include :user).  When using predefined provider credentials, only the
    :user field is different which is missing from the query, therefore we
    should not just assume the matched result is the correct one.
    Additionally, we need to check whether smtpmail-smtp-user, which was
    set by smtpmail, is the same as :user to be sure.
---
 auth-source-xoauth2-plugin.el | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/auth-source-xoauth2-plugin.el b/auth-source-xoauth2-plugin.el
index e8f51676bf..f28f5eb365 100644
--- a/auth-source-xoauth2-plugin.el
+++ b/auth-source-xoauth2-plugin.el
@@ -92,8 +92,28 @@ expected that `token_url', `client_id', `client_secret', and
       (dolist (auth-data orig-res)
         (auth-source-do-trivia "[xoauth2-plugin] Matched auth data: %s"
                                (pp-to-string auth-data))
-        (let ((auth (plist-get auth-data :auth)))
-          (when (equal auth "xoauth2")
+        (let ((auth (plist-get auth-data :auth))
+              (user (plist-get auth-data :user)))
+          (when (and (equal auth "xoauth2")
+                     ;; When sending mails, some auth-source query results from
+                     ;; some smtpmail authentication methods don't contain the
+                     ;; :user field (meanwhile queries from Gnus seems to 
always
+                     ;; include :user).  When using predefined provider
+                     ;; credentials, only the :user field is different to
+                     ;; distinguish among different accounts, which is
+                     ;; unfortunately missing in certain cases.  Fortunately,
+                     ;; smtpmail may set smtpmail-smtp-user to the user value
+                     ;; when X-Message-SMTP-Method is properly set.  Therefore
+                     ;; additionally, assuming X-Message-SMTP-Method is set
+                     ;; correctly, we need to check whether smtpmail-smtp-user
+                     ;; is the same as :user to be sure.
+                     (if smtpmail-smtp-user
+                         (progn
+                           (auth-source-do-trivia
+                            "[xoauth2-plugin] user: %s, smtpmail-smtp-user: %s"
+                            user smtpmail-smtp-user)
+                           (string= smtpmail-smtp-user user))
+                       t))
             (auth-source-do-debug
              (concat "[xoauth2-plugin] account \"%s\" has :auth set to "
                      "`xoauth2'.  Will get access token.")

Reply via email to