1 ;;; oauth2.el --- OAuth 2.0 Authorization Protocol
3 ;; Copyright (C) 2011 Free Software Foundation, Inc
5 ;; Author: Julien Danjou <julien@danjou.info>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; Implementation of the OAuth 2.0 draft.
28 ;; The main entry point is `oauth2-auth-and-store' which will return a token
29 ;; structure. This token structure can be then used with
30 ;; `oauth2-url-retrieve-synchronously' to retrieve any data that need OAuth
31 ;; authentication to be accessed.
33 ;; If the token needs to be refreshed, the code handles it automatically and
34 ;; store the new value of the access token.
41 (defun oauth2-request-authorization (auth-url client-id &optional scope state)
42 "Request OAuth authorization at AUTH-URL by launching `browse-url'.
43 CLIENT-ID is the client id provided by the provider.
44 It returns the code provided by the service."
45 (browse-url (concat auth-url
46 (if (string-match-p "\?" auth-url) "&" "?")
47 "client_id=" (url-hexify-string client-id)
48 "&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob"
49 (if scope (concat "&scope=" (url-hexify-string scope)) "")
50 (if state (concat "&state=" (url-hexify-string state)) "")))
51 (read-string "Enter the code your browser displayed: "))
53 (defun oauth2-request-access-parse ()
54 "Parse the result of an OAuth request."
55 (goto-char (point-min))
56 (when (search-forward-regexp "^$" nil t)
59 (defun oauth2-make-access-request (url data)
60 "Make an access request to URL using DATA in POST."
61 (let ((url-request-method "POST")
62 (url-request-data data)
63 (url-request-extra-headers
64 '(("Content-Type" . "application/x-www-form-urlencoded"))))
65 (with-current-buffer (url-retrieve-synchronously url)
66 (let ((data (oauth2-request-access-parse)))
67 (kill-buffer (current-buffer))
70 (defstruct oauth2-token
79 (defun oauth2-request-access (token-url client-id client-secret code)
80 "Request OAuth access at TOKEN-URL.
81 The CODE should be obtained with `oauth2-request-authorization'.
82 Return an `oauth2-token' structure."
85 (oauth2-make-access-request
88 "client_id=" client-id
89 "&client_secret=" client-secret
91 "&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code"))))
92 (make-oauth2-token :client-id client-id
93 :client-secret client-secret
94 :access-token (aget result 'access_token)
95 :refresh-token (aget result 'refresh_token)
96 :token-url token-url))))
99 (defun oauth2-refresh-access (token)
100 "Refresh OAuth access TOKEN.
101 TOKEN should be obtained with `oauth2-request-access'."
102 (setf (oauth2-token-access-token token)
103 (aget (oauth2-make-access-request
104 (oauth2-token-token-url token)
105 (concat "client_id=" (oauth2-token-client-id token)
106 "&client_secret=" (oauth2-token-client-secret token)
107 "&refresh_token=" (oauth2-token-refresh-token token)
108 "&grant_type=refresh_token"))
110 ;; If the token has a plstore, update it
111 (let ((plstore (oauth2-token-plstore token)))
113 (plstore-put plstore (oauth2-token-plstore-id token)
115 ,(oauth2-token-access-token token)
117 ,(oauth2-token-refresh-token token)))
118 (plstore-save plstore)))
122 (defun oauth2-auth (auth-url token-url client-id client-secret &optional scope state)
123 "Authenticate application via OAuth2."
124 (oauth2-request-access
128 (oauth2-request-authorization
129 auth-url client-id scope state)))
131 (defcustom oauth2-token-file (concat user-emacs-directory "oauth2.plstore")
132 "File path where store OAuth tokens."
136 (defun oauth2-compute-id (auth-url token-url resource-url)
137 "Compute an unique id based on URLs.
138 This allows to store the token in an unique way."
139 (secure-hash 'md5 (concat auth-url token-url resource-url)))
142 (defun oauth2-auth-and-store (auth-url token-url resource-url client-id client-secret)
143 "Request access to a resource and store it using `plstore'."
144 ;; We store a MD5 sum of all URL
145 (let* ((plstore (plstore-open oauth2-token-file))
146 (id (oauth2-compute-id auth-url token-url resource-url))
147 (plist (cdr (plstore-get plstore id))))
148 ;; Check if we found something matching this access
150 ;; We did, return the token object
151 (make-oauth2-token :plstore plstore
154 :client-secret client-secret
155 :access-token (plist-get plist :access-token)
156 :refresh-token (plist-get plist :refresh-token)
157 :token-url token-url)
158 (let ((token (oauth2-auth auth-url token-url
159 client-id client-secret resource-url)))
161 (setf (oauth2-token-plstore token) plstore)
162 (setf (oauth2-token-plstore-id token) id)
163 (plstore-put plstore id nil `(:access-token
164 ,(oauth2-token-access-token token)
166 ,(oauth2-token-refresh-token token)))
167 (plstore-save plstore)
170 (defun oauth2-url-append-access-token (token url)
171 "Append access token to URL."
173 (if (string-match-p "\?" url) "&" "?")
174 "access_token=" (oauth2-token-access-token token)))
176 ;; Local variable from `url'
177 ;; defined here to avoid compile warning
181 (defun oauth2-url-retrieve-synchronously (token url &optional request-method request-data request-extra-headers)
182 "Retrieve an URL synchronously using TOKENS to access it.
183 TOKENS can be obtained with `oauth2-auth'."
184 (let (tokens-need-renew)
185 (flet ((url-http-handle-authentication (proxy)
186 (setq tokens-need-renew t)
187 ;; This is to make `url' think
190 (let ((url-request-method request-method)
191 (url-request-data request-data)
192 (url-request-extra-headers request-extra-headers)
194 (setq url-buffer (url-retrieve-synchronously
195 (url-generic-parse-url
196 (oauth2-url-append-access-token token url))))
197 (if tokens-need-renew
198 (oauth2-url-retrieve-synchronously (oauth2-refresh-access token) url request-method request-data request-extra-headers)
203 ;;; oauth2.el ends here