GNU bug report logs - #78240
30.1; xoauth2 authentication fails in gnus imap mail fetching

Previous Next

Package: emacs;

Reported by: Anush V <j <at> gnu.org>

Date: Sun, 4 May 2025 13:52:02 UTC

Severity: normal

Found in version 30.1

To reply to this bug, email your comments to 78240 AT debbugs.gnu.org.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Sun, 04 May 2025 13:52:03 GMT) Full text and rfc822 format available.

Acknowledgement sent to Anush V <j <at> gnu.org>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 04 May 2025 13:52:03 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Anush V <j <at> gnu.org>
To: bug-gnu-emacs <at> gnu.org
Cc: manphiz <at> gmail.com
Subject: 30.1; xoauth2 authentication fails in gnus imap mail fetching
Date: Sun, 04 May 2025 09:51:10 -0400
Hello Emacs Maintainers,

I’m using gnus and I’m unable to downloads emails using xoauth2
authentication (provided by auth-source-xoauth2-plugin).  I’m able to
send emails though.

Configuration:
;; emacs --no-init
     (setopt imap-log t)
     (require 'mail-source)
     (add-to-list 'mail-sources '(imap :server "imap.gmail.com"
                                       :port 993
                                       :user "<my_email>"
                                       :stream ssl
                                       ;; dont delete from server
                                       :dontexpunge t
                                       ;; mark as seen after fetching
                                       :fetchflag "\\Seen"
                                       :authentication xoauth2
                                       ))
     (setq gnus-select-method '(nnnil ""))
     (setq gnus-secondary-select-methods
           '((nnmaildir "mail"
                        (directory "~/tmp")
                        (get-new-mail t))))


corresponding logs

     Waiting for response from imap.gmail.com...done
     Mail source (imap :server imap.gmail.com :port 993 :user <my_email> :stream ssl :dontexpunge t :fetchflag \Seen :authentication xoauth2) error (IMAP error: nil)
     nnmaildir: Reading incoming mail (no new mail)...done


I suspect the imap-authenticate call in mail-source-fetch-imap is
failing because xoauth2 and its related CHECK/AUTHENTICATE functions
are missing from imap-authenticator-alist

Please let me know if i’m wrong/missing something.

Since auth-source-xoauth2-plugin is an elpa package, i mailed here and
cc’ed the package maintainer.  If this is wrong place for this, please
let me know the appropriate mailing list.

Thank you for your time

* * *

In GNU Emacs 30.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.43,
cairo version 1.18.2)
System Description: Guix System

Configured using:
 'configure
 CONFIG_SHELL=/gnu/store/m0xdsa8cfq6mq1kxgxmpmpg71la4f0b9-bash-minimal-5.1.16/bin/bash
 SHELL=/gnu/store/m0xdsa8cfq6mq1kxgxmpmpg71la4f0b9-bash-minimal-5.1.16/bin/bash --prefix=/gnu/store/lq0nwm8qkj9cmyjm85z3dcqrjnglhcym-emacs-next-pgtk-30.1-rc1-2.7144e84 --enable-fast-install --with-pgtk --with-cairo --with-modules --with-native-compilation=aot --disable-build-details'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ
JPEG LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 MODULES NATIVE_COMP
NOTIFY INOTIFY PDUMPER PGTK PNG RSVG SECCOMP SOUND SQLITE3 THREADS
TIFF TOOLKIT_SCROLL_BARS TREE_SITTER XIM GTK3 ZLIB

--
Regards,
Anush V




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Sun, 04 May 2025 23:44:02 GMT) Full text and rfc822 format available.

Message #8 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Xiyue Deng <manphiz <at> gmail.com>
To: Anush V <j <at> gnu.org>, bug-gnu-emacs <at> gnu.org
Subject: Re: 30.1; xoauth2 authentication fails in gnus imap mail fetching
Date: Sun, 04 May 2025 16:43:28 -0700
[Message part 1 (text/plain, inline)]
Hi Anush,

Anush V <j <at> gnu.org> writes:

> Hello Emacs Maintainers,
>
> I’m using gnus and I’m unable to downloads emails using xoauth2
> authentication (provided by auth-source-xoauth2-plugin).  I’m able to
> send emails though.
>
> Configuration:
> ;; emacs --no-init
>      (setopt imap-log t)
>      (require 'mail-source)
>      (add-to-list 'mail-sources '(imap :server "imap.gmail.com"
>                                        :port 993
>                                        :user "<my_email>"
>                                        :stream ssl
>                                        ;; dont delete from server
>                                        :dontexpunge t
>                                        ;; mark as seen after fetching
>                                        :fetchflag "\\Seen"
>                                        :authentication xoauth2
>                                        ))
>      (setq gnus-select-method '(nnnil ""))
>      (setq gnus-secondary-select-methods
>            '((nnmaildir "mail"
>                         (directory "~/tmp")
>                         (get-new-mail t))))
>

I have only tested using Gnus with nnimap (see documentation at [1] or
[2]) where you want to add `(nnimap-authenticator xoauth2)' to your
`nnimap' settings.  I haven't tested with `mail-sources', which seems to
work differently compared to nnimap, and hence the implementation may be
missing.  I probably need to understand how `mail-sources' works and
extend the plugin accordingly, which could take a while.

Meanwhile, do you want to try using `nnimap' and see whether that works?

>
> corresponding logs
>
>      Waiting for response from imap.gmail.com...done
>      Mail source (imap :server imap.gmail.com :port 993 :user <my_email> :stream ssl :dontexpunge t :fetchflag \Seen :authentication xoauth2) error (IMAP error: nil)
>      nnmaildir: Reading incoming mail (no new mail)...done
>
>
> I suspect the imap-authenticate call in mail-source-fetch-imap is
> failing because xoauth2 and its related CHECK/AUTHENTICATE functions
> are missing from imap-authenticator-alist
>
> Please let me know if i’m wrong/missing something.
>
> Since auth-source-xoauth2-plugin is an elpa package, i mailed here and
> cc’ed the package maintainer.  If this is wrong place for this, please
> let me know the appropriate mailing list.
>
> Thank you for your time
>
> * * *
>
> In GNU Emacs 30.1 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.43,
> cairo version 1.18.2)
> System Description: Guix System
>
> Configured using:
>  'configure
>  CONFIG_SHELL=/gnu/store/m0xdsa8cfq6mq1kxgxmpmpg71la4f0b9-bash-minimal-5.1.16/bin/bash
>  SHELL=/gnu/store/m0xdsa8cfq6mq1kxgxmpmpg71la4f0b9-bash-minimal-5.1.16/bin/bash --prefix=/gnu/store/lq0nwm8qkj9cmyjm85z3dcqrjnglhcym-emacs-next-pgtk-30.1-rc1-2.7144e84 --enable-fast-install --with-pgtk --with-cairo --with-modules --with-native-compilation=aot --disable-build-details'
>
> Configured features:
> ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ
> JPEG LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 MODULES NATIVE_COMP
> NOTIFY INOTIFY PDUMPER PGTK PNG RSVG SECCOMP SOUND SQLITE3 THREADS
> TIFF TOOLKIT_SCROLL_BARS TREE_SITTER XIM GTK3 ZLIB
>
> --
> Regards,
> Anush V

[1] https://elpa.gnu.org/packages/auth-source-xoauth2-plugin.html
[2] https://gitlab.com/manphiz/auth-source-xoauth2-plugin/

-- 
Regards,
Xiyue Deng
[signature.asc (application/pgp-signature, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Mon, 05 May 2025 03:12:04 GMT) Full text and rfc822 format available.

Message #11 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Anush V <j <at> gnu.org>
To: Xiyue Deng <manphiz <at> gmail.com>
Cc: bug-gnu-emacs <at> gnu.org
Subject: Re: 30.1; xoauth2 authentication fails in gnus imap mail fetching
Date: Sun, 04 May 2025 23:10:09 -0400
> From: Xiyue Deng <manphiz <at> gmail.com>
> Date: Sun,  4 May 2025 16:43:28 -0700
>
> Hi Anush,
>
> Anush V <j <at> gnu.org> writes:
>
>> Hello Emacs Maintainers,
>>
>> I’m using gnus and I’m unable to downloads emails using xoauth2
>> authentication (provided by auth-source-xoauth2-plugin).  I’m able to
>> send emails though.
>>
>> Configuration:
>> ;; emacs --no-init
>>      (setopt imap-log t)
>>      (require 'mail-source)
>>      (add-to-list 'mail-sources '(imap :server "imap.gmail.com"
>>                                        :port 993
>>                                        :user "<my_email>"
>>                                        :stream ssl
>>                                        ;; dont delete from server
>>                                        :dontexpunge t
>>                                        ;; mark as seen after fetching
>>                                        :fetchflag "\\Seen"
>>                                        :authentication xoauth2
>>                                        ))
>>      (setq gnus-select-method '(nnnil ""))
>>      (setq gnus-secondary-select-methods
>>            '((nnmaildir "mail"
>>                         (directory "~/tmp")
>>                         (get-new-mail t))))
>>
>
> I have only tested using Gnus with nnimap (see documentation at [1] or
> [2]) where you want to add `(nnimap-authenticator xoauth2)' to your
> `nnimap' settings.  I haven't tested with `mail-sources', which seems to
> work differently compared to nnimap, and hence the implementation may be
> missing.  I probably need to understand how `mail-sources' works and
> extend the plugin accordingly, which could take a while.
>
> Meanwhile, do you want to try using `nnimap' and see whether that works?
>

Thank you for considering extending the functionality to mail-sources.

I tested nnimap and it works as expected. However, unlike
mail-sources, nnimap doesn't download emails to my machine.

--
Regards,
Anush Veeranala




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Mon, 05 May 2025 14:26:01 GMT) Full text and rfc822 format available.

Message #14 received at 78240 <at> debbugs.gnu.org (full text, mbox):

From: Robert Pluim <rpluim <at> gmail.com>
To: Anush V <j <at> gnu.org>
Cc: 78240 <at> debbugs.gnu.org, Xiyue Deng <manphiz <at> gmail.com>
Subject: Re: bug#78240: 30.1; xoauth2 authentication fails in gnus imap mail
 fetching
Date: Mon, 05 May 2025 16:25:05 +0200
>>>>> On Sun, 04 May 2025 23:10:09 -0400, Anush V <j <at> gnu.org> said:

    Anush> I tested nnimap and it works as expected. However, unlike
    Anush> mail-sources, nnimap doesn't download emails to my machine.

It will if you configure `nnimap-split-methods' to copy from the
nnimap backend to a local one. Or just copy the emails manually.

(I took a look at imap.el. It currently has zero integration with
auth-source, so getting nnimap to work should be a lot easier).

Robert
-- 




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Wed, 07 May 2025 01:56:02 GMT) Full text and rfc822 format available.

Message #17 received at 78240 <at> debbugs.gnu.org (full text, mbox):

From: Anush V <j <at> gnu.org>
To: Robert Pluim <rpluim <at> gmail.com>
Cc: 78240 <at> debbugs.gnu.org, Xiyue Deng <manphiz <at> gmail.com>
Subject: Re: bug#78240: 30.1; xoauth2 authentication fails in gnus imap mail
 fetching
Date: Tue, 06 May 2025 21:54:13 -0400
> From: Robert Pluim <rpluim <at> gmail.com>
> Date: Mon,  5 May 2025 16:25:05 +0200
>
>>>>>> On Sun, 04 May 2025 23:10:09 -0400, Anush V <j <at> gnu.org> said:
>
>     Anush> I tested nnimap and it works as expected. However, unlike
>     Anush> mail-sources, nnimap doesn't download emails to my machine.
>
> It will if you configure `nnimap-split-methods' to copy from the
> nnimap backend to a local one. Or just copy the emails manually.

I couldn’t figure out from info manual how to copy mails to local
using nnimap-split-methods.  I tried (nnimap-split-methods
'("nnmaildir+mail:inbox2" "")), but it ended up splitting mails on the
server.

I’m able to copy the emails manually though.

--
Regards,
Anush




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Wed, 07 May 2025 07:20:02 GMT) Full text and rfc822 format available.

Message #20 received at 78240 <at> debbugs.gnu.org (full text, mbox):

From: Robert Pluim <rpluim <at> gmail.com>
To: Anush V <j <at> gnu.org>
Cc: 78240 <at> debbugs.gnu.org, Xiyue Deng <manphiz <at> gmail.com>
Subject: Re: bug#78240: 30.1; xoauth2 authentication fails in gnus imap mail
 fetching
Date: Wed, 07 May 2025 09:19:04 +0200
>>>>> On Tue, 06 May 2025 21:54:13 -0400, Anush V <j <at> gnu.org> said:

    >> From: Robert Pluim <rpluim <at> gmail.com>
    >> Date: Mon,  5 May 2025 16:25:05 +0200
    >> 
    >>>>>>> On Sun, 04 May 2025 23:10:09 -0400, Anush V <j <at> gnu.org> said:
    >> 
    Anush> I tested nnimap and it works as expected. However, unlike
    Anush> mail-sources, nnimap doesn't download emails to my machine.
    >> 
    >> It will if you configure `nnimap-split-methods' to copy from the
    >> nnimap backend to a local one. Or just copy the emails manually.

    Anush> I couldn’t figure out from info manual how to copy mails to local
    Anush> using nnimap-split-methods.  I tried (nnimap-split-methods
    Anush> '("nnmaildir+mail:inbox2" "")), but it ended up splitting mails on the
    Anush> server.

I think that should be

(nnimap-split-methods '(("nnmaildir+mail:inbox2" "")))

(but I donʼt do nnimap splitting)

Robert
-- 




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Sat, 10 May 2025 22:28:02 GMT) Full text and rfc822 format available.

Message #23 received at 78240 <at> debbugs.gnu.org (full text, mbox):

From: Anush V <j <at> gnu.org>
To: Robert Pluim <rpluim <at> gmail.com>
Cc: 78240 <at> debbugs.gnu.org, Xiyue Deng <manphiz <at> gmail.com>
Subject: Re: bug#78240: 30.1; xoauth2 authentication fails in gnus imap mail
 fetching
Date: Sat, 10 May 2025 18:27:05 -0400
> From: Robert Pluim <rpluim <at> gmail.com>
> Date: Wed,  7 May 2025 09:19:04 +0200
>
>>>>>> On Tue, 06 May 2025 21:54:13 -0400, Anush V <j <at> gnu.org> said:
>
>     >> From: Robert Pluim <rpluim <at> gmail.com>
>     >> Date: Mon,  5 May 2025 16:25:05 +0200
>     >>
>     >>>>>>> On Sun, 04 May 2025 23:10:09 -0400, Anush V <j <at> gnu.org> said:
>     >>
>     Anush> I tested nnimap and it works as expected. However, unlike
>     Anush> mail-sources, nnimap doesn't download emails to my machine.
>     >>
>     >> It will if you configure `nnimap-split-methods' to copy from the
>     >> nnimap backend to a local one. Or just copy the emails manually.
>
>     Anush> I couldn’t figure out from info manual how to copy mails to local
>     Anush> using nnimap-split-methods.  I tried (nnimap-split-methods
>     Anush> '("nnmaildir+mail:inbox2" "")), but it ended up splitting mails on the
>     Anush> server.
>
> I think that should be
>
> (nnimap-split-methods '(("nnmaildir+mail:inbox2" "")))

This also didn’t copy mails to local.

--
Regards,
Anush




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Tue, 13 May 2025 16:34:01 GMT) Full text and rfc822 format available.

Message #26 received at 78240 <at> debbugs.gnu.org (full text, mbox):

From: Robert Pluim <rpluim <at> gmail.com>
To: Anush V <j <at> gnu.org>
Cc: 78240 <at> debbugs.gnu.org, Xiyue Deng <manphiz <at> gmail.com>
Subject: Re: bug#78240: 30.1; xoauth2 authentication fails in gnus imap mail
 fetching
Date: Tue, 13 May 2025 18:33:39 +0200
>>>>> On Sat, 10 May 2025 18:27:05 -0400, Anush V <j <at> gnu.org> said:

    >> From: Robert Pluim <rpluim <at> gmail.com>
    >> Date: Wed,  7 May 2025 09:19:04 +0200
    >> 
    >>>>>>> On Tue, 06 May 2025 21:54:13 -0400, Anush V <j <at> gnu.org> said:
    >> 
    >> >> From: Robert Pluim <rpluim <at> gmail.com>
    >> >> Date: Mon,  5 May 2025 16:25:05 +0200
    >> >>
    >> >>>>>>> On Sun, 04 May 2025 23:10:09 -0400, Anush V <j <at> gnu.org> said:
    >> >>
    Anush> I tested nnimap and it works as expected. However, unlike
    Anush> mail-sources, nnimap doesn't download emails to my machine.
    >> >>
    >> >> It will if you configure `nnimap-split-methods' to copy from the
    >> >> nnimap backend to a local one. Or just copy the emails manually.
    >> 
    Anush> I couldn’t figure out from info manual how to copy mails to local
    Anush> using nnimap-split-methods.  I tried (nnimap-split-methods
    Anush> '("nnmaildir+mail:inbox2" "")), but it ended up splitting mails on the
    Anush> server.
    >> 
    >> I think that should be
    >> 
    >> (nnimap-split-methods '(("nnmaildir+mail:inbox2" "")))

    Anush> This also didn’t copy mails to local.

Hmm, I get splitting when I also set `nnimap-inbox' appropriately, but
itʼs all server side splitting, and it uses `nnmail-split-methods'
instead of the nnimap variable, so either Iʼve got a config issue or
itʼs a a bug. Iʼll keep looking over the next week.

Robert
-- 




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Fri, 23 May 2025 16:59:01 GMT) Full text and rfc822 format available.

Message #29 received at 78240 <at> debbugs.gnu.org (full text, mbox):

From: Robert Pluim <rpluim <at> gmail.com>
To: Anush V <j <at> gnu.org>
Cc: 78240 <at> debbugs.gnu.org, Xiyue Deng <manphiz <at> gmail.com>
Subject: Re: bug#78240: 30.1; xoauth2 authentication fails in gnus imap mail
 fetching
Date: Fri, 23 May 2025 18:57:53 +0200
>>>>> On Tue, 13 May 2025 18:33:39 +0200, Robert Pluim <rpluim <at> gmail.com> said:

    Robert> Hmm, I get splitting when I also set `nnimap-inbox' appropriately, but
    Robert> itʼs all server side splitting, and it uses `nnmail-split-methods'
    Robert> instead of the nnimap variable, so either Iʼve got a config issue or
    Robert> itʼs a a bug. Iʼll keep looking over the next week.

So I was completely wrong about how nnimap splitting is implemented:
the splitting logic runs in Gnus, but all it does is move messages
around on the IMAP server, so it canʼt be used in place of imap.el to
download messages.

Robert
-- 




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Thu, 31 Jul 2025 15:23:02 GMT) Full text and rfc822 format available.

Message #32 received at 78240 <at> debbugs.gnu.org (full text, mbox):

From: Robert Pluim <rpluim <at> gmail.com>
To: Xiyue Deng <manphiz <at> gmail.com>
Cc: 78240 <at> debbugs.gnu.org, j <at> gnu.org
Subject: Re: bug#78240: 30.1; xoauth2 authentication fails in gnus imap mail
 fetching
Date: Thu, 31 Jul 2025 17:21:56 +0200
[Message part 1 (text/plain, inline)]
>>>>> On Sun, 04 May 2025 16:43:28 -0700, Xiyue Deng <manphiz <at> gmail.com> said:


    Xiyue> I have only tested using Gnus with nnimap (see documentation at [1] or
    Xiyue> [2]) where you want to add `(nnimap-authenticator xoauth2)' to your
    Xiyue> `nnimap' settings.  I haven't tested with `mail-sources', which seems to
    Xiyue> work differently compared to nnimap, and hence the implementation may be
    Xiyue> missing.  I probably need to understand how `mail-sources' works and
    Xiyue> extend the plugin accordingly, which could take a while.

Hi Xiyue,

I implemented auth-source lookup for imap.el (patch below). Would
there be any chance to see if you could extend your package to support
xoauth2 for imap.el?

It should be enough to add xoauth2 to `imap-authenticators', and
`imap-xoauth2-auth-p' and `imap-xoauth2-auth' to
`imap-authenticator-alist' (and their implementations, of course).

Robert
-- 

[0001-Add-auth-source-support-to-imap.el.patch (text/x-diff, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Tue, 05 Aug 2025 18:02:02 GMT) Full text and rfc822 format available.

Message #35 received at 78240 <at> debbugs.gnu.org (full text, mbox):

From: Xiyue Deng <manphiz <at> gmail.com>
To: Robert Pluim <rpluim <at> gmail.com>
Cc: 78240 <at> debbugs.gnu.org, j <at> gnu.org
Subject: Re: bug#78240: 30.1; xoauth2 authentication fails in gnus imap mail
 fetching
Date: Tue, 05 Aug 2025 11:01:25 -0700
[Message part 1 (text/plain, inline)]
Hi Robert,

Robert Pluim <rpluim <at> gmail.com> writes:

>>>>>> On Sun, 04 May 2025 16:43:28 -0700, Xiyue Deng <manphiz <at> gmail.com> said:
>
>
>     Xiyue> I have only tested using Gnus with nnimap (see documentation at [1] or
>     Xiyue> [2]) where you want to add `(nnimap-authenticator xoauth2)' to your
>     Xiyue> `nnimap' settings.  I haven't tested with `mail-sources', which seems to
>     Xiyue> work differently compared to nnimap, and hence the implementation may be
>     Xiyue> missing.  I probably need to understand how `mail-sources' works and
>     Xiyue> extend the plugin accordingly, which could take a while.
>
> Hi Xiyue,
>
> I implemented auth-source lookup for imap.el (patch below). Would
> there be any chance to see if you could extend your package to support
> xoauth2 for imap.el?
>
> It should be enough to add xoauth2 to `imap-authenticators', and
> `imap-xoauth2-auth-p' and `imap-xoauth2-auth' to
> `imap-authenticator-alist' (and their implementations, of course).
>

Thanks for implementing this!  As I haven't used imap.el I cannot test
this right now.  I'll try to find some time to set up such an account
and report back.  Hopefully this won't take too long.

> Robert
> -- 
>
> From 19901eda8b693937861d93856322f4b73a3dc231 Mon Sep 17 00:00:00 2001
> From: Robert Pluim <rpluim <at> gmail.com>
> Date: Mon, 28 Jul 2025 11:11:29 +0200
> Subject: [PATCH] Add auth-source support to imap.el
>
> * lisp/net/imap.el: Require auth-source.
> (imap-use-auth-source): New user option.
> (auth-source-creation-prompts): New defvar.
> (imap-credentials): New function, performs 'auth-source' search.
> (imap-authenticate): Call 'imap-credentials' if no user is
> specified and 'imap-use-auth-source' is non-nil.
>
> * etc/NEWS: Announce the feature.
> ---
>  etc/NEWS         |  12 +++++
>  lisp/net/imap.el | 119 +++++++++++++++++++++++++++++++++--------------
>  2 files changed, 96 insertions(+), 35 deletions(-)
>
> diff --git a/etc/NEWS b/etc/NEWS
> index b790c7e318c..61eadc63106 100644
> --- a/etc/NEWS
> +++ b/etc/NEWS
> @@ -1338,6 +1338,18 @@ replies.
>  
>  ** Imap
>  
> +---
> +*** New user option 'imap-use-auth-source'.
> +Default t.  Controls whether 'imap-authenticate' will attempt to lookup
> +credentials via 'auth-source' (but only when it is called with username
> +nil).
> +
> +---
> +*** 'imap-authenticate' can now retrieve credentials via 'auth-source'.
> +Calling 'imap-authenticate' with username nil will now perform an
> +'auth-source' search in order to obtain the username and the password.
> +Customize 'imap-use-auth-source' to nil to prevent this.
> +
>  ---
>  *** 'imap-authenticate' can now use PLAIN authentication.
>  "AUTH=PLAIN" support is auto-enabled if the IMAP server supports it.  Pass
> diff --git a/lisp/net/imap.el b/lisp/net/imap.el
> index f705da317e5..e381b3dc95f 100644
> --- a/lisp/net/imap.el
> +++ b/lisp/net/imap.el
> @@ -139,6 +139,8 @@
>  (eval-when-compile (require 'cl-lib))
>  (require 'utf7)
>  (require 'rfc2104)
> +(require 'auth-source)
> +
>  ;; Hmm... digest-md5 is not part of Emacs.
>  ;; FIXME: Should/can we use sasl-digest.el instead?
>  (declare-function digest-md5-parse-digest-challenge "ext:digest-md5")
> @@ -246,6 +248,13 @@ imap-store-password
>    "If non-nil, store session password without prompting."
>    :type 'boolean)
>  
> +(defcustom imap-use-auth-source t
> +  "If non-nil, lookup username and password using `auth-source'.
> +This only takes effect when the \"user\" passed to `imap-authenticate'
> +is nil."
> +  :type 'boolean
> +  :version "31.1")
> +
>  ;;; Various variables
>  
>  (defvar imap-fetch-data-hook nil
> @@ -1107,6 +1116,26 @@ imap-ping-server
>        (imap-ok-p (imap-send-command-wait "NOOP" buffer))
>      (error nil)))
>  
> +(defvar auth-source-creation-prompts)
> +
> +;; Stolen from, uhm, "inspired by" nnimap.el.
> +;; We don't want to drag in the whole of nnimap.
> +(defun imap-credentials (address ports user)
> +  (let* ((auth-source-creation-prompts
> +          '((user  . "imap: username for %h: ")
> +            (secret . "imap: password for %u: ")))
> +         (found (nth 0 (auth-source-search :max 1
> +                                           :host address
> +                                           :port ports
> +                                           :user user
> +                                           :require '(:user :secret)
> +                                           :create t))))
> +    (if found
> +        (list (plist-get found :user)
> +              (auth-info-password found)
> +              (plist-get found :save-function))
> +      nil)))
> +
>  (defun imap-authenticate (&optional user passwd buffer)
>    "Authenticate to server in BUFFER, using current buffer if nil.
>  It uses the authenticator specified when opening the server.
> @@ -1117,41 +1146,61 @@ imap-authenticate
>  user for a username and/or password."
>    (with-current-buffer (or buffer (current-buffer))
>      (if (not (eq imap-state 'nonauth))
> -	(or (eq imap-state 'auth)
> -	    (eq imap-state 'selected)
> -	    (eq imap-state 'examine))
> -      (make-local-variable 'imap-username)
> -      (make-local-variable 'imap-password)
> -      (make-local-variable 'imap-last-authenticator)
> -      (when user (setq imap-username user))
> -      (when passwd (setq imap-password passwd))
> -      (if imap-auth
> -	  (and (setq imap-last-authenticator
> -		     (assq imap-auth imap-authenticator-alist))
> -	       (funcall (nth 2 imap-last-authenticator) (current-buffer))
> -	       (setq imap-state 'auth))
> -	;; Choose authenticator.
> -	(let ((auths imap-authenticators)
> -	      auth)
> -	  (while (setq auth (pop auths))
> -	    ;; OK to use authenticator?
> -	    (setq imap-last-authenticator
> -		  (assq auth imap-authenticator-alist))
> -	    (when (funcall (nth 1 imap-last-authenticator) (current-buffer))
> -	      (message "imap: Authenticating to `%s' using `%s'..."
> -		       imap-server auth)
> -	      (setq imap-auth auth)
> -	      (if (funcall (nth 2 imap-last-authenticator) (current-buffer))
> -		  (progn
> -		    (message "imap: Authenticating to `%s' using `%s'...done"
> -			     imap-server auth)
> -		    ;; set imap-state correctly on successful auth attempt
> -		    (setq imap-state 'auth)
> -		    ;; stop iterating through the authenticator list
> -		    (setq auths nil))
> -		(message "imap: Authenticating to `%s' using `%s'...failed"
> -			 imap-server auth)))))
> -	imap-state))))
> +        (or (eq imap-state 'auth)
> +            (eq imap-state 'selected)
> +            (eq imap-state 'examine))
> +      ;; .authinfo can contain symbolic or numeric ports.
> +      (let ((ports (cond ((eq imap-port 993)
> +                          '(993 "imaps"))
> +                         ((eq imap-port 143)
> +                          '(143 "imap"))
> +                         (t imap-port)))
> +            (imap-credentials nil)
> +            (passwd-save-function nil))
> +        (make-local-variable 'imap-username)
> +        (make-local-variable 'imap-password)
> +        (make-local-variable 'imap-last-authenticator)
> +        (when user (setq imap-username user))
> +        (when passwd (setq imap-password passwd))
> +        (unless (or user (not imap-use-auth-source))
> +                                        ; Don't perform auth-source
> +                                        ; lookup if a user was specified
> +                                        ; explicitly.
> +          (when (setq imap-credentials (imap-credentials imap-server ports user))
> +            (setq imap-username (nth 0 imap-credentials)
> +                  imap-password (nth 1 imap-credentials)
> +                  passwd-save-function (nth 2 imap-credentials))))
> +        (if imap-auth                   ; It's a reauth
> +            (and (setq imap-last-authenticator
> +                       (assq imap-auth imap-authenticator-alist))
> +                 (funcall (nth 2 imap-last-authenticator) (current-buffer))
> +                 (setq imap-state 'auth))
> +          ;; Choose authenticator.
> +          (let ((auths imap-authenticators)
> +                auth)
> +            (while (setq auth (pop auths))
> +              ;; OK to use authenticator?
> +              (setq imap-last-authenticator
> +                    (assq auth imap-authenticator-alist))
> +              (when (funcall (nth 1 imap-last-authenticator) (current-buffer))
> +                (message "imap: Authenticating to `%s' using `%s'..."
> +                         imap-server auth)
> +                (setq imap-auth auth)
> +                (if (funcall (nth 2 imap-last-authenticator) (current-buffer))
> +                    (progn
> +                      (message "imap: Authenticating to `%s' using `%s'...done"
> +                               imap-server auth)
> +                      ;; Save the credentials if a new token was created
> +                      ;; via auth-source.
> +                      (when (functionp passwd-save-function)
> +                        (funcall passwd-save-function))
> +                      ;; set imap-state correctly on successful auth attempt
> +                      (setq imap-state 'auth)
> +                      ;; stop iterating through the authenticator list
> +                      (setq auths nil))
> +                  (message "imap: Authenticating to `%s' using `%s'...failed"
> +                           imap-server auth)))))
> +          imap-state)))))
>  
>  (defun imap-close (&optional buffer)
>    "Close connection to server in BUFFER.
> -- 
> 2.39.5
>

-- 
Regards,
Xiyue Deng
[signature.asc (application/pgp-signature, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#78240; Package emacs. (Wed, 06 Aug 2025 07:57:02 GMT) Full text and rfc822 format available.

Message #38 received at 78240 <at> debbugs.gnu.org (full text, mbox):

From: Robert Pluim <rpluim <at> gmail.com>
To: Xiyue Deng <manphiz <at> gmail.com>
Cc: 78240 <at> debbugs.gnu.org, j <at> gnu.org
Subject: Re: bug#78240: 30.1; xoauth2 authentication fails in gnus imap mail
 fetching
Date: Wed, 06 Aug 2025 09:56:38 +0200
>>>>> On Tue, 05 Aug 2025 11:01:25 -0700, Xiyue Deng <manphiz <at> gmail.com> said:

    Xiyue> Hi Robert,
    Xiyue> Robert Pluim <rpluim <at> gmail.com> writes:

    >>>>>>> On Sun, 04 May 2025 16:43:28 -0700, Xiyue Deng <manphiz <at> gmail.com> said:
    >> 
    >> 
    Xiyue> I have only tested using Gnus with nnimap (see documentation at [1] or
    Xiyue> [2]) where you want to add `(nnimap-authenticator xoauth2)' to your
    Xiyue> `nnimap' settings.  I haven't tested with `mail-sources', which seems to
    Xiyue> work differently compared to nnimap, and hence the implementation may be
    Xiyue> missing.  I probably need to understand how `mail-sources' works and
    Xiyue> extend the plugin accordingly, which could take a while.
    >> 
    >> Hi Xiyue,
    >> 
    >> I implemented auth-source lookup for imap.el (patch below). Would
    >> there be any chance to see if you could extend your package to support
    >> xoauth2 for imap.el?
    >> 
    >> It should be enough to add xoauth2 to `imap-authenticators', and
    >> `imap-xoauth2-auth-p' and `imap-xoauth2-auth' to
    >> `imap-authenticator-alist' (and their implementations, of course).
    >> 

    Xiyue> Thanks for implementing this!  As I haven't used imap.el I cannot test
    Xiyue> this right now.  I'll try to find some time to set up such an account
    Xiyue> and report back.  Hopefully this won't take too long.

If you have an existing nnimap server using xoauth2, then testing
against it with imap.el is easy:

(progn
  (require 'imap)
  (let ((i-buf (imap-open "imap.gmail.com" 993 'tls)))
    (imap-authenticate nil nil i-buf)
    (imap-mailbox-lsub "*" nil nil i-buf)))

If xoauth2 support is working, then that should auto-detect it and use
your xoauth2 credentials from .authinfo

If it doesnʼt work, then customize `imap-log' to t before loading
imap.el, and there will be an "*imap-log*" buffer containing a session
trace.

Robert
-- 




This bug report was last modified 6 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.