aboutsummaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
Diffstat (limited to 'lisp')
-rw-r--r--lisp/mastodon-auth.el24
-rw-r--r--lisp/mastodon-client.el30
-rw-r--r--lisp/mastodon-http.el11
3 files changed, 38 insertions, 27 deletions
diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el
index 83d7d04..b2399d2 100644
--- a/lisp/mastodon-auth.el
+++ b/lisp/mastodon-auth.el
@@ -40,8 +40,8 @@
:prefix "mastodon-auth-"
:group 'mastodon)
-(defvar mastodon-auth--token nil
- "User access token.")
+(defvar mastodon-auth--token-alist nil
+ "Alist of User access tokens keyed by instance url.")
(defun mastodon-auth--generate-token ()
"Make POST to generate auth token."
@@ -53,7 +53,8 @@
("username" . ,(read-string "Email: "))
("password" . ,(read-passwd "Password: "))
("scope" . "read write follow"))
- nil))
+ nil
+ :unauthenticated))
(defun mastodon-auth--get-token ()
"Make auth token request and return JSON response."
@@ -67,13 +68,16 @@
(json-read-from-string json-string))))
(defun mastodon-auth--access-token ()
- "Return `mastodon-auth--token'.
-
-Generate token and set `mastodon-auth--token' if nil."
- (or mastodon-auth--token
- (let* ((json (mastodon-auth--get-token))
- (token (plist-get json :access_token)))
- (setq mastodon-auth--token token))))
+ "Return the access token to use with the current `mastodon-instance-url'.
+
+Generate token and set if none known yet."
+ (let ((token
+ (cdr (assoc mastodon-instance-url mastodon-auth--token-alist))))
+ (unless token
+ (let ((json (mastodon-auth--get-token)))
+ (setq token (plist-get json :access_token))
+ (push (cons mastodon-instance-url token) mastodon-auth--token-alist)))
+ token))
(provide 'mastodon-auth)
;;; mastodon-auth.el ends here
diff --git a/lisp/mastodon-client.el b/lisp/mastodon-client.el
index b97197e..f8beb81 100644
--- a/lisp/mastodon-client.el
+++ b/lisp/mastodon-client.el
@@ -30,6 +30,7 @@
;;; Code:
(require 'plstore)
+(defvar mastodon-instance-url)
(autoload 'mastodon-http--api "mastodon-http")
(autoload 'mastodon-http--post "mastodon-http")
@@ -39,8 +40,8 @@
:group 'mastodon
:type 'file)
-(defvar mastodon-client--client-details nil
- "Client id and secret.")
+(defvar mastodon-client--client-details-alist nil
+ "An alist of Client id and secrets keyed by the instance url.")
(defun mastodon-client--register ()
"POST client to Mastodon."
@@ -50,7 +51,8 @@
("redirect_uris" . "urn:ietf:wg:oauth:2.0:oob")
("scopes" . "read write follow")
("website" . "https://github.com/jdenen/mastodon.el"))
- nil))
+ nil
+ :unauthenticated))
(defun mastodon-client--fetch ()
"Return JSON from `mastodon-client--register' call."
@@ -72,8 +74,8 @@
Make `mastodon-client--fetch' call to determine client values."
(let ((plstore (plstore-open (mastodon-client--token-file)))
- (client (mastodon-client--fetch)))
- (plstore-put plstore "mastodon" client nil)
+ (client (mastodon-client--fetch)))
+ (plstore-put plstore (concat "mastodon-" mastodon-instance-url) client nil)
(plstore-save plstore)
(plstore-close plstore)
client))
@@ -81,19 +83,23 @@ Make `mastodon-client--fetch' call to determine client values."
(defun mastodon-client--read ()
"Retrieve client_id and client_secret from `mastodon-client--token-file'."
(let* ((plstore (plstore-open (mastodon-client--token-file)))
- (mastodon (plstore-get plstore "mastodon")))
- (when mastodon
- (delete "mastodon" mastodon))))
+ (mastodon (plstore-get plstore (concat "mastodon-" mastodon-instance-url))))
+ (cdr mastodon)))
(defun mastodon-client ()
- "Return variable `mastodon-client--client-details' plist.
+ "Return variable client secrets to use for the current `mastodon-instance-url'..
Read plist from `mastodon-client--token-file' if variable is nil.
Fetch and store plist if `mastodon-client--read' returns nil."
- (or mastodon-client--client-details
- (setq mastodon-client--client-details
+ (let ((client-details (cdr (assoc mastodon-instance-url mastodon-client--client-details-alist))))
+ (unless client-details
+ (setq client-details
(or (mastodon-client--read)
- (mastodon-client--store)))))
+ (mastodon-client--store)))
+ (push (cons mastodon-instance-url client-details)
+ mastodon-client--client-details-alist))
+ client-details))
(provide 'mastodon-client)
;;; mastodon-client.el ends here
+
diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el
index f519e20..905a853 100644
--- a/lisp/mastodon-http.el
+++ b/lisp/mastodon-http.el
@@ -31,7 +31,6 @@
(require 'json)
(defvar mastodon-instance-url)
-(defvar mastodon-auth--token)
(autoload 'mastodon-auth--access-token "mastodon-auth")
(defvar mastodon-http--api-version "v1")
@@ -68,10 +67,10 @@ Open RESPONSE buffer if unsuccessful."
(funcall success)
(switch-to-buffer response))))
-(defun mastodon-http--post (url args headers)
+(defun mastodon-http--post (url args headers &optional unauthenticed-p)
"POST synchronously to URL with ARGS and HEADERS.
-Authorization header is included by default."
+Authorization header is included by default unless UNAUTHENTICED-P is non-nil."
(let ((url-request-method "POST")
(url-request-data
(when args
@@ -82,8 +81,10 @@ Authorization header is included by default."
args
"&")))
(url-request-extra-headers
- `(("Authorization" . ,(concat "Bearer " mastodon-auth--token))
- ,headers)))
+ (append
+ (unless unauthenticed-p
+ `(("Authorization" . (concat "Bearer " (mastodon-auth--access-token)))))
+ headers)))
(with-temp-buffer
(url-retrieve-synchronously url))))