]> code.delx.au - gnu-emacs-elpa/blob - packages/oauth2/oauth2.el
7966e629b245acd3635348f509c1f297682e2e3c
[gnu-emacs-elpa] / packages / oauth2 / oauth2.el
1 ;;; oauth2.el --- OAuth 2.0 Authorization Protocol
2
3 ;; Copyright (C) 2011 Free Software Foundation, Inc
4
5 ;; Author: Julien Danjou <julien@danjou.info>
6 ;; Version: 0.1
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
40 (defun oauth2-request-authorization (auth-url client-id &optional scope state)
41 "Request OAuth authorization at AUTH-URL by launching `browse-url'.
42 CLIENT-ID is the client id provided by the provider.
43 It returns the code provided by the service."
44 (browse-url (concat auth-url
45 (if (string-match-p "\?" auth-url) "&" "?")
46 "client_id=" (url-hexify-string client-id)
47 "&response_type=code&redirect_uri=urn:ietf:wg:oauth:2.0:oob"
48 (if scope (concat "&scope=" (url-hexify-string scope)) "")
49 (if state (concat "&state=" (url-hexify-string state)) "")))
50 (read-string "Enter the code your browser displayed: "))
51
52 (defun oauth2-request-access-parse ()
53 "Parse the result of an OAuth request."
54 (goto-char (point-min))
55 (when (search-forward-regexp "^$" nil t)
56 (json-read)))
57
58 (defun oauth2-make-access-request (url data)
59 "Make an access request to URL using DATA in POST."
60 (let ((url-request-method "POST")
61 (url-request-data data)
62 (url-request-extra-headers
63 '(("Content-Type" . "application/x-www-form-urlencoded"))))
64 (with-current-buffer (url-retrieve-synchronously url)
65 (let ((data (oauth2-request-access-parse)))
66 (kill-buffer (current-buffer))
67 data))))
68
69 (defstruct oauth2-token
70 plstore
71 plstore-id
72 client-id
73 client-secret
74 access-token
75 refresh-token
76 token-url)
77
78 (defun oauth2-request-access (token-url client-id client-secret code)
79 "Request OAuth access at TOKEN-URL.
80 The CODE should be obtained with `oauth2-request-authorization'.
81 Return an `oauth2-token' structure."
82 (when code
83 (let ((result
84 (oauth2-make-access-request
85 token-url
86 (concat
87 "client_id=" client-id
88 "&client_secret=" client-secret
89 "&code=" code
90 "&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code"))))
91 (make-oauth2-token :client-id client-id
92 :client-secret client-secret
93 :access-token (aget result 'access_token)
94 :refresh-token (aget result 'refresh_token)
95 :token-url token-url))))
96
97 ;;;###autoload
98 (defun oauth2-refresh-access (token)
99 "Refresh OAuth access TOKEN.
100 TOKEN should be obtained with `oauth2-request-access'."
101 (setf (oauth2-token-access-token token)
102 (aget (oauth2-make-access-request
103 (oauth2-token-token-url token)
104 (concat "client_id=" (oauth2-token-client-id token)
105 "&client_secret=" (oauth2-token-client-secret token)
106 "&refresh_token=" (oauth2-token-refresh-token token)
107 "&grant_type=refresh_token"))
108 'access_token))
109 ;; If the token has a plstore, update it
110 (let ((plstore (oauth2-token-plstore token)))
111 (when plstore
112 (plstore-put plstore (oauth2-token-plstore-id token)
113 nil `(:access-token
114 ,(oauth2-token-access-token token)
115 :refresh-token
116 ,(oauth2-token-refresh-token token)))
117 (plstore-save plstore)))
118 token)
119
120 ;;;###autoload
121 (defun oauth2-auth (auth-url token-url client-id client-secret &optional scope state)
122 "Authenticate application via OAuth2."
123 (oauth2-request-access
124 token-url
125 client-id
126 client-secret
127 (oauth2-request-authorization
128 auth-url client-id scope state)))
129
130 (defcustom oauth2-token-file (concat user-emacs-directory "oauth2.plstore")
131 "File path where store OAuth tokens."
132 :group 'oauth2
133 :type 'file)
134
135 (defun oauth2-compute-id (auth-url token-url resource-url)
136 "Compute an unique id based on URLs.
137 This allows to store the token in an unique way."
138 (secure-hash 'md5 (concat auth-url token-url resource-url)))
139
140 ;;;###autoload
141 (defun oauth2-auth-and-store (auth-url token-url resource-url client-id client-secret)
142 "Request access to a resource and store it using `plstore'."
143 ;; We store a MD5 sum of all URL
144 (let* ((plstore (plstore-open oauth2-token-file))
145 (id (oauth2-compute-id auth-url token-url resource-url))
146 (plist (cdr (plstore-get plstore id))))
147 ;; Check if we found something matching this access
148 (if plist
149 ;; We did, return the token object
150 (make-oauth2-token :plstore plstore
151 :plstore-id id
152 :client-id client-id
153 :client-secret client-secret
154 :access-token (plist-get plist :access-token)
155 :refresh-token (plist-get plist :refresh-token)
156 :token-url token-url)
157 (let ((token (oauth2-auth auth-url token-url
158 client-id client-secret resource-url)))
159 ;; Set the plstore
160 (setf (oauth2-token-plstore token) plstore)
161 (setf (oauth2-token-plstore-id token) id)
162 (plstore-put plstore id nil `(:access-token
163 ,(oauth2-token-access-token token)
164 :refresh-token
165 ,(oauth2-token-refresh-token token)))
166 (plstore-save plstore)
167 token))))
168
169 (defun oauth2-url-append-access-token (token url)
170 "Append access token to URL."
171 (concat url
172 (if (string-match-p "\?" url) "&" "?")
173 "access_token=" (oauth2-token-access-token token)))
174
175 ;;;###autoload
176 (defun oauth2-url-retrieve-synchronously (token url)
177 "Retrieve an URL synchronously using TOKENS to access it.
178 TOKENS can be obtained with `oauth2-auth'."
179 (let (tokens-need-renew)
180 (flet ((url-http-handle-authentication (proxy)
181 (setq tokens-need-renew t)
182 ;; This is to make `url' think
183 ;; it's done.
184 (setq success t)))
185 (let ((url-buffer (url-retrieve-synchronously
186 (oauth2-url-append-access-token token url))))
187 (if tokens-need-renew
188 (oauth2-url-retrieve-synchronously (oauth2-refresh-access token) url)
189 url-buffer)))))
190
191 (provide 'oauth2)
192
193 ;;; oauth2.el ends here