aboutsummaryrefslogtreecommitdiff
path: root/lisp/mastodon-auth.el
diff options
context:
space:
mode:
authorJohnson Denen <johnson.denen@gmail.com>2018-03-05 22:09:37 -0500
committerGitHub <noreply@github.com>2018-03-05 22:09:37 -0500
commitae8dabda04e377a6ac22cb854e4844f68073f533 (patch)
treeb6c875c5e88e72966440d3641ef37d320ee2d9fd /lisp/mastodon-auth.el
parente08bb5794762d22f90e85fd65cef7c143e6b9318 (diff)
parente9920d64b5283fca6a34b2144a5a35c4c1d02938 (diff)
Merge pull request #173 from jdenen/develop
Merge 0.7.2 into master
Diffstat (limited to 'lisp/mastodon-auth.el')
-rw-r--r--lisp/mastodon-auth.el44
1 files changed, 33 insertions, 11 deletions
diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el
index 83d7d04..e9889d9 100644
--- a/lisp/mastodon-auth.el
+++ b/lisp/mastodon-auth.el
@@ -2,7 +2,7 @@
;; Copyright (C) 2017 Johnson Denen
;; Author: Johnson Denen <johnson.denen@gmail.com>
-;; Version: 0.7.1
+;; Version: 0.7.2
;; Homepage: https://github.com/jdenen/mastodon.el
;; Package-Requires: ((emacs "24.4"))
@@ -40,8 +40,11 @@
: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.")
+
+(defvar mastodon-auth--acct-alist nil
+ "Alist of account accts (name@domain) keyed by instance url.")
(defun mastodon-auth--generate-token ()
"Make POST to generate auth token."
@@ -53,7 +56,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 +71,31 @@
(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))
+
+(defun mastodon-auth--get-account-name ()
+ "Request user credentials and return an account name."
+ (cdr (assoc
+ 'acct
+ (mastodon-http--get-json
+ (mastodon-http--api
+ "accounts/verify_credentials")))))
+
+(defun mastodon-auth--user-acct ()
+ "Return a mastodon user acct name."
+ (or (cdr (assoc mastodon-instance-url mastodon-auth--acct-alist))
+ (let ((acct (mastodon-auth--get-account-name)))
+ (push (cons mastodon-instance-url acct) mastodon-auth--acct-alist)
+ acct)))
(provide 'mastodon-auth)
;;; mastodon-auth.el ends here