Debian Codegraph @

mew-auth.el

raw ◀ ▶
1 Debian patch touch this file
1 ;;; mew-auth.el
2
3 ;; Author: Kazu Yamamoto <[email protected]>
4 ;; Created: Aug 17, 1999
5
6 ;;; Code:
7
8 (require 'mew)
9
10 (defun mew-auth-select (grt lst)
11 (mew-auth-select2 (mew-split grt mew-sp) lst))
12
13 (defun mew-auth-select2 (auths lst)
14 (let (n preference strongest)
15 (dolist (auth auths)
16 (setq n (mew-member-case-equal auth lst))
17 (cond
18 ((null n) ())
19 ((null preference)
20 (setq preference n)
21 (setq strongest auth))
22 ((< n preference)
23 (setq preference n)
24 (setq strongest auth))))
25 strongest))
26
27 (defun mew-md5-raw (str)
28 (let* ((md5str (mew-md5 str))
29 (len (length md5str))
30 (md5raw (make-string (/ len 2) 0))
31 (i 0) (j 0))
32 (while (< i len)
33 (aset md5raw j (+ (* (mew-hexchar-to-int (aref md5str i)) 16)
34 (mew-hexchar-to-int (aref md5str (1+ i)))))
35 (setq i (+ i 2))
36 (setq j (1+ j)))
37 md5raw))
38
39 ;; (mew-hmac-md5 "what do ya want for nothing?" "Jefe")
40 ;; -> 0x750c783e6ab0b503eaa86e310a5db738
41
42 (defun mew-hmac-md5 (message key)
43 "HMAC-MD5 defined in RFC 2104"
44 (let* ((keylen (length key))
45 (ipad 54) ;; 0x36
46 (opad 92) ;; 0x5c
47 (ikey (make-string 64 0))
48 okey digest)
49 (when (< keylen 64)
50 (dotimes (i keylen)
51 (aset ikey i (aref key i))))
52 (setq okey (copy-sequence ikey))
53 (dotimes (i 64)
54 (aset ikey i (logxor (aref ikey i) ipad))
55 (aset okey i (logxor (aref okey i) opad)))
56 (setq digest (mew-md5-raw (concat ikey message)))
57 (mew-md5 (concat okey digest))))
58
59 (defun mew-cram-md5 (user passwd b64-challenge)
60 "CRAM-MD5 defined in RFC 2195"
61 (let* ((challenge (mew-base64-decode-string b64-challenge))
62 (response (mew-hmac-md5 challenge passwd)))
63 (mew-base64-encode-string (format "%s %s" user response))))
64
65 ;;
66
67 (defun mew-keyed-md5 (key passwd)
68 (mew-md5 (concat key passwd)))
69
•70 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
71 ;; XOAUTH2
72
73 (defun mew-auth-xoauth2-auth-string (user token)
74 ;; base64([email protected]^Aauth=Bearer ya29vF9dft4...^A^A)
75 (base64-encode-string (format "user=%s\1auth=Bearer %s\1\1" user token) t))
76
77 (defun mew-auth-xoauth2-json-status (status-string)
78 ;; https://developers.google.com/gmail/imap/xoauth2-protocol#error_response_2
79 (require 'json)
80 (let ((json-status
81 (ignore-errors
82 (json-read-from-string
83 (base64-decode-string status-string)))))
84 (if json-status
85 (if (string-match "^2" (cdr (assoc 'status json-status)))
86 "OK" ;; 2XX
87 "NO" ;; XXX: Anyway NO?
88 )
89 "OK" ;; XXX: Maybe OK if not JSON.
90 )))
91
92 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
93 ;; OAuth2
94
95 (defvar mew-auth-oauth2-client-id
96 "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.apps.googleusercontent.com")
97
98 (defvar mew-auth-oauth2-client-secret
99 "xxxxxxxxxxxxxxxxxxxxxxxx")
100
101 (defvar mew-auth-oauth2-auth-url "https://accounts.google.com/o/oauth2/auth"
102 "OAuth2 auth server URL.")
103
104 (defvar mew-auth-oauth2-token-url "https://accounts.google.com/o/oauth2/token"
105 "OAuth2 token server URL.")
106
107 (defvar mew-auth-oauth2-resource-url "https://mail.google.com/"
108 "URL used to request access to Mail Resources.")
109
110 (defvar mew-auth-oauth2-redirect-url nil
111 "URL used to OAuth redirect url.")
112
113 (declare-function oauth2-auth-and-store "oauth2")
114 (declare-function oauth2-refresh-access "oauth2")
115 (declare-function oauth2-token-access-token "oauth2")
116
117 (defun mew-auth-oauth2-auth-and-store
118 (resource-url client-id client-secret &optional redirect-url)
119 "Request access to a mail resource and store it using `auth-source'."
120 (require 'oauth2)
121 (oauth2-auth-and-store
122 mew-auth-oauth2-auth-url
123 mew-auth-oauth2-token-url
124 resource-url
125 client-id
126 client-secret
127 redirect-url))
128
129 (defun mew-auth-oauth2-token ()
130 "Get OAuth token for Mew to access mail service."
131 (require 'oauth2)
132 (let ((token (mew-auth-oauth2-auth-and-store
133 mew-auth-oauth2-resource-url
134 mew-auth-oauth2-client-id
135 mew-auth-oauth2-client-secret
136 mew-auth-oauth2-redirect-url)))
137 (oauth2-refresh-access token)
138 token))
139
140 (defun mew-auth-oauth2-token-access-token ()
141 (require 'oauth2)
142 (ignore-errors (oauth2-token-access-token (mew-auth-oauth2-token))))
143
144 (provide 'mew-auth)
145
146 ;;; Copyright Notice:
147
148 ;; Copyright (C) 2000-2015 Mew developing team.
149 ;; All rights reserved.
150
151 ;; Redistribution and use in source and binary forms, with or without
152 ;; modification, are permitted provided that the following conditions
153 ;; are met:
154 ;;
155 ;; 1. Redistributions of source code must retain the above copyright
156 ;; notice, this list of conditions and the following disclaimer.
157 ;; 2. Redistributions in binary form must reproduce the above copyright
158 ;; notice, this list of conditions and the following disclaimer in the
159 ;; documentation and/or other materials provided with the distribution.
160 ;; 3. Neither the name of the team nor the names of its contributors
161 ;; may be used to endorse or promote products derived from this software
162 ;; without specific prior written permission.
163 ;;
164 ;; THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
165 ;; ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
166 ;; IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
167 ;; PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
168 ;; LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
169 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
170 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
171 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
172 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
173 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
174 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
175
176 ;;; mew-auth.el ends here