<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Dejice Jacob: University of Glasgow (Posts about mutt)</title><link>http://www.dcs.gla.ac.uk/~jacobd/</link><description></description><atom:link href="http://www.dcs.gla.ac.uk/~jacobd/categories/mutt.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2024 &lt;a href="mailto:dejice.jacob@glasgow.ac.uk"&gt;Dejice Jacob&lt;/a&gt; </copyright><lastBuildDate>Mon, 18 Nov 2024 11:10:30 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Configure Mutt to work with OAuth 2.0</title><link>http://www.dcs.gla.ac.uk/~jacobd/posts/2022/03/configure-mutt-to-work-with-oauth-20/</link><dc:creator>Dejice Jacob</dc:creator><description>&lt;p&gt;My email client of choice is &lt;a href="https://gitlab.com/muttmua/mutt"&gt;&lt;em&gt;mutt&lt;/em&gt;&lt;/a&gt;. The keyboard short-cuts are
ingrained into muscle memory. I have tried to use complex passwords to keep myself secure. While that
may help, many large web service providers require the use of &lt;a href="https://oauth.net/2"&gt;OAuth 2.0&lt;/a&gt; for better security. &lt;/p&gt;
&lt;p&gt;&lt;img src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/a1/Mutt.png/320px-Mutt.png" title="Mutt mail user agent (MUA)" alt="Mutt mail user agent (MUA)"&gt;
&lt;/p&gt;
&lt;p&gt;The fine &lt;em&gt;defenders of the galaxy&lt;/em&gt; at &lt;a href="https://www.gla.ac.uk/it"&gt;University of Glasgow IT&lt;/a&gt; 
started pushing for better security to access email and &lt;em&gt;office365&lt;/em&gt; applications.  If Computer Scientists
do not lead the way on these things, what hope is there for the rest of digital society? &lt;/p&gt;
&lt;h4&gt;Introduction&lt;/h4&gt;
&lt;p&gt;&lt;a href="https://www.vanormondt.net/~peter/"&gt;Peter van Ormondt&lt;/a&gt; wrote a simplified (&lt;em&gt;for dummies&lt;/em&gt;) 
&lt;a href="https://www.vanormondt.net/~peter/blog/2021-03-16-mutt-office365-mfa.html"&gt;guide&lt;/a&gt; that even I could understand. 
It worked first time on &lt;em&gt;Outlook365&lt;/em&gt; and &lt;em&gt;GMail&lt;/em&gt;. On GMail, I can now turn off "Less Secure Apps"&lt;sup id="fnref:lesssecure"&gt;&lt;a class="footnote-ref" href="http://www.dcs.gla.ac.uk/~jacobd/posts/2022/03/configure-mutt-to-work-with-oauth-20/#fn:lesssecure"&gt;1&lt;/a&gt;&lt;/sup&gt;
that annoyingly keeps getting switched off, if the method is not utilised regularly. &lt;/p&gt;
&lt;p&gt;A better understanding of how OAuth 2.0 has been implemented is explained in the OAuth 2.0 
&lt;a href="https://gitlab.com/muttmua/mutt/-/blob/master/contrib/mutt_oauth2.py.README"&gt;documentation&lt;/a&gt; found in the mutt repo. 
While Mutt has native &lt;a href="http://www.mutt.org/doc/manual/#oauth"&gt;OAuth2 support&lt;/a&gt;, it provides a hook
to an external script to provide the authentication details. Conveniently, the Mutt project themselves have 
provided a Python script &lt;a href="https://gitlab.com/muttmua/mutt/-/raw/master/contrib/mutt_oauth2.py"&gt;&lt;code&gt;mutt_oauth2.py&lt;/code&gt;&lt;/a&gt;
to authorise  the user. The script keeps local state on a file which can be used to refresh the token. The 
&lt;code&gt;mutt_oauth2.py&lt;/code&gt; script keeps this encrypted using &lt;code&gt;gpg&lt;/code&gt;. &lt;/p&gt;
&lt;h4&gt;Configuration&lt;/h4&gt;
&lt;h5&gt;Configure and Authorise with &lt;a href="https://gitlab.com/muttmua/mutt/-/raw/master/contrib/mutt_oauth2.py"&gt;&lt;code&gt;mutt_oauth2.py&lt;/code&gt;&lt;/a&gt;&lt;/h5&gt;
&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Create a separate &lt;code&gt;gpg&lt;/code&gt; user to encrypt all the OAuth2 tokens for all providers. You could just as well re-use
one of your other GPG keys for this. &lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;$&lt;span class="w"&gt; &lt;/span&gt;gpg&lt;span class="w"&gt; &lt;/span&gt;--gen-key&lt;span class="w"&gt; &lt;/span&gt;
GnuPG&lt;span class="w"&gt; &lt;/span&gt;needs&lt;span class="w"&gt; &lt;/span&gt;to&lt;span class="w"&gt; &lt;/span&gt;construct&lt;span class="w"&gt; &lt;/span&gt;a&lt;span class="w"&gt; &lt;/span&gt;user&lt;span class="w"&gt; &lt;/span&gt;ID&lt;span class="w"&gt; &lt;/span&gt;to&lt;span class="w"&gt; &lt;/span&gt;identify&lt;span class="w"&gt; &lt;/span&gt;your&lt;span class="w"&gt; &lt;/span&gt;key.

Real&lt;span class="w"&gt; &lt;/span&gt;name:&lt;span class="w"&gt; &lt;/span&gt;My&lt;span class="w"&gt; &lt;/span&gt;OAuth2&lt;span class="w"&gt; &lt;/span&gt;Token&lt;span class="w"&gt; &lt;/span&gt;Encryption&lt;span class="w"&gt; &lt;/span&gt;Key
Email&lt;span class="w"&gt; &lt;/span&gt;address:&lt;span class="w"&gt; &lt;/span&gt;token.encryptor@oauth2.me
You&lt;span class="w"&gt; &lt;/span&gt;selected&lt;span class="w"&gt; &lt;/span&gt;this&lt;span class="w"&gt; &lt;/span&gt;USER-ID:
&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="s2"&gt;"My OAuth2 Token Encryption Key &amp;lt;token.encryptor@oauth2.me&amp;gt;"&lt;/span&gt;

Change&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;N&lt;span class="o"&gt;)&lt;/span&gt;ame,&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;E&lt;span class="o"&gt;)&lt;/span&gt;mail,&lt;span class="w"&gt; &lt;/span&gt;or&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;O&lt;span class="o"&gt;)&lt;/span&gt;kay/&lt;span class="o"&gt;(&lt;/span&gt;Q&lt;span class="o"&gt;)&lt;/span&gt;uit?&lt;span class="w"&gt; &lt;/span&gt;O
We&lt;span class="w"&gt; &lt;/span&gt;need&lt;span class="w"&gt; &lt;/span&gt;to&lt;span class="w"&gt; &lt;/span&gt;generate&lt;span class="w"&gt; &lt;/span&gt;a&lt;span class="w"&gt; &lt;/span&gt;lot&lt;span class="w"&gt; &lt;/span&gt;of&lt;span class="w"&gt; &lt;/span&gt;random&lt;span class="w"&gt; &lt;/span&gt;bytes.&lt;span class="w"&gt; &lt;/span&gt;It&lt;span class="w"&gt; &lt;/span&gt;is&lt;span class="w"&gt; &lt;/span&gt;a&lt;span class="w"&gt; &lt;/span&gt;good&lt;span class="w"&gt; &lt;/span&gt;idea&lt;span class="w"&gt; &lt;/span&gt;to&lt;span class="w"&gt; &lt;/span&gt;perform
some&lt;span class="w"&gt; &lt;/span&gt;other&lt;span class="w"&gt; &lt;/span&gt;action&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;type&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;on&lt;span class="w"&gt; &lt;/span&gt;the&lt;span class="w"&gt; &lt;/span&gt;keyboard,&lt;span class="w"&gt; &lt;/span&gt;move&lt;span class="w"&gt; &lt;/span&gt;the&lt;span class="w"&gt; &lt;/span&gt;mouse,&lt;span class="w"&gt; &lt;/span&gt;utilize&lt;span class="w"&gt; &lt;/span&gt;the
disks&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;during&lt;span class="w"&gt; &lt;/span&gt;the&lt;span class="w"&gt; &lt;/span&gt;prime&lt;span class="w"&gt; &lt;/span&gt;generation&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;this&lt;span class="w"&gt; &lt;/span&gt;gives&lt;span class="w"&gt; &lt;/span&gt;the&lt;span class="w"&gt; &lt;/span&gt;random&lt;span class="w"&gt; &lt;/span&gt;number
generator&lt;span class="w"&gt; &lt;/span&gt;a&lt;span class="w"&gt; &lt;/span&gt;better&lt;span class="w"&gt; &lt;/span&gt;chance&lt;span class="w"&gt; &lt;/span&gt;to&lt;span class="w"&gt; &lt;/span&gt;gain&lt;span class="w"&gt; &lt;/span&gt;enough&lt;span class="w"&gt; &lt;/span&gt;entropy.
&lt;/pre&gt;&lt;/div&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Download and install &lt;a href="https://gitlab.com/muttmua/mutt/-/blob/master/contrib/mutt_oauth2.py"&gt;&lt;code&gt;mutt_oauth2.py&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;$&lt;span class="w"&gt; &lt;/span&gt;curl&lt;span class="w"&gt; &lt;/span&gt;-o&lt;span class="w"&gt; &lt;/span&gt;/any-path/mutt_oauth2.py&lt;span class="w"&gt; &lt;/span&gt;https://gitlab.com/muttmua/mutt/-/raw/master/contrib/mutt_oauth2.py?inline&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nb"&gt;false&lt;/span&gt;
$&lt;span class="w"&gt; &lt;/span&gt;chmod&lt;span class="w"&gt; &lt;/span&gt;u+x&lt;span class="w"&gt; &lt;/span&gt;/any-path/mutt_oauth2.py
&lt;/pre&gt;&lt;/div&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Replace &lt;code&gt;YOUR_GPG_IDENTITY&lt;/code&gt; with your &lt;em&gt;GPG&lt;/em&gt; key in 
&lt;a href="https://gitlab.com/muttmua/mutt/-/blob/master/contrib/mutt_oauth2.py#L47"&gt;&lt;code&gt;mutt_oauth2.py&lt;/code&gt;&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Our setup masquerades as the &lt;strong&gt;Mozilla Thunderbird&lt;/strong&gt; e-mail client and utilizes their &lt;a href="https://hg.mozilla.org/comm-central/file/tip/mailnews/base/src/OAuth2Providers.jsm"&gt;&lt;code&gt;client-id&lt;/code&gt; and &lt;code&gt;client-secret&lt;/code&gt;&lt;/a&gt; 
   which is hard-coded into the client. The client secret is actually within the open-source repos of the Thunderbird client.
   You can also create your own credentials. See instructions for &lt;a href="http://www.dcs.gla.ac.uk/~jacobd/posts/2022/03/configure-mutt-to-work-with-oauth-20/#owncred"&gt;GMail&lt;/a&gt;.  The Thunderbird client registration 
   details as of today : &lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="https://hg.mozilla.org/comm-central/file/tip/mailnews/base/src/OAuth2Providers.jsm#l81"&gt;GMail&lt;/a&gt;: &lt;strong&gt;client-id&lt;/strong&gt; - 
     &lt;a href="https://hg.mozilla.org/comm-central/file/tip/mailnews/base/src/OAuth2Providers.jsm#l83"&gt;&lt;code&gt;406964657835-aq8lmia8j95dhl1a2bvharmfk3t1hgqj.apps.googleusercontent.com&lt;/code&gt;&lt;/a&gt;, 
 &lt;strong&gt;client-secret&lt;/strong&gt; - &lt;a href="https://hg.mozilla.org/comm-central/file/tip/mailnews/base/src/OAuth2Providers.jsm#l84"&gt;&lt;code&gt;kSmqreRr0qwBWJgbf5Y-PjSU&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://hg.mozilla.org/comm-central/file/tip/mailnews/base/src/OAuth2Providers.jsm#l127"&gt;Microsoft&lt;/a&gt;: &lt;strong&gt;client-id&lt;/strong&gt; - 
     &lt;a href="https://hg.mozilla.org/comm-central/file/tip/mailnews/base/src/OAuth2Providers.jsm#l129"&gt;&lt;code&gt;08162f7c-0fd2-4200-a84a-f25a4db0b584&lt;/code&gt;&lt;/a&gt;,
 &lt;strong&gt;client-secret&lt;/strong&gt; - &lt;a href="https://hg.mozilla.org/comm-central/file/tip/mailnews/base/src/OAuth2Providers.jsm#l130"&gt;&lt;code&gt;TxRBilcHdC6WGBee]fs?QR:SJ8nI[g82&lt;/code&gt;&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Intial authorisation for this client should be executed as: &lt;code&gt;/any-path/mutt_oauth2.py /any-path/oath2_token_file --authorize&lt;/code&gt;. There should be a separate file for each email provider. When requested to input &lt;em&gt;authorisation flow&lt;/em&gt; or &lt;em&gt;authentication method&lt;/em&gt;, &lt;code&gt;localhostauthcode&lt;/code&gt; will store the file in the same path. This is a one time operation and will be valid as long as the &lt;strong&gt;oauth2_token_file&lt;/strong&gt; is available. You can delete the file and do the authorisation again if you so desire. Executing this script will provide a URL to paste into a browser. After opening the link in the browser and finishing any requisite authorisation, the script will obtain a token, encrypt it with your &lt;em&gt;GPG&lt;/em&gt; key and store it locally in the path you have chosen.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h5&gt;Configure &lt;code&gt;.mutt&lt;/code&gt; config&lt;/h5&gt;
&lt;p&gt;Add the following entries to the &lt;code&gt;.mutt&lt;/code&gt; config file for &lt;strong&gt;OAUTH2&lt;/strong&gt; authentication. &lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code literal-block"&gt;set&lt;span class="w"&gt; &lt;/span&gt;imap_authenticators&lt;span class="w"&gt; &lt;/span&gt;=&lt;span class="w"&gt; &lt;/span&gt;"oauthbearer:xoauth2"
set&lt;span class="w"&gt; &lt;/span&gt;imap_oauth_refresh_command&lt;span class="w"&gt; &lt;/span&gt;=&lt;span class="w"&gt; &lt;/span&gt;"/any-path/mutt_oauth2.py&lt;span class="w"&gt; &lt;/span&gt;/any-path/oath2_token_file"
set&lt;span class="w"&gt; &lt;/span&gt;smtp_authenticators&lt;span class="w"&gt; &lt;/span&gt;=&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cp"&gt;${&lt;/span&gt;&lt;span class="n"&gt;imap_authenticators&lt;/span&gt;&lt;span class="cp"&gt;}&lt;/span&gt;
set&lt;span class="w"&gt; &lt;/span&gt;smtp_oauth_refresh_command&lt;span class="w"&gt; &lt;/span&gt;=&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="cp"&gt;${&lt;/span&gt;&lt;span class="n"&gt;imap_oauth_refresh_command&lt;/span&gt;&lt;span class="cp"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;xoauth2&lt;/code&gt; was the experimental authentication protocol which got standardised as &lt;code&gt;oauthbearer&lt;/code&gt;. However, it seems that while 
GMail seems to work with &lt;code&gt;oauthbearer&lt;/code&gt;, Microsoft still requires &lt;code&gt;xoauth2&lt;/code&gt;. &lt;/p&gt;
&lt;h5&gt;&lt;a name="owncred"&gt;&lt;/a&gt; Roll your own Client credentials for GMail&lt;/h5&gt;
&lt;ol&gt;
&lt;li&gt;Login to your google account, and navigate to your developer console to generate 
&lt;a href="https://console.cloud.google.com/apis/credentials"&gt;OAuth 2.0 credentials&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Navigate to the "&lt;em&gt;Credentials&lt;/em&gt;" page and click on &lt;code&gt;+ Create Credentials&lt;/code&gt; to create an OAuth client ID. &lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;This &lt;code&gt;client-ID&lt;/code&gt; and &lt;code&gt;client-secret&lt;/code&gt; can then be used in the &lt;code&gt;mutt_oauth2.py&lt;/code&gt; script. &lt;/p&gt;
&lt;div class="footnote"&gt;
&lt;hr&gt;
&lt;ol&gt;
&lt;li id="fn:lesssecure"&gt;
&lt;p&gt;I fear the long term consequences of cajoling users by using manipulative language 
such as this.  It will cause a loss of credibility for experts in the long run. 
Regaining trust that is once lost crying "Wolf" will only happen after a period 
of difficult consequences for both expert and layperson. &lt;a class="footnote-backref" href="http://www.dcs.gla.ac.uk/~jacobd/posts/2022/03/configure-mutt-to-work-with-oauth-20/#fnref:lesssecure" title="Jump back to footnote 1 in the text"&gt;↩&lt;/a&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;/div&gt;</description><category>2FA</category><category>mutt</category><category>oauth2</category><category>security</category><guid>http://www.dcs.gla.ac.uk/~jacobd/posts/2022/03/configure-mutt-to-work-with-oauth-20/</guid><pubDate>Fri, 04 Mar 2022 16:53:39 GMT</pubDate></item></channel></rss>