aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohnson Denen <johnson.denen@gmail.com>2017-04-23 00:51:49 -0400
committerJohnson Denen <johnson.denen@gmail.com>2017-04-23 11:09:06 -0400
commit950a71a7e89efe48804a89322f9a974610d40770 (patch)
treed68e672d8e7df9a4eb3f34ef6921aa49fce80978
parent949912d793cc8b80e6acb0978268405dc21a13e9 (diff)
Rewrite mastodon-auth
- Remove client functions - Use mastodon-http--post - No more callback spaghetti
-rw-r--r--lisp/mastodon-auth.el159
-rw-r--r--lisp/mastodon-http.el4
-rw-r--r--lisp/mastodon-toot.el1
-rw-r--r--test/mastodon-auth-tests.el162
-rw-r--r--test/mastodon-http-tests.el2
-rw-r--r--test/mastodon-toot-tests.el2
6 files changed, 71 insertions, 259 deletions
diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el
index 69404ac..09813d3 100644
--- a/lisp/mastodon-auth.el
+++ b/lisp/mastodon-auth.el
@@ -28,140 +28,45 @@
;;; Code:
(require 'plstore)
-(require 'mastodon-http)
+(require 'mastodon-client)
(defgroup mastodon-auth nil
"Authenticate with Mastodon."
:prefix "mastodon-auth-"
:group 'mastodon)
-(defvar mastodon--client-app-plist nil)
-(defvar mastodon--api-token-string nil)
-
-(defun mastodon-auth--token-file ()
- "Returns `mastodon-token-file' string."
- mastodon-token-file)
-
-(defun mastodon-auth--registration-success ()
- (let ((client-data (mastodon--json-hash-table)))
- (setq mastodon--client-app-plist
- `(:client_id
- ,(gethash "client_id" client-data)
- :client_secret
- ,(gethash "client_secret" client-data)))))
-
-(defun mastodon--register-client-app-triage (status)
- "Callback function to triage `mastodon--register-client-app' response.
-
-STATUS is passed by `url-retrieve'."
- (mastodon--http-response-triage status
- 'mastodon-auth--registration-success))
-
-(defun mastodon--register-client-app ()
- "Add `:client_id' and `client_secret' to `mastodon--client-plist'."
- (mastodon--http-post (mastodon--api-for "apps")
- 'mastodon--register-client-app-triage
- '(("client_name" . "mastodon.el")
- ("redirect_uris" . "urn:ietf:wg:oauth:2.0:oob")
- ("scopes" . "read write follow")
- ("website" . "https://github.com/jdenen/mastodon.el"))))
-
-(defun mastodon-auth--client-app-secret-p ()
- "Return t if `mastodon--client-app-plist' has a :client_secret value."
- (when (plist-get mastodon--client-app-plist :client_secret) t))
-
-(defun mastodon-auth--client ()
- "Return `mastodon--client-app-plist' value."
- mastodon--client-app-plist)
-
-(defun mastodon--register-and-return-client-app ()
- "Register `mastodon' with an instance. Return `mastodon--client-app-plist'."
- (if (mastodon-auth--client-app-secret-p)
- (mastodon-auth--client)
- (progn
- (mastodon--register-client-app)
- (sleep-for 2)
- (mastodon--register-and-return-client-app))))
-
-(defun mastodon--store-client-id-and-secret ()
- "Store `:client_id' and `:client_secret' in a plstore."
- (let ((client-plist (mastodon--register-and-return-client-app))
- (plstore (plstore-open (mastodon-auth--token-file))))
- (plstore-put plstore "mastodon" `(:client_id
- ,(plist-get client-plist :client_id)
- :client_secret
- ,(plist-get client-plist :client_secret))
- nil)
- (plstore-save plstore)
- client-plist))
-
-(defun mastodon--client-app ()
- "Return `mastodon--client-app-plist'.
-
-If not set, retrieves client data from `mastodon-token-file'.
-If no data can be found in the token file, registers the app and stores its data via `mastodon--store-client-id-and-secret'."
- (if (plist-get mastodon--client-app-plist :client_secret)
- mastodon--client-app-plist
- (let* ((plstore (plstore-open (mastodon-auth--token-file)))
- (mastodon (plstore-get plstore "mastodon")))
- (if mastodon
- (progn
- (setq mastodon--client-app-plist (delete "mastodon" mastodon))
- mastodon--client-app-plist)
- (progn
- (setq mastodon--client-app-plist (mastodon--store-client-id-and-secret))
- mastodon--client-app-plist)))))
-
-(defun mastodon-auth--get-token-success ()
- (let ((token-data (mastodon--json-hash-table)))
- (progn
- (setq mastodon--api-token-string (gethash "access_token" token-data))
- mastodon--api-token-string)))
-
-(defun mastodon--get-access-token-triage (status)
- "Callback function to triage `mastodon--get-access-token' response.
-
-STATUS is passed by `url-retrieve'."
- (mastodon--http-response-triage status
- 'mastodon-auth--get-token-success))
-
-(defun mastodon-auth--user-and-passwd ()
- "Prompt for user email and password."
- (let ((email (read-string "Email: "))
- (passwd (read-passwd "Password: ")))
- (cons email passwd)))
-
-(defun mastodon--get-access-token ()
- "Retrieve access token from instance.
-
-Authenticates with email address and password. Neither are not stored."
- (let* ((creds (mastodon-auth--user-and-passwd))
- (email (car creds))
- (passwd (cdr creds)))
- (mastodon--http-post (concat mastodon-instance-url "/oauth/token")
- 'mastodon--get-access-token-triage
- `(("client_id" . ,(plist-get (mastodon--client-app) :client_id))
- ("client_secret" . ,(plist-get (mastodon--client-app) :client_secret))
- ("grant_type" . "password")
- ("username" . ,email)
- ("password" . ,passwd)
- ("scope" . "read write follow")))))
-
-(defun mastodon-auth--token ()
- "Return `mastodon--api-token-string'."
- mastodon--api-token-string)
-
-(defun mastodon--access-token ()
- "Return `mastodon--api-token-string'.
-
-If not set, retrieves token with `mastodon--get-access-token'."
- (or (mastodon-auth--token)
+(defvar mastodon-auth--token nil
+ "")
+
+(defun mastodon-auth--generate-token ()
+ ""
+ (mastodon-http--post
+ (concat mastodon-instance-url "/oauth/token")
+ `(("client_id" . ,(plist-get (mastodon-client) :client_id))
+ ("client_secret" . ,(plist-get (mastodon-client) :client_secret))
+ ("grant_type" . "password")
+ ("username" . ,(read-string "Email: "))
+ ("password" . ,(read-passwd "Password: "))
+ ("scope" . "read write follow"))
+ nil))
+
+(defun mastodon-auth--get-token ()
+ ""
+ (with-current-buffer (mastodon-auth--generate-token)
+ (goto-char (point-min))
+ (re-search-forward "^$" nil 'move)
+ (let ((json-object-type 'plist)
+ (json-key-type 'keyword)
+ (json-array-type 'vector)
+ (json-string (buffer-substring-no-properties (point) (point-max))))
+ (json-read-from-string json-string))))
+
+(defun mastodon-auth--access-token ()
+ (or mastodon-auth--token
(progn
- (mastodon--get-access-token)
- (or (mastodon-auth--token)
- (progn
- (sleep-for 2)
- (mastodon--access-token))))))
+ (let* ((json (mastodon-auth--get-token))
+ (token (plist-get json :access_token)))
+ (setq mastodon-auth--token token)))))
(provide 'mastodon-auth)
;;; mastodon-auth.el ends here
diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el
index 582f3a8..e6e5549 100644
--- a/lisp/mastodon-http.el
+++ b/lisp/mastodon-http.el
@@ -131,7 +131,7 @@ Authorization header is included by default."
args
"&")))
(url-request-extra-headers
- `(("Authorization" . ,(concat "Bearer " (mastodon--access-token)))
+ `(("Authorization" . ,(concat "Bearer " mastodon-auth--token))
,headers)))
(with-temp-buffer
(url-retrieve-synchronously url))))
@@ -143,7 +143,7 @@ Pass response buffer to CALLBACK function."
(let ((url-request-method "GET")
(url-request-extra-headers
`(("Authorization" . ,(concat "Bearer "
- (mastodon--access-token))))))
+ (mastodon-auth--access-token))))))
(url-retrieve-synchronously url)))
(defun mastodon-http--get-json (url)
diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el
index 0346f46..9096a47 100644
--- a/lisp/mastodon-toot.el
+++ b/lisp/mastodon-toot.el
@@ -28,7 +28,6 @@
;;; Code:
(require 'mastodon-auth)
-(require 'mastodon-http)
(defgroup mastodon-toot nil
"Capture Mastodon toots."
diff --git a/test/mastodon-auth-tests.el b/test/mastodon-auth-tests.el
index b3edea8..e46f1a6 100644
--- a/test/mastodon-auth-tests.el
+++ b/test/mastodon-auth-tests.el
@@ -1,132 +1,40 @@
(require 'el-mock)
-(ert-deftest mastodon-auth:token-file ()
- "Should return `mastodon-token-file' value."
- (should (string= (mastodon-auth--token-file) "~/.emacs.d/mastodon.plstore")))
-
-(ert-deftest mastodon-auth:registration-success ()
- "Should set `mastodon--client-app-plist' on succesful registration."
- (let ((hash (make-hash-table :test 'equal))
- (client-plist '(:client_id "client-id" :client_secret "client-secret")))
- (puthash "client_id" "client-id" hash)
- (puthash "client_secret" "client-secret" hash)
- (with-mock
- (stub mastodon--json-hash-table => hash)
- (mastodon-auth--registration-success)
- (should (equal client-plist mastodon--client-app-plist)))))
-
-(ert-deftest mastodon-auth:register-client-app ()
- "Should POST client data to /apps endpoint and return client plist."
- (let ((app-plist '("id" "id-val" "secret" "secret-val")))
- (with-mock
- (mock (mastodon--api-for "apps") => "https://instance/api/v1/apps")
- (mock (mastodon--register-client-app-triage "status") => app-plist)
- (mock (mastodon--http-post "https://instance/api/v1/apps"
- 'mastodon--register-client-app-triage
- '(("client_name" . "mastodon.el")
- ("redirect_uris" . "urn:ietf:wg:oauth:2.0:oob")
- ("scopes" . "read write follow")
- ("website" . "https://github.com/jdenen/mastodon.el")))
- => (funcall 'mastodon--register-client-app-triage "status"))
- (should (eq app-plist (mastodon--register-client-app))))))
-
-(ert-deftest mastodon-auth:register-client-app-triage ()
- "Should wrap `mastodon--http-response-triage' call and return client plist."
- (let ((app-plist '("id" "id-val" "secret" "secret-val")))
- (with-mock
- (mock (mastodon-auth--registration-success) => app-plist)
- (mock (mastodon--http-response-triage "status" 'mastodon-auth--registration-success)
- => (funcall 'mastodon-auth--registration-success))
- (should (eq app-plist (mastodon--register-client-app-triage "status"))))))
-
-(ert-deftest mastodon-auth:register-and-return-client-app:with-p ()
- "Should return a plist of client_id and client_secret without registration."
- (let ((app-plist '(:client_id "id-val" :client_secret "secret-val")))
- (with-mock
- (mock (mastodon-auth--client-app-secret-p) => t)
- (mock (mastodon-auth--client) => app-plist)
- (stub mastodon-auth--registration-success => mastodon--client-app-plist)
- (not-called sleep-for)
- (should (equal app-plist (mastodon--register-and-return-client-app))))))
-
-(defun helper:read-plstore (file key)
- (let* ((plstore (plstore-open file))
- (masto (delete "mastodon" (plstore-get plstore "mastodon"))))
- (progn
- (plstore-close plstore)
- (plist-get masto key))))
-
-(ert-deftest mastodon-auth:store-client-id-and-secret ()
- "Should create plstore from client plist. Should return plist."
- (let ((app-plist '(:client_id "id-val" :client_secret "secret-val")))
- (with-mock
- (mock (mastodon--register-and-return-client-app) => app-plist)
- (mock (mastodon-auth--token-file) => "stubfile.plstore")
- (should (eq app-plist (mastodon--store-client-id-and-secret)))
- (should (string= (helper:read-plstore (mastodon-auth--token-file) :client_id) "id-val"))
- (should (string= (helper:read-plstore (mastodon-auth--token-file) :client_secret) "secret-val")))))
-
-(ert-deftest mastodon-auth:client-app:memoized ()
- "Should return `mastodon--client-app-plist' if it has a :client_secret."
- (with-mock
- (mock (plist-get mastodon--client-app-plist :client_secret) => t)
- (should (eq (mastodon--client-app) mastodon--client-app-plist))))
-
-(ert-deftest mastodon-auth:client-app:stored ()
- "Should retrieve from `mastodon-token-file' if not memoized."
+(ert-deftest generate-token ()
+ ""
(with-mock
- (stub plist-get)
- (mock (mastodon-auth--token-file) => "fixture/client.plstore")
- (should (equal (mastodon--client-app) '(:client_id "id" :client_secret "secret")))))
-
-(ert-deftest mastodon-auth:client-app:generated ()
- "Should generate `mastodon--client-app-plist' if not memoized or stored."
- (with-mock
- (stub plist-get)
- (mock (mastodon-auth--token-file) => "fixture/empty.plstore")
- (mock (mastodon--store-client-id-and-secret))
- (mastodon--client-app)))
-
-(ert-deftest mastodon-auth:get-token-success ()
- "Should return access token from `url-retrieve' response JSON."
- (let ((hash (make-hash-table :test 'equal)))
- (puthash "access_token" "token-value" hash)
- (with-mock
- (mock (mastodon--json-hash-table) => hash)
- (should (string= (mastodon-auth--get-token-success) "token-value")))))
-
-(ert-deftest mastodon-auth:get-access-token-triage ()
- "Should wrap `mastodon--http-response-triage'."
- (with-mock
- (mock (mastodon--http-response-triage "status" 'mastodon-auth--get-token-success))
- (mastodon--get-access-token-triage "status")))
-
-(ert-deftest mastodon-auth:get-access-token ()
- "Should POST auth data to retrieve access token."
- (let ((client-app '(:client_id "id" :client_secret "secret")))
- (with-mock
- (mock (mastodon-auth--user-and-passwd) => (cons "email" "password"))
- (mock (mastodon--client-app) => client-app)
- (mock (mastodon--http-post "https://mastodon.social/oauth/token"
- 'mastodon--get-access-token-triage
+ (let ((mastodon-instance-url "https://instance.url"))
+ (mock (mastodon-client) => '(:client_id "id" :client_secret "secret"))
+ (mock (read-string "Email: ") => "foo@bar.com")
+ (mock (read-passwd "Password: ") => "password")
+ (mock (mastodon-http--post "https://instance.url/oauth/token"
'(("client_id" . "id")
("client_secret" . "secret")
- ("grant_type" . "password")
- ("username" . "email")
- ("password" . "password")
- ("scope" . "read write follow"))))
- (mastodon--get-access-token))))
-
-(ert-deftest mastodon-auth:access-token:memoized ()
- "Should return `mastodon--api-token-string' if set."
- (with-mock
- (mock (mastodon-auth--token) => "foobar")
- (should (string= (mastodon--access-token) "foobar"))))
-
-(ert-deftest mastodon-auth:access-token:generated ()
- "Should generate `mastodon--api-token-string' if not memoized."
- (with-mock
- (mock (mastodon-auth--token) => nil)
- (mock (mastodon--get-access-token)
- => (mock (mastodon-auth--token) => "foobar"))
- (should (string= (mastodon--access-token) "foobar"))))
+ ("grant_type" . "password")
+ ("username" . "foo@bar.com")
+ ("password" . "password")
+ ("scope" . "read write follow"))
+ nil))
+ (mastodon-auth--generate-token))))
+
+(ert-deftest get-token ()
+ ""
+ (with-temp-buffer
+ (with-mock
+ (mock (mastodon-auth--generate-token) => (progn
+ (insert "\n\n{\"access_token\":\"abcdefg\"}")
+ (current-buffer)))
+ (should (equal (mastodon-auth--get-token) '(:access_token "abcdefg"))))))
+
+(ert-deftest access-token-1 ()
+ ""
+ (let ((mastodon-auth--token "foobar"))
+ (should (string= (mastodon-auth--access-token) "foobar"))))
+
+(ert-deftest access-token-2 ()
+ ""
+ (let ((mastodon-auth--token nil))
+ (with-mock
+ (mock (mastodon-auth--get-token) => '(:access_token "foobaz"))
+ (should (string= (mastodon-auth--access-token) "foobaz"))
+ (should (string= mastodon-auth--token "foobaz")))))
diff --git a/test/mastodon-http-tests.el b/test/mastodon-http-tests.el
index 06b92d5..972cedb 100644
--- a/test/mastodon-http-tests.el
+++ b/test/mastodon-http-tests.el
@@ -5,5 +5,5 @@
(let ((callback-double (lambda () "double")))
(with-mock
(mock (url-retrieve-synchronously "https://foo.bar/baz"))
- (mock (mastodon--access-token) => "test-token")
+ (mock (mastodon-auth--access-token) => "test-token")
(mastodon-http--get "https://foo.bar/baz"))))
diff --git a/test/mastodon-toot-tests.el b/test/mastodon-toot-tests.el
index eae9e89..ca04ae8 100644
--- a/test/mastodon-toot-tests.el
+++ b/test/mastodon-toot-tests.el
@@ -10,7 +10,7 @@
(stub mastodon--api-for => "https://instance/api/v/statuses")
(stub buffer-string => "This is a test toot")
(stub kill-buffer-and-window)
- (stub mastodon--access-token => "access-token-string")
+ (stub mastodon-auth--access-token => "access-token-string")
(mock (mastodon--http-post "https://instance/api/v/statuses"
'mastodon-toot--send-triage
'(("status" . "This is a test toot"))