Notes on migrating to OAUTH2 with Gnus
Table of Contents
1. Introduction
Last year, my employer began offering a two-factor authentication (2FA) protocol through Cisco Duo. This April, it was announced that this optional service would be mandatory for students in May and all faculty in June. After some prodding, I decided it would be better to embrace the change than to curse the disruption it was going to cause later.
I am writing this document in case others find their workflow disrupted, as I did.
Let me note that I have been through a similar, but less comprehensive, migration to 2FA with my previous employer in 2016-7. I recall wrestling with the assorted problems and finding there were almost no solutions in the FOSS community at that time. This experience made the current migration more difficult for me because I had to spend a lot of time unlearning my old lessons. Worse, it steered me in the wrong directions so that I wasted a good deal of time.
2. Enrolment and Expectations
Enrolment was done via email with no information provided by IST to prepare for the disruption, errh, extra step in authentication. My wife, who uses Thunderbird, discovered she was locked out of her account the next morning. I suggested she use the Office web-mail interface–that was not a pleasant experience for either of us.
3. Thunderbird
It turns out the Thunderbird has created a very seamless OAUTH2 authentication module. All that was needed was to change the security type of both the IMAP (incoming) and SMTP (outgoing) servers to OAUTH2. The first time the client tried to access the user's INBOX, Thunderbird popped up a browser window where the 2FA login was completed, then everything worked as of old.
4. Gnus
I use Gnus, a newsreader written in Emacs Lisp, that also has support for email. As that link to the Gnus tutorial shows, there is no discussion of how to configure Gnus to use an OAUTH2-based authentication scheme.
After several false starts, I settled on DavMail. DavMail works as a
shim: Gnus is configured so that it thinks davmail
is both the IMAP
and SMTP server, and davmail
handles those tasks and authentication
with the MS Exchange server and 2FA.
Why did I choose davmail
?
4.1. Configuration of Gnus
The easiest way to configure a running Gnus instance is to use the
*Server*
buffer. From the *Group*
buffer, one types ^
to enter
the *Server*
buffer. Once there, you will be able to add new server
by pressing a
. That will prompt for the server method (nnimap
in
my case), then a name (DavMail
for me), then it opens up a new
buffer with some cryptic code like
(nnimap "DavMail")
To enable Gnus
to use davmail
, I added:
(nnimap "DavMail" (nnimap-authinfo-file "~/.authinfo.gpg") (nnimap-address "localhost") (nnimap-server-port 1143) (nnimap-record-commands t) (nnimap-stream plain))
To explain:
- The first line tells
Gnus
the server protocol and the name it will use; - the second line says to use the file
.authinfo.gpg
to get account information (username, password, hostname); - the third line says that the server's address is
localhost
; - the fourth line is the port the SMTP server is listening on (the default for
davmail
); - the fifth line is optional and really only useful for debugging;
- the sixth line says to communicate with the server in plain text.
I can set-up an ssl
connection between Gnus
and davmail
, but to
get things working, I want to keep it simple.
4.2. TODO Update the Gnus wiki based on this experience
4.3. Update authinfo
As noted, Gnus
will use authinfo.gpg
to get password data
from. So, I update it with the appropriate information:
machine localhost login UMnetid@umanitoba.ca password <mypassword>
Because localhost
is the hostname of both IMAP and SMTP servers, I
leave off any port information. To flush the stale authinfo
data in
Emacs, one does M-: (auth-source-forget-all-cached) RET
.
4.4. Update .gnus.el
The configuration file of Gnus
needs updating, too. To gnus-posting-styles
, I add:
("DavMail:.+" (address ,(cdr (assoc 'umanitoba email-addresses))) (X-Message-SMTP-Method "smtp localhost 1025 UMnetid@umanitoba.ca"))
This will ensure that emails in buffers with names starting with
DavMail:
will have email sent through davmail
.
4.5. Installation of DavMail
I use Debian testing, a Linux distribution, and unless I really need to do otherwise, I install software from Debian's repositories. So, installation was as simple as looking up the software and seeing what else needed to be installed.
root@t14:~# apt-cache search davmail davmail - POP/IMAP/SMTP/CalDav/LDAP to Microsoft Exchange gateway
root@t14:~# apt-get -s install davmail
That lists a lot of extra software, but without email access, I can't
do my job. So, I installed davmail
:
root@t14:~# apt-get install davmail
See Linux setup for more information.
4.6. Configuration of DavMail
As it turned out, almost all the third-party documentation on
davmail
that I read is inaccurate. In fact, the most useful source
of information is the DavMail FAQ and Getting Started pages.
Here are the settings that I eventually discovered work:
4.6.1. Main
There are a number of options for the Exchange Protocol
. I believe
that to ensure that davmail
pops up the correct prompt,
O365Interactive is the correct choice.
All other settings are left at the default value, including the url.
4.6.2. Network
In the theme of "keep it simple", I leave the settings alone:
4.6.3. Encryption
It turned out that the settings here can be left "as are". It took me a long time to figure this out, because web-pages like that maintained by UVa's Physics Department suggest otherwise. This site, in fact, caused me the most problem, because I was very confident that they had everything right.
4.6.4. Logging
Logging is really useful. I turned up the the log-level to the maximum
and put the log files in my /tmp
directory–I do not like my home
directory being filled with such things.
4.6.5. Advanced
Leave it alone for now.
4.7. First run
To test out my configuration, I navigate to the *Server*
buffer in
Emacs, place point over the DavMail
server entry and type O
to
open the server. Davmail
pops up a small window with instructions to
click on the Office365 link. Doing so makes my default browser,
firefox
, open a new tab where I login to Office365 using Duo's
MFA. Until the end of the login, the process appears to be the same as
if I were logging into web-mail. However, the login redirects to a
blank webpage. I copy that page's url and paste that back into the
waiting davmail
window, then click Send
.
It works!
DavMail
begins to index my mail while Gnus
waits. I have
read that this step can be slow, so I go do some other things and when
I return, I have working email.
And yes, this was more like my tenth or fifteenth run, not my first.
5. Review of other sites
Here is my opinion of advice found on several sites that one might find by searching the internet.
5.1. Blinchen.org
The page https://blog.binchen.org/posts/how-to-get-email-from-exchange-server-without-outlook.html is out-of-date and should be avoided.
5.2. Davmail mailing list
This thread was useful in alerting me to performance issues with
davmail
. But, the settings that are posted there (where clientid
is set), contributed to my thinking that I should be using the
clientid
of my employer. That was a mistake.
5.3. UVa Physics
This webpage seemed like it was matched perfectly to my situation (UVa
uses Duo to do MFA, too). So, I had to read my log files very closely
to realize that the advice to set the clientid
was wrong for my
needs.
The related page is out-of-date, too, since Thunderbird no longer
needs to use a shim like davmail
.
5.4. Emacs-devel
Roland Winkler initiated a long discussion on the Emacs devel mailing list in August 2021. The discussion eventually petered out without leading to any conclusions, but it is informative.
5.5. Northwestern IT
The Thunderbird configuration on Northwestern's site is correct and enabled me to painlessly configure Thunderbird.
6. Update
I struggled with the slowness of davmail
for quite a while (and still do). Here are a few things I have changed.
6.1. Advanced configuration
In the Advanced tab (see 4.6.5), tick the box Use Exchange
message size in IMAP
. If this is not checked, then davmail
reads
every message to determine its size; if it is checked, then davmail
only needs to read the Exchange header for each message. This speeds
things up quite a bit.
6.2. Forward mail to a third-party provider
Eventually, even with the speed-up mentioned, I gave up trying to use
davmail
to read email (imap). I have since set-up a paid account on
a third-party email provider that does not use oauth2 and forward my
email to it. I still use davmail
to send outgoing mail through
Exchange. This meant changing the gnus-posting-styles
(see 4.4).
Admittedly, this remains a somewhat painful experience, because Gnus
start-up time is still quite long due to the davmail
(and Exchange)
bottleneck. But, after that, the experience is much like using Gmail
with Gnus
.