]> code.delx.au - gnu-emacs-elpa/blob - packages/oauth2/oauth2.el
* packages/oauth2/oauth2.el: Revert fix URL double escaping, update to 0.5
[gnu-emacs-elpa] / packages / oauth2 / oauth2.el
1 ;;; oauth2.el --- OAuth 2.0 Authorization Protocol
2
3 ;; Copyright (C) 2011-2012 Free Software Foundation, Inc
4
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Version: 0.5
7 ;; Keywords: comm
8
9 ;; This file is part of GNU Emacs.
10
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.
15
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.
20
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/>.
23
24 ;;; Commentary:
25
26 ;; Implementation of the OAuth 2.0 draft.
27 ;;
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.
32 ;;
33 ;; If the token needs to be refreshed, the code handles it automatically and
34 ;; store the new value of the access token.
35
36 ;;; Code:
37
38 (require 'plstore)
39 (require 'json)
40
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: "))
52
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)
57 (json-read)))
58
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))
68 data))))
69
70 (defstruct oauth2-token
71 plstore
72 plstore-id
73 client-id
74 client-secret
75 access-token
76 refresh-token
77 token-url)
78
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."
83 (when code
84 (let ((result
85 (oauth2-make-access-request
86 token-url
87 (concat
88 "client_id=" client-id
89 "&client_secret=" client-secret
90 "&code=" code
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 (cdr (assoc 'access_token result))
95 :refresh-token (cdr (assoc 'refresh_token result))
96 :token-url token-url))))
97
98 ;;;###autoload
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 (cdr (assoc 'access_token
104 (oauth2-make-access-request
105 (oauth2-token-token-url token)
106 (concat "client_id=" (oauth2-token-client-id token)
107 "&client_secret=" (oauth2-token-client-secret token)
108 "&refresh_token=" (oauth2-token-refresh-token token)
109 "&grant_type=refresh_token")))))
110 ;; If the token has a plstore, update it
111 (let ((plstore (oauth2-token-plstore token)))
112 (when plstore
113 (plstore-put plstore (oauth2-token-plstore-id token)
114 nil `(:access-token
115 ,(oauth2-token-access-token token)
116 :refresh-token
117 ,(oauth2-token-refresh-token token)))
118 (plstore-save plstore)))
119 token)
120
121 ;;;###autoload
122 (defun oauth2-auth (auth-url token-url client-id client-secret &optional scope state)
123 "Authenticate application via OAuth2."
124 (oauth2-request-access
125 token-url
126 client-id
127 client-secret
128 (oauth2-request-authorization
129 auth-url client-id scope state)))
130
131 (defcustom oauth2-token-file (concat user-emacs-directory "oauth2.plstore")
132 "File path where store OAuth tokens."
133 :group 'oauth2
134 :type 'file)
135
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)))
140
141 ;;;###autoload
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
149 (if plist
150 ;; We did, return the token object
151 (make-oauth2-token :plstore plstore
152 :plstore-id id
153 :client-id client-id
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)))
160 ;; Set the plstore
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)
165 :refresh-token
166 ,(oauth2-token-refresh-token token)))
167 (plstore-save plstore)
168 token))))
169
170 (defun oauth2-url-append-access-token (token url)
171 "Append access token to URL."
172 (concat url
173 (if (string-match-p "\?" url) "&" "?")
174 "access_token=" (oauth2-token-access-token token)))
175
176 ;; Local variable from `url'
177 ;; defined here to avoid compile warning
178 (defvar success)
179
180 ;;;###autoload
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
188 ;; it's done.
189 (setq success t)))
190 (let ((url-request-method request-method)
191 (url-request-data request-data)
192 (url-request-extra-headers request-extra-headers)
193 (url-buffer))
194 (setq url-buffer (url-retrieve-synchronously
195 (oauth2-url-append-access-token token url)))
196 (if tokens-need-renew
197 (oauth2-url-retrieve-synchronously (oauth2-refresh-access token) url request-method request-data request-extra-headers)
198 url-buffer)))))
199
200 (provide 'oauth2)
201
202 ;;; oauth2.el ends here