Debian Codegraph @

debian/patches/510_xoauth2.patch

raw
1 Subject: Experimental support for IMAP/SMTP/POP with XOAUTH2
2 Author: Yoshinari Nomura <[email protected]>
3 Origin: https://github.com/yoshinari-nomura/Mew/commit/eac7b88b8ab091eea53140dec0f7a76206239eaf
4 Origin: https://github.com/tats/Mew/tree/feature/xoauth2
5
6 cf. https://groups.google.com/g/mew-ja/c/Hn9L27ll-eY
7
8 This patch is just tested with:
9 + Gmail SMTP/IMAP XOAUTH2, Ubuntu Linux
10 and requires oauth2.el from ELPA.
11
12 Get your own OAuth cliend-id and client-secret and
13 setup these variables in your init.el:
14
15 (setq mew-auth-oauth-client-id "xxxxxxxxxapps.googleusercontent.com"
16 mew-auth-oauth-client-secret "xxxxxxxxxxxxxxxxxxxxxxxxxx"
17 plstore-cache-passphrase-for-symmetric-encryption t
18 epg-pinentry-mode 'loopback)
19
20 You may want to give priority to XOAUTH2:
21
22 (setq mew-config-alist
23 '(
24 ("default"
25 ; ...
26 (smtp-auth-list '("XOAUTH2"))
27 (pop-auth-list '("XOAUTH2"))
28 (imap-auth-list '("XOAUTH2")))
29 ; ...
30 ))
31
32 If you want to another email service instead of Gmail, you will
33 have to change these variables:
34
35 (defvar mew-auth-oauth2-auth-url
36 "https://accounts.google.com/o/oauth2/auth"
37 "OAuth2 auth server URL.")
38
39 (defvar mew-auth-oauth2-token-url
40 "https://accounts.google.com/o/oauth2/token"
41 "OAuth2 token server URL.")
42
43 (defvar mew-auth-oauth2-resource-url
44 "https://mail.google.com/"
45 "URL used to request access to Mail Resources.")
46
47 (defvar mew-auth-oauth2-redirect-url nil
48 "URL used to OAuth redirect url.")
49
50 diff --git a/mew-auth.el b/mew-auth.el
51 index eaf6677..49d0f46 100644
52 --- a/mew-auth.el
53 +++ b/mew-auth.el
54 @@ -67,6 +67,80 @@
55 (defun mew-keyed-md5 (key passwd)
56 (mew-md5 (concat key passwd)))
57
58 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
59 +;; XOAUTH2
60 +
61 +(defun mew-auth-xoauth2-auth-string (user token)
62 + ;; base64([email protected]^Aauth=Bearer ya29vF9dft4...^A^A)
63 + (base64-encode-string (format "user=%s\1auth=Bearer %s\1\1" user token) t))
64 +
65 +(defun mew-auth-xoauth2-json-status (status-string)
66 + ;; https://developers.google.com/gmail/imap/xoauth2-protocol#error_response_2
67 + (require 'json)
68 + (let ((json-status
69 + (ignore-errors
70 + (json-read-from-string
71 + (base64-decode-string status-string)))))
72 + (if json-status
73 + (if (string-match "^2" (cdr (assoc 'status json-status)))
74 + "OK" ;; 2XX
75 + "NO" ;; XXX: Anyway NO?
76 + )
77 + "OK" ;; XXX: Maybe OK if not JSON.
78 + )))
79 +
80 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
81 +;; OAuth2
82 +
83 +(defvar mew-auth-oauth2-client-id
84 + "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com")
85 +
86 +(defvar mew-auth-oauth2-client-secret
87 + "xxxxxxxxxxxxxxxxxxxxxxxx")
88 +
89 +(defvar mew-auth-oauth2-auth-url "https://accounts.google.com/o/oauth2/auth"
90 + "OAuth2 auth server URL.")
91 +
92 +(defvar mew-auth-oauth2-token-url "https://accounts.google.com/o/oauth2/token"
93 + "OAuth2 token server URL.")
94 +
95 +(defvar mew-auth-oauth2-resource-url "https://mail.google.com/"
96 + "URL used to request access to Mail Resources.")
97 +
98 +(defvar mew-auth-oauth2-redirect-url nil
99 + "URL used to OAuth redirect url.")
100 +
101 +(declare-function oauth2-auth-and-store "oauth2")
102 +(declare-function oauth2-refresh-access "oauth2")
103 +(declare-function oauth2-token-access-token "oauth2")
104 +
105 +(defun mew-auth-oauth2-auth-and-store
106 + (resource-url client-id client-secret &optional redirect-url)
107 + "Request access to a mail resource and store it using `auth-source'."
108 + (require 'oauth2)
109 + (oauth2-auth-and-store
110 + mew-auth-oauth2-auth-url
111 + mew-auth-oauth2-token-url
112 + resource-url
113 + client-id
114 + client-secret
115 + redirect-url))
116 +
117 +(defun mew-auth-oauth2-token ()
118 + "Get OAuth token for Mew to access mail service."
119 + (require 'oauth2)
120 + (let ((token (mew-auth-oauth2-auth-and-store
121 + mew-auth-oauth2-resource-url
122 + mew-auth-oauth2-client-id
123 + mew-auth-oauth2-client-secret
124 + mew-auth-oauth2-redirect-url)))
125 + (oauth2-refresh-access token)
126 + token))
127 +
128 +(defun mew-auth-oauth2-token-access-token ()
129 + (require 'oauth2)
130 + (ignore-errors (oauth2-token-access-token (mew-auth-oauth2-token))))
131 +
132 (provide 'mew-auth)
133
134 ;;; Copyright Notice:
135 diff --git a/mew-imap.el b/mew-imap.el
136 index c07dc93..558227f 100644
137 --- a/mew-imap.el
138 +++ b/mew-imap.el
139 @@ -56,6 +56,7 @@
140 (defvar mew-imap-fsm
141 '(("greeting" ("OK" . "capability"))
142 ("capability" ("OK" . "post-capability"))
143 + ("auth-xoauth2" ("OK" . "next") ("NO" . "xoauth2-wpwd"))
144 ("auth-cram-md5" ("OK" . "pwd-cram-md5") ("NO" . "wpwd"))
145 ("pwd-cram-md5" ("OK" . "next") ("NO" . "wpwd"))
146 ("auth-login" ("OK" . "user-login") ("NO" . "wpwd"))
147 @@ -969,7 +970,8 @@
148
149 (defvar mew-imap-auth-alist
150 '(("CRAM-MD5" mew-imap-command-auth-cram-md5)
151 - ("LOGIN" mew-imap-command-auth-login)))
152 + ("LOGIN" mew-imap-command-auth-login)
153 + ("XOAUTH2" mew-imap-command-auth-xoauth2)))
154
155 (defun mew-imap-auth-get-func (auth)
156 (nth 1 (mew-assoc-case-equal auth mew-imap-auth-alist 0)))
157 @@ -1007,6 +1009,43 @@
158 (epasswd (mew-base64-encode-string passwd)))
159 (mew-imap-process-send-string2 pro epasswd)))
160
161 +;;;;;;;;;;;;;;;;
162 +;; XOAUTH2
163 +
164 +(defun mew-imap-command-auth-xoauth2 (pro pnm)
165 + (let* ((user (mew-imap-get-user pnm))
166 + (token (mew-auth-oauth2-token-access-token))
167 + (auth-string (mew-auth-xoauth2-auth-string user token)))
168 + ;; XXX: need to reset satus if token is nil.
169 + (mew-imap-process-send-string pro pnm (format "AUTHENTICATE XOAUTH2 %s" auth-string))
170 + (mew-imap-set-status pnm "auth-xoauth2")))
171 +
172 +;; XXX: defalias does not work!
173 +;; (defalias 'mew-imap2-command-auth-xoauth2 'mew-imap-command-auth-xoauth2)
174 +(defun mew-imap2-command-auth-xoauth2 (pro pnm)
175 + (let* ((user (mew-imap2-get-user pnm))
176 + (token (mew-auth-oauth2-token-access-token))
177 + (auth-string (mew-auth-xoauth2-auth-string user token)))
178 + ;; XXX: need to reset satus if token is nil.
179 + (mew-imap2-process-send-string pro pnm (format "AUTHENTICATE XOAUTH2 %s" auth-string))
180 + (mew-imap2-set-status pnm "auth-xoauth2")))
181 +
182 +(defun mew-imap-command-xoauth2-wpwd (pro pnm)
183 + (mew-imap-set-done pnm t)
184 + (mew-passwd-set-passwd (mew-imap-passtag pnm) nil)
185 + (delete-process pro)
186 + ;; XXX: Should be cared more! Clear process and filter without sending LOGOUT.
187 + (error "IMAP XOAUTH2 token is wrong!"))
188 +
189 +;; XXX: defalias does not work!
190 +;; (defalias 'mew-imap2-command-xoauth2-wpwd 'mew-imap-command-xoauth2-wpwd)
191 +(defun mew-imap2-command-xoauth2-wpwd (pro pnm)
192 + (mew-imap2-set-done pnm t)
193 + (mew-passwd-set-passwd (mew-imap2-passtag pnm) nil)
194 + (delete-process pro)
195 + ;; XXX: Should be cared more! Clear process and filter without sending LOGOUT.
196 + (error "IMAP XOAUTH2 token is wrong!"))
197 +
198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199 ;;;
200 ;;; Sub functions
201 @@ -1477,8 +1516,12 @@
202 (if (string= status "greeting")
203 (setq next (mew-imap-fsm-next "greeting" "OK"))
204 (setq stay t)))
205 - ((and (goto-char (point-min)) (looking-at "\\+"))
206 - (setq next (mew-imap-fsm-next status "OK")))
207 + ((and (goto-char (point-min)) (looking-at "\\+ *\\(.*\\)"))
208 + (setq next (mew-imap-fsm-next
209 + status
210 + (if (string= status "auth-xoauth2")
211 + (mew-auth-xoauth2-json-status (mew-match-string 1))
212 + "OK"))))
213 ((and (goto-char (point-max)) (= (forward-line -1) 0) (looking-at eos))
214 (mew-imap-set-tag pnm nil)
215 (setq code (mew-match-string 1))
216 diff --git a/mew-imap2.el b/mew-imap2.el
217 index 49b8231..c579216 100644
218 --- a/mew-imap2.el
219 +++ b/mew-imap2.el
220 @@ -37,6 +37,7 @@
221 (defvar mew-imap2-fsm
222 '(("greeting" ("OK" . "capability"))
223 ("capability" ("OK" . "post-capability"))
224 + ("auth-xoauth2" ("OK" . "next") ("NO" . "xoauth2-wpwd"))
225 ("auth-cram-md5" ("OK" . "pwd-cram-md5") ("NO" . "wpwd"))
226 ("pwd-cram-md5" ("OK" . "next") ("NO" . "wpwd"))
227 ("auth-login" ("OK" . "user-login") ("NO" . "wpwd"))
228 @@ -241,7 +242,8 @@
229
230 (defvar mew-imap2-auth-alist
231 '(("CRAM-MD5" mew-imap2-command-auth-cram-md5)
232 - ("LOGIN" mew-imap2-command-auth-login)))
233 + ("LOGIN" mew-imap2-command-auth-login)
234 + ("XOAUTH2" mew-imap2-command-auth-xoauth2)))
235
236 (defun mew-imap2-auth-get-func (auth)
237 (nth 1 (mew-assoc-case-equal auth mew-imap2-auth-alist 0)))
238 @@ -537,7 +539,7 @@ with '*' in the region are handled."
239 (buf (process-buffer process))
240 aux stay next func code resp)
241 (save-excursion
242 - (mew-imap2-debug (upcase status) string)
243 + (mew-imap2-debug (upcase (format "%s" status)) string)
244 (if (and buf (get-buffer buf)) (set-buffer buf))
245 (while (string-match "^\\*[^\n]*\n" str)
246 (setq aux (substring str 0 (match-end 0)))
247 @@ -551,9 +553,13 @@ with '*' in the region are handled."
248 (cond
249 (next
250 nil)
251 - ((string-match "^\\+" str)
252 + ((string-match "^\\+ *\\(.*\\)" str)
253 (mew-imap2-set-aux pnm str)
254 - (setq next (mew-imap2-fsm-next status "OK")))
255 + (setq next (mew-imap2-fsm-next
256 + status
257 + (if (string= status "auth-xoauth2")
258 + (mew-auth-xoauth2-json-status (mew-match-string 1))
259 + "OK"))))
260 ((string-match eos str)
261 (mew-imap2-set-tag pnm nil)
262 (setq code (mew-match-string 1 str))
263 diff --git a/mew-pop.el b/mew-pop.el
264 index 9e2db7c..75f7cb7 100644
265 --- a/mew-pop.el
266 +++ b/mew-pop.el
267 @@ -41,6 +41,7 @@
268 (defvar mew-pop-fsm
269 '(("greeting" nil ("\\+OK" . "capa"))
270 ("capa" t ("\\+OK" . "auth") ("-ERR" . "pswd"))
271 + ("xoauth2" nil ("\\+OK" . "list") ("-ERR" . "wpwd"))
272 ("auth-cram-md5" nil ("\\+OK" . "pwd-cram-md5") ("-ERR" . "wpwd"))
273 ("pwd-cram-md5" nil ("\\+OK" . "list") ("-ERR" . "wpwd"))
274 ("auth-plain" nil ("\\+OK" . "pwd-plain") ("-ERR" . "wpwd"))
275 @@ -503,11 +504,19 @@
276
277 (defvar mew-pop-auth-alist
278 '(("CRAM-MD5" mew-pop-command-auth-cram-md5)
279 - ("PLAIN" mew-pop-command-auth-plain)))
280 + ("PLAIN" mew-pop-command-auth-plain)
281 + ("XOAUTH2" mew-pop-command-auth-xoauth2)))
282
283 (defun mew-pop-auth-get-func (auth)
284 (nth 1 (mew-assoc-case-equal auth mew-pop-auth-alist 0)))
285
286 +(defun mew-pop-command-auth-xoauth2 (pro pnm)
287 + (let* ((user (mew-pop-get-user pnm))
288 + (token (mew-auth-oauth2-token-access-token))
289 + (auth-string (mew-auth-xoauth2-auth-string user token)))
290 + (mew-pop-process-send-string pro "AUTH XOAUTH2 %s" auth-string)
291 + (mew-smtp-set-status pnm "auth-xoauth2")))
292 +
293 (defun mew-pop-command-auth-cram-md5 (pro pnm)
294 (mew-pop-process-send-string pro "AUTH CRAM-MD5")
295 (mew-pop-set-status pnm "auth-cram-md5"))
296 diff --git a/mew-smtp.el b/mew-smtp.el
297 index e3dbbdc..a4ea807 100644
298 --- a/mew-smtp.el
299 +++ b/mew-smtp.el
300 @@ -48,6 +48,10 @@
301 ("user-login" ("334" . "pwd-login") (t . "wpwd"))
302 ("pwd-login" ("235" . "next") (t . "wpwd"))
303 ("auth-plain" ("235" . "next") (t . "wpwd"))
304 + ;; This is checked only for Gmail https://developers.google.com/gmail/imap/xoauth2-protocol
305 + ;; XXX: MS Exchange Returns 334 like CRAM-MD5?
306 + ;; https://docs.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth
307 + ("auth-xoauth2" ("235" . "next") (t . "wpwd"))
308 ;; See blow
309 ;; ("auth-plain" ("334" . "pwd-plain") (t . "wpwd"))
310 ;; ("pwd-plain" ("235" . "next") (t . "wpwd"))
311 @@ -249,7 +253,8 @@
312 (defvar mew-smtp-auth-alist
313 '(("CRAM-MD5" mew-smtp-command-auth-cram-md5) ;; RFC 2195
314 ("PLAIN" mew-smtp-command-auth-plain) ;; RFC 2595
315 - ("LOGIN" mew-smtp-command-auth-login))) ;; No spec
316 + ("LOGIN" mew-smtp-command-auth-login) ;; No spec
317 + ("XOAUTH2" mew-smtp-command-auth-xoauth2)))
318
319 (defun mew-smtp-auth-get-func (auth)
320 (nth 1 (mew-assoc-case-equal auth mew-smtp-auth-alist 0)))
321 @@ -308,6 +313,13 @@
322 (mew-smtp-process-send-string pro "AUTH PLAIN %s" plain)
323 (mew-smtp-set-status pnm "auth-plain")))
324
325 +(defun mew-smtp-command-auth-xoauth2 (pro pnm)
326 + (let* ((user (mew-smtp-get-auth-user pnm))
327 + (token (mew-auth-oauth2-token-access-token))
328 + (auth-string (mew-auth-xoauth2-auth-string user token)))
329 + (mew-smtp-process-send-string pro "AUTH XOAUTH2 %s" auth-string)
330 + (mew-smtp-set-status pnm "auth-xoauth2")))
331 +
332 ;; (defun mew-smtp-command-auth-plain (pro pnm)
333 ;; (mew-smtp-process-send-string pro "AUTH PLAIN")
334 ;; (mew-smtp-set-status pnm "auth-plain"))
335 diff --git a/mew-vars.el b/mew-vars.el
336 index 3fa1af3..9801229 100644
337 --- a/mew-vars.el
338 +++ b/mew-vars.el
339 @@ -619,9 +619,9 @@ your e-mail address is automatically set"
340
341 (defcustom mew-smtp-auth-list '("CRAM-MD5" "PLAIN" "LOGIN")
342 "*A list of SMTP AUTH methods in the preferred order.
343 -Currently, \"CRAM-MD5\", \"PLAIN\", and \"LOGIN\" can be used."
344 +Currently, \"CRAM-MD5\", \"PLAIN\", \"LOGIN\", and \"XOAUTH2\" can be used."
345 :group 'mew-smtp
346 - :type '(repeat (choice (const "CRAM-MD5") (const "PLAIN") (const "LOGIN"))))
347 + :type '(repeat (choice (const "CRAM-MD5") (const "PLAIN") (const "LOGIN") (const "XOAUTH2"))))
348
349 (defcustom mew-smtp-helo-domain "localhost"
350 "*An e-mail domain to tell a SMTP server with HELO/EHLO."
351 @@ -736,9 +736,9 @@ t means SASL according to 'mew-pop-auth-list'."
352
353 (defcustom mew-pop-auth-list '("CRAM-MD5" "PLAIN")
354 "*A list of SASL methods in the preferred order.
355 -Currently, \"CRAM-MD5\" can be used."
356 +Currently, \"CRAM-MD5\", \"PLAIN\", and \"XOAUTH2\" can be used."
357 :group 'mew-pop
358 - :type '(repeat (choice (const "CRAM-MD5") (const "PLAIN"))))
359 + :type '(repeat (choice (const "CRAM-MD5") (const "PLAIN") (const "XOAUTH2"))))
360
361 (defcustom mew-pop-delete t
362 "*Whether or not delete messages on a POP server after retrieval by
363 @@ -826,11 +826,11 @@ t means SASL according to 'mew-imap-auth-list'."
364 :group 'mew-imap
365 :type 'boolean)
366
367 -(defcustom mew-imap-auth-list '("CRAM-MD5" "LOGIN")
368 +(defcustom mew-imap-auth-list '("CRAM-MD5" "LOGIN")
369 "*A list of SASL methods in the preferred order.
370 -Currently, \"CRAM-MD5\" and \"LOGIN\" can be used."
371 +Currently, \"CRAM-MD5\", \"LOGIN\", and \"XOAUTH2\" can be used."
372 :group 'mew-imap
373 - :type '(repeat (choice (const "CRAM-MD5") (const "LOGIN"))))
374 + :type '(repeat (choice (const "CRAM-MD5") (const "LOGIN") (const "XOAUTH2"))))
375
376 (defcustom mew-imap-delete t
377 "*Whether or not delete messages on an IMAP server after retrieval by