branch: elpa/mastodon
commit 0fe10fbedfbfd0bfb49aa8e04d2ff1d085a93ab6
Author: marty hiatt <martianhia...@disroot.org>
Commit: marty hiatt <martianhia...@disroot.org>

    add optional auth-source support.
---
 lisp/mastodon-auth.el   | 33 ++++++++++++++++++++++++++++++
 lisp/mastodon-client.el | 54 +++++++++++++++++++++++++++++++------------------
 2 files changed, 67 insertions(+), 20 deletions(-)

diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el
index 6e932a9160..c10429c470 100644
--- a/lisp/mastodon-auth.el
+++ b/lisp/mastodon-auth.el
@@ -215,6 +215,39 @@ Handle any errors from the server."
      (error "Mastodon-auth--access-token: %s: %s" class error))
     (_ (error "Unknown response from mastodon-auth--get-token!"))))
 
+(defun mastodon-auth-source-get (user host &optional token create)
+  "Fetch an auth source token.
+If CREATE, prompt for a token and save it if there is no such entry.
+Return a list of user, password/secret, and the item's save-function."
+  (let* ((auth-source-creation-prompts
+          '((secret . "%u access token: ")))
+         (source
+          (car
+           (auth-source-search :host host :user user
+                               :require '(:user :secret)
+                               :secret (if token token nil)
+                               ;; "create" alone doesn't work here!:
+                               :create (if create t nil)))))
+    (when source
+      (let ((creds
+             `(,(plist-get source :user)
+               ,(auth-info-password source)
+               ,(plist-get source :save-function))))
+        ;; FIXME: is this ok to be here?
+        (when create ;; call save function:
+          (when (functionp (nth 2 creds))
+            (funcall (nth 2 creds))))
+        creds))))
+
+(defun mastodon-auth-source-token (url handle &optional token create)
+  "Parse URL, search auth sourced with it, USERNAME and TOKEN.
+Calls `mastodon-auth-source-get', returns only the token."
+  (let ((host (url-host
+               (url-generic-parse-url url)))
+        (username (car (split-string handle "@"))))
+    (nth 1
+         (mastodon-auth-source-get username host token create))))
+
 (defun mastodon-auth--get-account-name ()
   "Request user credentials and return an account name."
   (alist-get 'acct
diff --git a/lisp/mastodon-client.el b/lisp/mastodon-client.el
index 63ef8c3c31..26aec537b2 100644
--- a/lisp/mastodon-client.el
+++ b/lisp/mastodon-client.el
@@ -134,24 +134,32 @@ Return plist without the KEY."
 
 (defun mastodon-client--make-user-details-plist ()
   "Make a plist with current user details.  Return it."
-  `(:username ,(mastodon-client--form-user-from-vars)
-              :instance ,mastodon-instance-url
-              :client_id ,(plist-get (mastodon-client) :client_id)
-              :client_secret ,(plist-get (mastodon-client) :client_secret)))
+  `( :username ,(mastodon-client--form-user-from-vars)
+     :instance ,mastodon-instance-url
+     :client_id ,(plist-get (mastodon-client) :client_id)
+     :client_secret ,(plist-get (mastodon-client) :client_secret)))
 
 (defun mastodon-client--store-access-token (token)
   "Save TOKEN as :access_token, encrypted, in plstore of the current user.
 Return the plist after the operation."
   (let* ((user-details (mastodon-client--make-user-details-plist))
          (plstore (plstore-open (mastodon-client--token-file)))
-         (username (plist-get user-details :username))
          (key (concat "user-" username))
          (print-length nil)
          (print-level nil))
-    (if mastodon-client-encrypt-access-token
-        (plstore-put plstore key user-details `(:access_token ,token))
-      (plstore-put plstore key
-                   (append user-details `(:access_token ,token)) nil))
+    (cond (mastodon-auth-use-auth-source
+           ;; auth-source:
+           (let ((handle (plist-get user-details :username)))
+             (mastodon-auth-source-token
+              mastodon-instance-url handle token :create)
+             (plstore-put plstore key user-details nil)))
+          ;; plstore encrypted:
+          (mastodon-client-encrypt-access-token
+           (plstore-put plstore key user-details `(:access_token ,token)))
+          (t ;; plstore sans encryption:
+           ;; (kept only because changing from this disrupts users):
+           (plstore-put plstore key
+                        (append user-details `(:access_token ,token)) nil)))
     (plstore-save plstore)
     (plstore-close plstore)
     (cdr (plstore-get plstore key))))
@@ -160,19 +168,25 @@ Return the plist after the operation."
   "USER-DETAILS is a plist consisting of user details.
 Save it to plstore under key \"active-user\", with the :access_token
 value encrypted."
-  (let ((plstore (plstore-open (mastodon-client--token-file))))
+  (let ((plstore (plstore-open (mastodon-client--token-file)))
+        (handle (plist-get user-details :username))
+        (token
+         (if mastodon-auth-use-auth-source
+             (mastodon-auth-source-token mastodon-instance-url handle)
+           (plist-get user-details :access_token)))
+        (sans-token (if mastodon-auth-use-auth-source
+                        user-details
+                      ;; remove acces_token from user-details:
+                      (cl-remf user-details :access_token)
+                      user-details))
+        (print-length nil)
+        (print-level nil))
     (if (not mastodon-client-encrypt-access-token)
         (plstore-put plstore "active-user" user-details nil)
-      (let ((token (plist-get user-details :access_token))
-            (sans-token (progn ;; remove acces_token from user-details
-                          (cl-remf user-details :access_token)
-                          user-details))
-            (print-length nil)
-            (print-level nil))
-        (plstore-put plstore "active-user"
-                     sans-token `(:access_token ,token))))
-    (plstore-save plstore)
-    (plstore-close plstore)))
+      (plstore-put plstore "active-user"
+                   sans-token `(:access_token ,token))))
+  (plstore-save plstore)
+  (plstore-close plstore))
 
 (defun mastodon-client--form-user-from-vars ()
   "Create a username from user variable.  Return that username.

Reply via email to