aboutsummaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
authorAbhiseck Paira <abhiseckpaira@disroot.org>2021-12-28 18:40:53 +0530
committerAbhiseck Paira <abhiseckpaira@disroot.org>2022-01-13 20:27:15 +0530
commit2d5717faf83d59d11a4f89ce96eb326a39a5ee56 (patch)
tree6090210abe546750e9a02df185b450f92f4236d1 /lisp
parente18a2d541fca4c5f08f35d1de72d3a3ee9cfe011 (diff)
refactor *-access-token and *-handle-token-response
Refactor `mastodon-auth--access-token' and `mastodon-auth--handle-token-response' to work with the new authentication mechanism.
Diffstat (limited to 'lisp')
-rw-r--r--lisp/mastodon-auth.el32
1 files changed, 22 insertions, 10 deletions
diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el
index 55db0c0..71f790a 100644
--- a/lisp/mastodon-auth.el
+++ b/lisp/mastodon-auth.el
@@ -111,16 +111,26 @@ if you are happy with unencryped storage use e.g. \"~/authinfo\"."
(json-read-from-string json-string))))
(defun mastodon-auth--access-token ()
- "Return exiting or generate new access token.
-
-If an access token for `mastodon-instance-url' is in
-`mastodon-auth--token-alist', return it.
-
-Otherwise, generate a token and pass it to
-`mastodon-auth--handle-token-reponse'."
- (if-let ((token (cdr (assoc mastodon-instance-url mastodon-auth--token-alist))))
- token
- (mastodon-auth--handle-token-response (mastodon-auth--get-token))))
+ "Return the access token to use with `mastodon-instance-url'.
+
+Generate/save token if none known yet."
+ (cond (mastodon-auth--token-alist
+ ;; user variables are known and
+ ;; initialised already.
+ (alist-get mastodon-instance-url mastodon-auth--token-alist
+ nil nil 'equal))
+ ((plist-get (mastodon-client-active-user) :access_token)
+ ;; user variables needs to initialised by reading from
+ ;; plstore.
+ (push (cons mastodon-instance-url
+ (plist-get (mastodon-client-active-user) :access_token))
+ mastodon-auth--token-alist)
+ (alist-get mastodon-instance-url mastodon-auth--token-alist
+ nil nil 'equal))
+ (t
+ ;; user access-token needs to fetched from the server and
+ ;; stored and variables initialised.
+ (mastodon-auth--handle-token-response (mastodon-auth--get-token)))))
(defun mastodon-auth--handle-token-response (response)
"Add token RESPONSE to `mastodon-auth--token-alist'.
@@ -131,6 +141,8 @@ Handle any errors from the server."
(pcase response
((and (let token (plist-get response :access_token))
(guard token))
+ (mastodon-client-make-user-active
+ (mastodon-client-store-access-token token))
(cdar (push (cons mastodon-instance-url token)
mastodon-auth--token-alist)))