diff options
author | Johnson Denen <johnson.denen@gmail.com> | 2017-04-08 07:23:07 -0400 |
---|---|---|
committer | jdenen <Johnson.Denen@ascenaretail.com> | 2017-04-09 08:55:36 -0400 |
commit | 96f4b3627b3cbb38b28ba07206fb6196e71e212a (patch) | |
tree | d4229a2dbfa8890fbddf6990beefe5498d9c524f | |
parent | 1457f9f5cd435d395b474e1cdc0949e387e0665c (diff) |
Refactor authorization
-rw-r--r-- | lisp/mastodon-auth.el | 120 | ||||
-rw-r--r-- | lisp/mastodon-http.el | 43 | ||||
-rw-r--r-- | lisp/mastodon-toot.el | 45 | ||||
-rw-r--r-- | lisp/mastodon.el | 19 |
4 files changed, 167 insertions, 60 deletions
diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el index 86665f3..5a52f8c 100644 --- a/lisp/mastodon-auth.el +++ b/lisp/mastodon-auth.el @@ -1,54 +1,86 @@ -(defun mastodon--read-access-token-file () - (let* ((plstore (plstore-open mastodon-token-file)) - (plist (cdr (plstore-get plstore "mastodon")))) - (if (plist-get plist :access_token) - plist))) - -(defun mastodon--access-token-p () - (if (mastodon--get-client-token) - t)) - -(defun mastodon--get-client-token () - (plist-get mastodon--client-plist :access_token)) - -(defun mastodon--read-or-get-access-token () - (if (mastodon--access-token-p) - (mastodon--get-client-token) - (progn - (mastodon--client-id-and-secret) - (sleep-for 1) ;; FIXME - (mastodon--access-token) - (sleep-for 2) ;; FIXME - (mastodon--store-access-token-file) - (mastodon--get-client-token)))) - -(defun mastodon--client-id-and-secret () - "Adds client_id and client_secret to `mastodon--client-plist'." +(require 'mastodon) +(require 'mastodon-http) + +(defgroup mastodon-auth nil + "Authenticate with Mastodon." + :group 'mastodon) + +(defvar mastodon--client-app-plist nil) +(defvar mastodon--api-token-string nil) + +(defun mastodon--register-client-app-triage (status) + (mastodon--http-response-triage status + (lambda () (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 () + "Adds client_id and client_secret to `mastodon--client-plist'. + + Returns a `plist' with CLIENT_ID and CLIENT_SECRET." (mastodon--http-post (mastodon--api-for "apps") - (lambda (status) (let ((client-data (mastodon--json-hash-table status))) - (setq mastodon--client-plist `(:client_id ,(gethash "client_id" client-data) :client_secret ,(gethash "client_secret" client-data))))) + 'mastodon--register-client-app-triage '(("client_name" . "mastodon.el") ("redirect_uris" . "urn:ietf:wg:oauth:2.0:oob") ("scopes" . "read write follow")))) -(defun mastodon--store-access-token-file () - (let ((plstore (plstore-open mastodon-token-file))) - (plstore-put plstore "mastodon" nil - `(:client_id - ,(plist-get mastodon--client-plist :client_id) - :client_secret - ,(plist-get mastodon--client-plist :client_secret) - :access_token - ,(plist-get mastodon--client-plist :access_token))) - (plstore-save plstore))) +(defun mastodon--register-and-return-client-app () + (progn + (mastodon--register-client-app) + mastodon--client-app-plist)) -(defun mastodon--access-token () - "Adds access_token to `mastodon--client-plist'." +(defun mastodon--store-client-id-and-secret () + (let ((client-plist (mastodon--register-and-return-client-app)) + (plstore (plstore-open mastodon-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 () + (if (plist-get mastodon--client-app-plist :client_secret) + mastodon--client-app-plist + (let* ((plstore (plstore-open mastodon-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--get-access-token-triage (status) + (mastodon--http-response-triage status + (lambda () + (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 () (mastodon--http-post (concat mastodon-instance-url "/oauth/token") - (lambda (status) (let ((token-data (mastodon--json-hash-table status))) - (setq mastodon--client-plist (plist-put mastodon--client-plist :access_token (gethash "access_token" token-data))))) - `(("client_id" . ,(plist-get mastodon--client-plist :client_id)) - ("client_secret" . ,(plist-get mastodon--client-plist :client_secret)) + '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" . ,(read-string "Email: ")) ("password" . ,(read-passwd "Password: "))))) + +(defun mastodon--access-token () + (if mastodon--api-token-string + mastodon--api-token-string + (progn + (mastodon--get-access-token) + (while (not mastodon--api-token-string) + (sleep-for 1) + (mastodon--access-token)) + mastodon--api-token-string))) + +(provide 'mastodon-auth) diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index 22c8c3f..0e49c08 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -1,3 +1,5 @@ +(require 'mastodon) + (defun mastodon--api-for (endpoint) "Returns Mastondon API URL for ENDPOINT." (concat mastodon-instance-url "/api/" mastodon--api-version "/" endpoint)) @@ -18,17 +20,44 @@ Response buffer is passed to the CALLBACK function." "&"))) (url-retrieve url callback))) -(defun mastodon--response-json (status) - "Returns JSON string from `mastodon--http-post' response buffer." - (let ((resp (with-current-buffer (current-buffer) - (buffer-substring-no-properties (point-min) (point-max))))) +(defun mastodon--response-buffer () + (with-current-buffer (current-buffer) + (buffer-substring-no-properties (point-min) (point-max)))) + +(defun mastodon--response-body-substring (pattern) + (let ((resp (mastodon--response-buffer))) (progn - (string-match "\{.*\}" resp) + (string-match pattern resp) (match-string 0 resp)))) -(defun mastodon--json-hash-table (status) +(defun mastodon--response-match-p (pattern) + (let ((resp (mastodon--response-buffer))) + (string-match-p pattern resp))) + +(defun mastodon--response-status-p () + (when (mastodon--response-match-p "^HTTP/1.*$") t)) + +(defun mastodon--response-json () + (mastodon--response-body-substring "\{.*\}")) + +(defun mastodon--response-code () + (let* ((status-line (mastodon--response-body-substring "^HTTP/1.*$"))) + (progn + (string-match "[0-9][0-9][0-9]" status-line) + (match-string 0 status-line)))) + +(defun mastodon--json-hash-table () "Reads JSON string from `mastodon--response-json' into a hash table." (let ((json-object-type 'hash-table) (json-array-type 'list) (json-key-type 'string)) - (json-read-from-string (mastodon--response-json status)))) + (json-read-from-string (mastodon--response-json)))) + +(defun mastodon--http-response-triage (status success) + (when (not (mastodon--response-status)) + (mastodon--http-response-triage status)) + (if (string-prefix-p "2" (mastodon--response-code)) + (funcall success) + (switch-to-buffer (current-buffer)))) + +(provide 'mastodon-http) diff --git a/lisp/mastodon-toot.el b/lisp/mastodon-toot.el new file mode 100644 index 0000000..82fac8c --- /dev/null +++ b/lisp/mastodon-toot.el @@ -0,0 +1,45 @@ +(require 'mastodon-auth) +(require 'mastodon-http) + +(defgroup mastodon-toot nil + "Capture Mastodon toots." + :group 'mastodon) + +(defun mastodon-new-toot () + (interactive) + (progn + (switch-to-buffer-other-window (get-buffer-create "*new toot*")) + (mastodon-toot-mode t))) + +(defun mastodon-toot--send () + (interactive) + (let ((toot (buffer-string)) + (endpoint (mastodon--api-for "statuses"))) + (progn + (kill-buffer-and-window) + (mastodon--http-post endpoint + (lambda (status) (switch-to-buffer (current-buffer))) ;; FIXME + `(("status" . ,toot) + ("visibility" . "public")) + `(("Authorization" . ,(concat + "Bearer " + (mastodon--access-token)))))))) + +(defun mastodon-toot--cancel () + (interactive) + (kill-buffer-and-window)) + +(defvar mastodon-toot-mode-map + (let ((map (make-sparse-keymap))) + (define-key map (kbd "C-c C-c") #'mastodon-toot--send) + (define-key map (kbd "C-c C-k") #'mastodon-toot--cancel) + map) + "Keymap for `mastodon-toot-mode'.") + +(define-minor-mode mastodon-toot-mode + "Minor mode to capture Mastodon toots." + :group 'mastodon-toot + :keymap mastodon-toot-mode-map + :global nil) + +(provide 'mastodon-toot) diff --git a/lisp/mastodon.el b/lisp/mastodon.el index b3946f8..0103615 100644 --- a/lisp/mastodon.el +++ b/lisp/mastodon.el @@ -1,7 +1,5 @@ -(load-file "mastodon-auth.el") -(load-file "mastodon-http.el") - -(defvar mastodon--api-version "v1") +(defgroup mastodon nil + "Interface with Mastodon.") (defcustom mastodon-instance-url "https://mastodon.social" "Base URL for the Masto instance from which you toot." @@ -14,10 +12,13 @@ :group 'mastodon :type 'file) -(defvar mastodon--client-plist (mastodon--read-access-token-file) - "Stores CLIENT_ID, CLIENT_SECRET, and ACCESS_TOKEN. +(defvar mastodon--api-version "v1") -Reads values from `mastodon-token-file' if they exist.") +;;;###autoload +(defun mastodon () + (interactive) + (load-file "mastodon-http.el") + (load-file "mastodon-auth.el") + (load-file "mastodon-toot.el")) -(defvar mastodon--token (mastodon--read-or-get-access-token) - "API token for Mastodon.") +(provide 'mastodon) |